esd_usb2.c 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156
  1. /*
  2. * CAN driver for esd CAN-USB/2 and CAN-USB/Micro
  3. *
  4. * Copyright (C) 2010-2012 Matthias Fuchs <matthias.fuchs@esd.eu>, esd gmbh
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms of the GNU General Public License as published
  8. * by the Free Software Foundation; version 2 of the License.
  9. *
  10. * This program is distributed in the hope that it will be useful, but
  11. * WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License along
  16. * with this program; if not, write to the Free Software Foundation, Inc.,
  17. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  18. */
  19. #include <linux/signal.h>
  20. #include <linux/slab.h>
  21. #include <linux/module.h>
  22. #include <linux/netdevice.h>
  23. #include <linux/usb.h>
  24. #include <linux/can.h>
  25. #include <linux/can/dev.h>
  26. #include <linux/can/error.h>
  27. MODULE_AUTHOR("Matthias Fuchs <matthias.fuchs@esd.eu>");
  28. MODULE_DESCRIPTION("CAN driver for esd CAN-USB/2 and CAN-USB/Micro interfaces");
  29. MODULE_LICENSE("GPL v2");
  30. /* Define these values to match your devices */
  31. #define USB_ESDGMBH_VENDOR_ID 0x0ab4
  32. #define USB_CANUSB2_PRODUCT_ID 0x0010
  33. #define USB_CANUSBM_PRODUCT_ID 0x0011
  34. #define ESD_USB2_CAN_CLOCK 60000000
  35. #define ESD_USBM_CAN_CLOCK 36000000
  36. #define ESD_USB2_MAX_NETS 2
  37. /* USB2 commands */
  38. #define CMD_VERSION 1 /* also used for VERSION_REPLY */
  39. #define CMD_CAN_RX 2 /* device to host only */
  40. #define CMD_CAN_TX 3 /* also used for TX_DONE */
  41. #define CMD_SETBAUD 4 /* also used for SETBAUD_REPLY */
  42. #define CMD_TS 5 /* also used for TS_REPLY */
  43. #define CMD_IDADD 6 /* also used for IDADD_REPLY */
  44. /* esd CAN message flags - dlc field */
  45. #define ESD_RTR 0x10
  46. /* esd CAN message flags - id field */
  47. #define ESD_EXTID 0x20000000
  48. #define ESD_EVENT 0x40000000
  49. #define ESD_IDMASK 0x1fffffff
  50. /* esd CAN event ids used by this driver */
  51. #define ESD_EV_CAN_ERROR_EXT 2
  52. /* baudrate message flags */
  53. #define ESD_USB2_UBR 0x80000000
  54. #define ESD_USB2_LOM 0x40000000
  55. #define ESD_USB2_NO_BAUDRATE 0x7fffffff
  56. #define ESD_USB2_TSEG1_MIN 1
  57. #define ESD_USB2_TSEG1_MAX 16
  58. #define ESD_USB2_TSEG1_SHIFT 16
  59. #define ESD_USB2_TSEG2_MIN 1
  60. #define ESD_USB2_TSEG2_MAX 8
  61. #define ESD_USB2_TSEG2_SHIFT 20
  62. #define ESD_USB2_SJW_MAX 4
  63. #define ESD_USB2_SJW_SHIFT 14
  64. #define ESD_USBM_SJW_SHIFT 24
  65. #define ESD_USB2_BRP_MIN 1
  66. #define ESD_USB2_BRP_MAX 1024
  67. #define ESD_USB2_BRP_INC 1
  68. #define ESD_USB2_3_SAMPLES 0x00800000
  69. /* esd IDADD message */
  70. #define ESD_ID_ENABLE 0x80
  71. #define ESD_MAX_ID_SEGMENT 64
  72. /* SJA1000 ECC register (emulated by usb2 firmware) */
  73. #define SJA1000_ECC_SEG 0x1F
  74. #define SJA1000_ECC_DIR 0x20
  75. #define SJA1000_ECC_ERR 0x06
  76. #define SJA1000_ECC_BIT 0x00
  77. #define SJA1000_ECC_FORM 0x40
  78. #define SJA1000_ECC_STUFF 0x80
  79. #define SJA1000_ECC_MASK 0xc0
  80. /* esd bus state event codes */
  81. #define ESD_BUSSTATE_MASK 0xc0
  82. #define ESD_BUSSTATE_WARN 0x40
  83. #define ESD_BUSSTATE_ERRPASSIVE 0x80
  84. #define ESD_BUSSTATE_BUSOFF 0xc0
  85. #define RX_BUFFER_SIZE 1024
  86. #define MAX_RX_URBS 4
  87. #define MAX_TX_URBS 16 /* must be power of 2 */
  88. struct header_msg {
  89. u8 len; /* len is always the total message length in 32bit words */
  90. u8 cmd;
  91. u8 rsvd[2];
  92. };
  93. struct version_msg {
  94. u8 len;
  95. u8 cmd;
  96. u8 rsvd;
  97. u8 flags;
  98. __le32 drv_version;
  99. };
  100. struct version_reply_msg {
  101. u8 len;
  102. u8 cmd;
  103. u8 nets;
  104. u8 features;
  105. __le32 version;
  106. u8 name[16];
  107. __le32 rsvd;
  108. __le32 ts;
  109. };
  110. struct rx_msg {
  111. u8 len;
  112. u8 cmd;
  113. u8 net;
  114. u8 dlc;
  115. __le32 ts;
  116. __le32 id; /* upper 3 bits contain flags */
  117. u8 data[8];
  118. };
  119. struct tx_msg {
  120. u8 len;
  121. u8 cmd;
  122. u8 net;
  123. u8 dlc;
  124. u32 hnd; /* opaque handle, not used by device */
  125. __le32 id; /* upper 3 bits contain flags */
  126. u8 data[8];
  127. };
  128. struct tx_done_msg {
  129. u8 len;
  130. u8 cmd;
  131. u8 net;
  132. u8 status;
  133. u32 hnd; /* opaque handle, not used by device */
  134. __le32 ts;
  135. };
  136. struct id_filter_msg {
  137. u8 len;
  138. u8 cmd;
  139. u8 net;
  140. u8 option;
  141. __le32 mask[ESD_MAX_ID_SEGMENT + 1];
  142. };
  143. struct set_baudrate_msg {
  144. u8 len;
  145. u8 cmd;
  146. u8 net;
  147. u8 rsvd;
  148. __le32 baud;
  149. };
  150. /* Main message type used between library and application */
  151. struct __attribute__ ((packed)) esd_usb2_msg {
  152. union {
  153. struct header_msg hdr;
  154. struct version_msg version;
  155. struct version_reply_msg version_reply;
  156. struct rx_msg rx;
  157. struct tx_msg tx;
  158. struct tx_done_msg txdone;
  159. struct set_baudrate_msg setbaud;
  160. struct id_filter_msg filter;
  161. } msg;
  162. };
  163. static struct usb_device_id esd_usb2_table[] = {
  164. {USB_DEVICE(USB_ESDGMBH_VENDOR_ID, USB_CANUSB2_PRODUCT_ID)},
  165. {USB_DEVICE(USB_ESDGMBH_VENDOR_ID, USB_CANUSBM_PRODUCT_ID)},
  166. {}
  167. };
  168. MODULE_DEVICE_TABLE(usb, esd_usb2_table);
  169. struct esd_usb2_net_priv;
  170. struct esd_tx_urb_context {
  171. struct esd_usb2_net_priv *priv;
  172. u32 echo_index;
  173. int dlc;
  174. };
  175. struct esd_usb2 {
  176. struct usb_device *udev;
  177. struct esd_usb2_net_priv *nets[ESD_USB2_MAX_NETS];
  178. struct usb_anchor rx_submitted;
  179. int net_count;
  180. u32 version;
  181. int rxinitdone;
  182. };
  183. struct esd_usb2_net_priv {
  184. struct can_priv can; /* must be the first member */
  185. atomic_t active_tx_jobs;
  186. struct usb_anchor tx_submitted;
  187. struct esd_tx_urb_context tx_contexts[MAX_TX_URBS];
  188. struct esd_usb2 *usb2;
  189. struct net_device *netdev;
  190. int index;
  191. u8 old_state;
  192. struct can_berr_counter bec;
  193. };
  194. static void esd_usb2_rx_event(struct esd_usb2_net_priv *priv,
  195. struct esd_usb2_msg *msg)
  196. {
  197. struct net_device_stats *stats = &priv->netdev->stats;
  198. struct can_frame *cf;
  199. struct sk_buff *skb;
  200. u32 id = le32_to_cpu(msg->msg.rx.id) & ESD_IDMASK;
  201. if (id == ESD_EV_CAN_ERROR_EXT) {
  202. u8 state = msg->msg.rx.data[0];
  203. u8 ecc = msg->msg.rx.data[1];
  204. u8 txerr = msg->msg.rx.data[2];
  205. u8 rxerr = msg->msg.rx.data[3];
  206. skb = alloc_can_err_skb(priv->netdev, &cf);
  207. if (skb == NULL) {
  208. stats->rx_dropped++;
  209. return;
  210. }
  211. if (state != priv->old_state) {
  212. priv->old_state = state;
  213. switch (state & ESD_BUSSTATE_MASK) {
  214. case ESD_BUSSTATE_BUSOFF:
  215. priv->can.state = CAN_STATE_BUS_OFF;
  216. cf->can_id |= CAN_ERR_BUSOFF;
  217. priv->can.can_stats.bus_off++;
  218. can_bus_off(priv->netdev);
  219. break;
  220. case ESD_BUSSTATE_WARN:
  221. priv->can.state = CAN_STATE_ERROR_WARNING;
  222. priv->can.can_stats.error_warning++;
  223. break;
  224. case ESD_BUSSTATE_ERRPASSIVE:
  225. priv->can.state = CAN_STATE_ERROR_PASSIVE;
  226. priv->can.can_stats.error_passive++;
  227. break;
  228. default:
  229. priv->can.state = CAN_STATE_ERROR_ACTIVE;
  230. break;
  231. }
  232. } else {
  233. priv->can.can_stats.bus_error++;
  234. stats->rx_errors++;
  235. cf->can_id |= CAN_ERR_PROT | CAN_ERR_BUSERROR;
  236. switch (ecc & SJA1000_ECC_MASK) {
  237. case SJA1000_ECC_BIT:
  238. cf->data[2] |= CAN_ERR_PROT_BIT;
  239. break;
  240. case SJA1000_ECC_FORM:
  241. cf->data[2] |= CAN_ERR_PROT_FORM;
  242. break;
  243. case SJA1000_ECC_STUFF:
  244. cf->data[2] |= CAN_ERR_PROT_STUFF;
  245. break;
  246. default:
  247. cf->data[3] = ecc & SJA1000_ECC_SEG;
  248. break;
  249. }
  250. /* Error occurred during transmission? */
  251. if (!(ecc & SJA1000_ECC_DIR))
  252. cf->data[2] |= CAN_ERR_PROT_TX;
  253. if (priv->can.state == CAN_STATE_ERROR_WARNING ||
  254. priv->can.state == CAN_STATE_ERROR_PASSIVE) {
  255. cf->data[1] = (txerr > rxerr) ?
  256. CAN_ERR_CRTL_TX_PASSIVE :
  257. CAN_ERR_CRTL_RX_PASSIVE;
  258. }
  259. cf->data[6] = txerr;
  260. cf->data[7] = rxerr;
  261. }
  262. priv->bec.txerr = txerr;
  263. priv->bec.rxerr = rxerr;
  264. stats->rx_packets++;
  265. stats->rx_bytes += cf->can_dlc;
  266. netif_rx(skb);
  267. }
  268. }
  269. static void esd_usb2_rx_can_msg(struct esd_usb2_net_priv *priv,
  270. struct esd_usb2_msg *msg)
  271. {
  272. struct net_device_stats *stats = &priv->netdev->stats;
  273. struct can_frame *cf;
  274. struct sk_buff *skb;
  275. int i;
  276. u32 id;
  277. if (!netif_device_present(priv->netdev))
  278. return;
  279. id = le32_to_cpu(msg->msg.rx.id);
  280. if (id & ESD_EVENT) {
  281. esd_usb2_rx_event(priv, msg);
  282. } else {
  283. skb = alloc_can_skb(priv->netdev, &cf);
  284. if (skb == NULL) {
  285. stats->rx_dropped++;
  286. return;
  287. }
  288. cf->can_id = id & ESD_IDMASK;
  289. cf->can_dlc = get_can_dlc(msg->msg.rx.dlc & ~ESD_RTR);
  290. if (id & ESD_EXTID)
  291. cf->can_id |= CAN_EFF_FLAG;
  292. if (msg->msg.rx.dlc & ESD_RTR) {
  293. cf->can_id |= CAN_RTR_FLAG;
  294. } else {
  295. for (i = 0; i < cf->can_dlc; i++)
  296. cf->data[i] = msg->msg.rx.data[i];
  297. }
  298. stats->rx_packets++;
  299. stats->rx_bytes += cf->can_dlc;
  300. netif_rx(skb);
  301. }
  302. return;
  303. }
  304. static void esd_usb2_tx_done_msg(struct esd_usb2_net_priv *priv,
  305. struct esd_usb2_msg *msg)
  306. {
  307. struct net_device_stats *stats = &priv->netdev->stats;
  308. struct net_device *netdev = priv->netdev;
  309. struct esd_tx_urb_context *context;
  310. if (!netif_device_present(netdev))
  311. return;
  312. context = &priv->tx_contexts[msg->msg.txdone.hnd & (MAX_TX_URBS - 1)];
  313. if (!msg->msg.txdone.status) {
  314. stats->tx_packets++;
  315. stats->tx_bytes += context->dlc;
  316. can_get_echo_skb(netdev, context->echo_index);
  317. } else {
  318. stats->tx_errors++;
  319. can_free_echo_skb(netdev, context->echo_index);
  320. }
  321. /* Release context */
  322. context->echo_index = MAX_TX_URBS;
  323. atomic_dec(&priv->active_tx_jobs);
  324. netif_wake_queue(netdev);
  325. }
  326. static void esd_usb2_read_bulk_callback(struct urb *urb)
  327. {
  328. struct esd_usb2 *dev = urb->context;
  329. int retval;
  330. int pos = 0;
  331. int i;
  332. switch (urb->status) {
  333. case 0: /* success */
  334. break;
  335. case -ENOENT:
  336. case -EPIPE:
  337. case -EPROTO:
  338. case -ESHUTDOWN:
  339. return;
  340. default:
  341. dev_info(dev->udev->dev.parent,
  342. "Rx URB aborted (%d)\n", urb->status);
  343. goto resubmit_urb;
  344. }
  345. while (pos < urb->actual_length) {
  346. struct esd_usb2_msg *msg;
  347. msg = (struct esd_usb2_msg *)(urb->transfer_buffer + pos);
  348. switch (msg->msg.hdr.cmd) {
  349. case CMD_CAN_RX:
  350. if (msg->msg.rx.net >= dev->net_count) {
  351. dev_err(dev->udev->dev.parent, "format error\n");
  352. break;
  353. }
  354. esd_usb2_rx_can_msg(dev->nets[msg->msg.rx.net], msg);
  355. break;
  356. case CMD_CAN_TX:
  357. if (msg->msg.txdone.net >= dev->net_count) {
  358. dev_err(dev->udev->dev.parent, "format error\n");
  359. break;
  360. }
  361. esd_usb2_tx_done_msg(dev->nets[msg->msg.txdone.net],
  362. msg);
  363. break;
  364. }
  365. pos += msg->msg.hdr.len << 2;
  366. if (pos > urb->actual_length) {
  367. dev_err(dev->udev->dev.parent, "format error\n");
  368. break;
  369. }
  370. }
  371. resubmit_urb:
  372. usb_fill_bulk_urb(urb, dev->udev, usb_rcvbulkpipe(dev->udev, 1),
  373. urb->transfer_buffer, RX_BUFFER_SIZE,
  374. esd_usb2_read_bulk_callback, dev);
  375. retval = usb_submit_urb(urb, GFP_ATOMIC);
  376. if (retval == -ENODEV) {
  377. for (i = 0; i < dev->net_count; i++) {
  378. if (dev->nets[i])
  379. netif_device_detach(dev->nets[i]->netdev);
  380. }
  381. } else if (retval) {
  382. dev_err(dev->udev->dev.parent,
  383. "failed resubmitting read bulk urb: %d\n", retval);
  384. }
  385. return;
  386. }
  387. /*
  388. * callback for bulk IN urb
  389. */
  390. static void esd_usb2_write_bulk_callback(struct urb *urb)
  391. {
  392. struct esd_tx_urb_context *context = urb->context;
  393. struct esd_usb2_net_priv *priv;
  394. struct net_device *netdev;
  395. size_t size = sizeof(struct esd_usb2_msg);
  396. WARN_ON(!context);
  397. priv = context->priv;
  398. netdev = priv->netdev;
  399. /* free up our allocated buffer */
  400. usb_free_coherent(urb->dev, size,
  401. urb->transfer_buffer, urb->transfer_dma);
  402. if (!netif_device_present(netdev))
  403. return;
  404. if (urb->status)
  405. netdev_info(netdev, "Tx URB aborted (%d)\n", urb->status);
  406. netdev->trans_start = jiffies;
  407. }
  408. static ssize_t show_firmware(struct device *d,
  409. struct device_attribute *attr, char *buf)
  410. {
  411. struct usb_interface *intf = to_usb_interface(d);
  412. struct esd_usb2 *dev = usb_get_intfdata(intf);
  413. return sprintf(buf, "%d.%d.%d\n",
  414. (dev->version >> 12) & 0xf,
  415. (dev->version >> 8) & 0xf,
  416. dev->version & 0xff);
  417. }
  418. static DEVICE_ATTR(firmware, S_IRUGO, show_firmware, NULL);
  419. static ssize_t show_hardware(struct device *d,
  420. struct device_attribute *attr, char *buf)
  421. {
  422. struct usb_interface *intf = to_usb_interface(d);
  423. struct esd_usb2 *dev = usb_get_intfdata(intf);
  424. return sprintf(buf, "%d.%d.%d\n",
  425. (dev->version >> 28) & 0xf,
  426. (dev->version >> 24) & 0xf,
  427. (dev->version >> 16) & 0xff);
  428. }
  429. static DEVICE_ATTR(hardware, S_IRUGO, show_hardware, NULL);
  430. static ssize_t show_nets(struct device *d,
  431. struct device_attribute *attr, char *buf)
  432. {
  433. struct usb_interface *intf = to_usb_interface(d);
  434. struct esd_usb2 *dev = usb_get_intfdata(intf);
  435. return sprintf(buf, "%d", dev->net_count);
  436. }
  437. static DEVICE_ATTR(nets, S_IRUGO, show_nets, NULL);
  438. static int esd_usb2_send_msg(struct esd_usb2 *dev, struct esd_usb2_msg *msg)
  439. {
  440. int actual_length;
  441. return usb_bulk_msg(dev->udev,
  442. usb_sndbulkpipe(dev->udev, 2),
  443. msg,
  444. msg->msg.hdr.len << 2,
  445. &actual_length,
  446. 1000);
  447. }
  448. static int esd_usb2_wait_msg(struct esd_usb2 *dev,
  449. struct esd_usb2_msg *msg)
  450. {
  451. int actual_length;
  452. return usb_bulk_msg(dev->udev,
  453. usb_rcvbulkpipe(dev->udev, 1),
  454. msg,
  455. sizeof(*msg),
  456. &actual_length,
  457. 1000);
  458. }
  459. static int esd_usb2_setup_rx_urbs(struct esd_usb2 *dev)
  460. {
  461. int i, err = 0;
  462. if (dev->rxinitdone)
  463. return 0;
  464. for (i = 0; i < MAX_RX_URBS; i++) {
  465. struct urb *urb = NULL;
  466. u8 *buf = NULL;
  467. /* create a URB, and a buffer for it */
  468. urb = usb_alloc_urb(0, GFP_KERNEL);
  469. if (!urb) {
  470. dev_warn(dev->udev->dev.parent,
  471. "No memory left for URBs\n");
  472. err = -ENOMEM;
  473. break;
  474. }
  475. buf = usb_alloc_coherent(dev->udev, RX_BUFFER_SIZE, GFP_KERNEL,
  476. &urb->transfer_dma);
  477. if (!buf) {
  478. dev_warn(dev->udev->dev.parent,
  479. "No memory left for USB buffer\n");
  480. err = -ENOMEM;
  481. goto freeurb;
  482. }
  483. usb_fill_bulk_urb(urb, dev->udev,
  484. usb_rcvbulkpipe(dev->udev, 1),
  485. buf, RX_BUFFER_SIZE,
  486. esd_usb2_read_bulk_callback, dev);
  487. urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
  488. usb_anchor_urb(urb, &dev->rx_submitted);
  489. err = usb_submit_urb(urb, GFP_KERNEL);
  490. if (err) {
  491. usb_unanchor_urb(urb);
  492. usb_free_coherent(dev->udev, RX_BUFFER_SIZE, buf,
  493. urb->transfer_dma);
  494. }
  495. freeurb:
  496. /* Drop reference, USB core will take care of freeing it */
  497. usb_free_urb(urb);
  498. if (err)
  499. break;
  500. }
  501. /* Did we submit any URBs */
  502. if (i == 0) {
  503. dev_err(dev->udev->dev.parent, "couldn't setup read URBs\n");
  504. return err;
  505. }
  506. /* Warn if we've couldn't transmit all the URBs */
  507. if (i < MAX_RX_URBS) {
  508. dev_warn(dev->udev->dev.parent,
  509. "rx performance may be slow\n");
  510. }
  511. dev->rxinitdone = 1;
  512. return 0;
  513. }
  514. /*
  515. * Start interface
  516. */
  517. static int esd_usb2_start(struct esd_usb2_net_priv *priv)
  518. {
  519. struct esd_usb2 *dev = priv->usb2;
  520. struct net_device *netdev = priv->netdev;
  521. struct esd_usb2_msg *msg;
  522. int err, i;
  523. msg = kmalloc(sizeof(*msg), GFP_KERNEL);
  524. if (!msg) {
  525. err = -ENOMEM;
  526. goto out;
  527. }
  528. /*
  529. * Enable all IDs
  530. * The IDADD message takes up to 64 32 bit bitmasks (2048 bits).
  531. * Each bit represents one 11 bit CAN identifier. A set bit
  532. * enables reception of the corresponding CAN identifier. A cleared
  533. * bit disabled this identifier. An additional bitmask value
  534. * following the CAN 2.0A bits is used to enable reception of
  535. * extended CAN frames. Only the LSB of this final mask is checked
  536. * for the complete 29 bit ID range. The IDADD message also allows
  537. * filter configuration for an ID subset. In this case you can add
  538. * the number of the starting bitmask (0..64) to the filter.option
  539. * field followed by only some bitmasks.
  540. */
  541. msg->msg.hdr.cmd = CMD_IDADD;
  542. msg->msg.hdr.len = 2 + ESD_MAX_ID_SEGMENT;
  543. msg->msg.filter.net = priv->index;
  544. msg->msg.filter.option = ESD_ID_ENABLE; /* start with segment 0 */
  545. for (i = 0; i < ESD_MAX_ID_SEGMENT; i++)
  546. msg->msg.filter.mask[i] = cpu_to_le32(0xffffffff);
  547. /* enable 29bit extended IDs */
  548. msg->msg.filter.mask[ESD_MAX_ID_SEGMENT] = cpu_to_le32(0x00000001);
  549. err = esd_usb2_send_msg(dev, msg);
  550. if (err)
  551. goto out;
  552. err = esd_usb2_setup_rx_urbs(dev);
  553. if (err)
  554. goto out;
  555. priv->can.state = CAN_STATE_ERROR_ACTIVE;
  556. out:
  557. if (err == -ENODEV)
  558. netif_device_detach(netdev);
  559. if (err)
  560. netdev_err(netdev, "couldn't start device: %d\n", err);
  561. kfree(msg);
  562. return err;
  563. }
  564. static void unlink_all_urbs(struct esd_usb2 *dev)
  565. {
  566. struct esd_usb2_net_priv *priv;
  567. int i, j;
  568. usb_kill_anchored_urbs(&dev->rx_submitted);
  569. for (i = 0; i < dev->net_count; i++) {
  570. priv = dev->nets[i];
  571. if (priv) {
  572. usb_kill_anchored_urbs(&priv->tx_submitted);
  573. atomic_set(&priv->active_tx_jobs, 0);
  574. for (j = 0; j < MAX_TX_URBS; j++)
  575. priv->tx_contexts[j].echo_index = MAX_TX_URBS;
  576. }
  577. }
  578. }
  579. static int esd_usb2_open(struct net_device *netdev)
  580. {
  581. struct esd_usb2_net_priv *priv = netdev_priv(netdev);
  582. int err;
  583. /* common open */
  584. err = open_candev(netdev);
  585. if (err)
  586. return err;
  587. /* finally start device */
  588. err = esd_usb2_start(priv);
  589. if (err) {
  590. netdev_warn(netdev, "couldn't start device: %d\n", err);
  591. close_candev(netdev);
  592. return err;
  593. }
  594. netif_start_queue(netdev);
  595. return 0;
  596. }
  597. static netdev_tx_t esd_usb2_start_xmit(struct sk_buff *skb,
  598. struct net_device *netdev)
  599. {
  600. struct esd_usb2_net_priv *priv = netdev_priv(netdev);
  601. struct esd_usb2 *dev = priv->usb2;
  602. struct esd_tx_urb_context *context = NULL;
  603. struct net_device_stats *stats = &netdev->stats;
  604. struct can_frame *cf = (struct can_frame *)skb->data;
  605. struct esd_usb2_msg *msg;
  606. struct urb *urb;
  607. u8 *buf;
  608. int i, err;
  609. int ret = NETDEV_TX_OK;
  610. size_t size = sizeof(struct esd_usb2_msg);
  611. if (can_dropped_invalid_skb(netdev, skb))
  612. return NETDEV_TX_OK;
  613. /* create a URB, and a buffer for it, and copy the data to the URB */
  614. urb = usb_alloc_urb(0, GFP_ATOMIC);
  615. if (!urb) {
  616. netdev_err(netdev, "No memory left for URBs\n");
  617. stats->tx_dropped++;
  618. dev_kfree_skb(skb);
  619. goto nourbmem;
  620. }
  621. buf = usb_alloc_coherent(dev->udev, size, GFP_ATOMIC,
  622. &urb->transfer_dma);
  623. if (!buf) {
  624. netdev_err(netdev, "No memory left for USB buffer\n");
  625. stats->tx_dropped++;
  626. dev_kfree_skb(skb);
  627. goto nobufmem;
  628. }
  629. msg = (struct esd_usb2_msg *)buf;
  630. msg->msg.hdr.len = 3; /* minimal length */
  631. msg->msg.hdr.cmd = CMD_CAN_TX;
  632. msg->msg.tx.net = priv->index;
  633. msg->msg.tx.dlc = cf->can_dlc;
  634. msg->msg.tx.id = cpu_to_le32(cf->can_id & CAN_ERR_MASK);
  635. if (cf->can_id & CAN_RTR_FLAG)
  636. msg->msg.tx.dlc |= ESD_RTR;
  637. if (cf->can_id & CAN_EFF_FLAG)
  638. msg->msg.tx.id |= cpu_to_le32(ESD_EXTID);
  639. for (i = 0; i < cf->can_dlc; i++)
  640. msg->msg.tx.data[i] = cf->data[i];
  641. msg->msg.hdr.len += (cf->can_dlc + 3) >> 2;
  642. for (i = 0; i < MAX_TX_URBS; i++) {
  643. if (priv->tx_contexts[i].echo_index == MAX_TX_URBS) {
  644. context = &priv->tx_contexts[i];
  645. break;
  646. }
  647. }
  648. /*
  649. * This may never happen.
  650. */
  651. if (!context) {
  652. netdev_warn(netdev, "couldn't find free context\n");
  653. ret = NETDEV_TX_BUSY;
  654. goto releasebuf;
  655. }
  656. context->priv = priv;
  657. context->echo_index = i;
  658. context->dlc = cf->can_dlc;
  659. /* hnd must not be 0 - MSB is stripped in txdone handling */
  660. msg->msg.tx.hnd = 0x80000000 | i; /* returned in TX done message */
  661. usb_fill_bulk_urb(urb, dev->udev, usb_sndbulkpipe(dev->udev, 2), buf,
  662. msg->msg.hdr.len << 2,
  663. esd_usb2_write_bulk_callback, context);
  664. urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
  665. usb_anchor_urb(urb, &priv->tx_submitted);
  666. can_put_echo_skb(skb, netdev, context->echo_index);
  667. atomic_inc(&priv->active_tx_jobs);
  668. /* Slow down tx path */
  669. if (atomic_read(&priv->active_tx_jobs) >= MAX_TX_URBS)
  670. netif_stop_queue(netdev);
  671. err = usb_submit_urb(urb, GFP_ATOMIC);
  672. if (err) {
  673. can_free_echo_skb(netdev, context->echo_index);
  674. atomic_dec(&priv->active_tx_jobs);
  675. usb_unanchor_urb(urb);
  676. stats->tx_dropped++;
  677. if (err == -ENODEV)
  678. netif_device_detach(netdev);
  679. else
  680. netdev_warn(netdev, "failed tx_urb %d\n", err);
  681. goto releasebuf;
  682. }
  683. netdev->trans_start = jiffies;
  684. /*
  685. * Release our reference to this URB, the USB core will eventually free
  686. * it entirely.
  687. */
  688. usb_free_urb(urb);
  689. return NETDEV_TX_OK;
  690. releasebuf:
  691. usb_free_coherent(dev->udev, size, buf, urb->transfer_dma);
  692. nobufmem:
  693. usb_free_urb(urb);
  694. nourbmem:
  695. return ret;
  696. }
  697. static int esd_usb2_close(struct net_device *netdev)
  698. {
  699. struct esd_usb2_net_priv *priv = netdev_priv(netdev);
  700. struct esd_usb2_msg *msg;
  701. int i;
  702. msg = kmalloc(sizeof(*msg), GFP_KERNEL);
  703. if (!msg)
  704. return -ENOMEM;
  705. /* Disable all IDs (see esd_usb2_start()) */
  706. msg->msg.hdr.cmd = CMD_IDADD;
  707. msg->msg.hdr.len = 2 + ESD_MAX_ID_SEGMENT;
  708. msg->msg.filter.net = priv->index;
  709. msg->msg.filter.option = ESD_ID_ENABLE; /* start with segment 0 */
  710. for (i = 0; i <= ESD_MAX_ID_SEGMENT; i++)
  711. msg->msg.filter.mask[i] = 0;
  712. if (esd_usb2_send_msg(priv->usb2, msg) < 0)
  713. netdev_err(netdev, "sending idadd message failed\n");
  714. /* set CAN controller to reset mode */
  715. msg->msg.hdr.len = 2;
  716. msg->msg.hdr.cmd = CMD_SETBAUD;
  717. msg->msg.setbaud.net = priv->index;
  718. msg->msg.setbaud.rsvd = 0;
  719. msg->msg.setbaud.baud = cpu_to_le32(ESD_USB2_NO_BAUDRATE);
  720. if (esd_usb2_send_msg(priv->usb2, msg) < 0)
  721. netdev_err(netdev, "sending setbaud message failed\n");
  722. priv->can.state = CAN_STATE_STOPPED;
  723. netif_stop_queue(netdev);
  724. close_candev(netdev);
  725. kfree(msg);
  726. return 0;
  727. }
  728. static const struct net_device_ops esd_usb2_netdev_ops = {
  729. .ndo_open = esd_usb2_open,
  730. .ndo_stop = esd_usb2_close,
  731. .ndo_start_xmit = esd_usb2_start_xmit,
  732. .ndo_change_mtu = can_change_mtu,
  733. };
  734. static const struct can_bittiming_const esd_usb2_bittiming_const = {
  735. .name = "esd_usb2",
  736. .tseg1_min = ESD_USB2_TSEG1_MIN,
  737. .tseg1_max = ESD_USB2_TSEG1_MAX,
  738. .tseg2_min = ESD_USB2_TSEG2_MIN,
  739. .tseg2_max = ESD_USB2_TSEG2_MAX,
  740. .sjw_max = ESD_USB2_SJW_MAX,
  741. .brp_min = ESD_USB2_BRP_MIN,
  742. .brp_max = ESD_USB2_BRP_MAX,
  743. .brp_inc = ESD_USB2_BRP_INC,
  744. };
  745. static int esd_usb2_set_bittiming(struct net_device *netdev)
  746. {
  747. struct esd_usb2_net_priv *priv = netdev_priv(netdev);
  748. struct can_bittiming *bt = &priv->can.bittiming;
  749. struct esd_usb2_msg *msg;
  750. int err;
  751. u32 canbtr;
  752. int sjw_shift;
  753. canbtr = ESD_USB2_UBR;
  754. if (priv->can.ctrlmode & CAN_CTRLMODE_LISTENONLY)
  755. canbtr |= ESD_USB2_LOM;
  756. canbtr |= (bt->brp - 1) & (ESD_USB2_BRP_MAX - 1);
  757. if (le16_to_cpu(priv->usb2->udev->descriptor.idProduct) ==
  758. USB_CANUSBM_PRODUCT_ID)
  759. sjw_shift = ESD_USBM_SJW_SHIFT;
  760. else
  761. sjw_shift = ESD_USB2_SJW_SHIFT;
  762. canbtr |= ((bt->sjw - 1) & (ESD_USB2_SJW_MAX - 1))
  763. << sjw_shift;
  764. canbtr |= ((bt->prop_seg + bt->phase_seg1 - 1)
  765. & (ESD_USB2_TSEG1_MAX - 1))
  766. << ESD_USB2_TSEG1_SHIFT;
  767. canbtr |= ((bt->phase_seg2 - 1) & (ESD_USB2_TSEG2_MAX - 1))
  768. << ESD_USB2_TSEG2_SHIFT;
  769. if (priv->can.ctrlmode & CAN_CTRLMODE_3_SAMPLES)
  770. canbtr |= ESD_USB2_3_SAMPLES;
  771. msg = kmalloc(sizeof(*msg), GFP_KERNEL);
  772. if (!msg)
  773. return -ENOMEM;
  774. msg->msg.hdr.len = 2;
  775. msg->msg.hdr.cmd = CMD_SETBAUD;
  776. msg->msg.setbaud.net = priv->index;
  777. msg->msg.setbaud.rsvd = 0;
  778. msg->msg.setbaud.baud = cpu_to_le32(canbtr);
  779. netdev_info(netdev, "setting BTR=%#x\n", canbtr);
  780. err = esd_usb2_send_msg(priv->usb2, msg);
  781. kfree(msg);
  782. return err;
  783. }
  784. static int esd_usb2_get_berr_counter(const struct net_device *netdev,
  785. struct can_berr_counter *bec)
  786. {
  787. struct esd_usb2_net_priv *priv = netdev_priv(netdev);
  788. bec->txerr = priv->bec.txerr;
  789. bec->rxerr = priv->bec.rxerr;
  790. return 0;
  791. }
  792. static int esd_usb2_set_mode(struct net_device *netdev, enum can_mode mode)
  793. {
  794. switch (mode) {
  795. case CAN_MODE_START:
  796. netif_wake_queue(netdev);
  797. break;
  798. default:
  799. return -EOPNOTSUPP;
  800. }
  801. return 0;
  802. }
  803. static int esd_usb2_probe_one_net(struct usb_interface *intf, int index)
  804. {
  805. struct esd_usb2 *dev = usb_get_intfdata(intf);
  806. struct net_device *netdev;
  807. struct esd_usb2_net_priv *priv;
  808. int err = 0;
  809. int i;
  810. netdev = alloc_candev(sizeof(*priv), MAX_TX_URBS);
  811. if (!netdev) {
  812. dev_err(&intf->dev, "couldn't alloc candev\n");
  813. err = -ENOMEM;
  814. goto done;
  815. }
  816. priv = netdev_priv(netdev);
  817. init_usb_anchor(&priv->tx_submitted);
  818. atomic_set(&priv->active_tx_jobs, 0);
  819. for (i = 0; i < MAX_TX_URBS; i++)
  820. priv->tx_contexts[i].echo_index = MAX_TX_URBS;
  821. priv->usb2 = dev;
  822. priv->netdev = netdev;
  823. priv->index = index;
  824. priv->can.state = CAN_STATE_STOPPED;
  825. priv->can.ctrlmode_supported = CAN_CTRLMODE_LISTENONLY;
  826. if (le16_to_cpu(dev->udev->descriptor.idProduct) ==
  827. USB_CANUSBM_PRODUCT_ID)
  828. priv->can.clock.freq = ESD_USBM_CAN_CLOCK;
  829. else {
  830. priv->can.clock.freq = ESD_USB2_CAN_CLOCK;
  831. priv->can.ctrlmode_supported |= CAN_CTRLMODE_3_SAMPLES;
  832. }
  833. priv->can.bittiming_const = &esd_usb2_bittiming_const;
  834. priv->can.do_set_bittiming = esd_usb2_set_bittiming;
  835. priv->can.do_set_mode = esd_usb2_set_mode;
  836. priv->can.do_get_berr_counter = esd_usb2_get_berr_counter;
  837. netdev->flags |= IFF_ECHO; /* we support local echo */
  838. netdev->netdev_ops = &esd_usb2_netdev_ops;
  839. SET_NETDEV_DEV(netdev, &intf->dev);
  840. netdev->dev_id = index;
  841. err = register_candev(netdev);
  842. if (err) {
  843. dev_err(&intf->dev, "couldn't register CAN device: %d\n", err);
  844. free_candev(netdev);
  845. err = -ENOMEM;
  846. goto done;
  847. }
  848. dev->nets[index] = priv;
  849. netdev_info(netdev, "device %s registered\n", netdev->name);
  850. done:
  851. return err;
  852. }
  853. /*
  854. * probe function for new USB2 devices
  855. *
  856. * check version information and number of available
  857. * CAN interfaces
  858. */
  859. static int esd_usb2_probe(struct usb_interface *intf,
  860. const struct usb_device_id *id)
  861. {
  862. struct esd_usb2 *dev;
  863. struct esd_usb2_msg *msg;
  864. int i, err;
  865. dev = kzalloc(sizeof(*dev), GFP_KERNEL);
  866. if (!dev) {
  867. err = -ENOMEM;
  868. goto done;
  869. }
  870. dev->udev = interface_to_usbdev(intf);
  871. init_usb_anchor(&dev->rx_submitted);
  872. usb_set_intfdata(intf, dev);
  873. msg = kmalloc(sizeof(*msg), GFP_KERNEL);
  874. if (!msg) {
  875. err = -ENOMEM;
  876. goto free_msg;
  877. }
  878. /* query number of CAN interfaces (nets) */
  879. msg->msg.hdr.cmd = CMD_VERSION;
  880. msg->msg.hdr.len = 2;
  881. msg->msg.version.rsvd = 0;
  882. msg->msg.version.flags = 0;
  883. msg->msg.version.drv_version = 0;
  884. err = esd_usb2_send_msg(dev, msg);
  885. if (err < 0) {
  886. dev_err(&intf->dev, "sending version message failed\n");
  887. goto free_msg;
  888. }
  889. err = esd_usb2_wait_msg(dev, msg);
  890. if (err < 0) {
  891. dev_err(&intf->dev, "no version message answer\n");
  892. goto free_msg;
  893. }
  894. dev->net_count = (int)msg->msg.version_reply.nets;
  895. dev->version = le32_to_cpu(msg->msg.version_reply.version);
  896. if (device_create_file(&intf->dev, &dev_attr_firmware))
  897. dev_err(&intf->dev,
  898. "Couldn't create device file for firmware\n");
  899. if (device_create_file(&intf->dev, &dev_attr_hardware))
  900. dev_err(&intf->dev,
  901. "Couldn't create device file for hardware\n");
  902. if (device_create_file(&intf->dev, &dev_attr_nets))
  903. dev_err(&intf->dev,
  904. "Couldn't create device file for nets\n");
  905. /* do per device probing */
  906. for (i = 0; i < dev->net_count; i++)
  907. esd_usb2_probe_one_net(intf, i);
  908. free_msg:
  909. kfree(msg);
  910. if (err)
  911. kfree(dev);
  912. done:
  913. return err;
  914. }
  915. /*
  916. * called by the usb core when the device is removed from the system
  917. */
  918. static void esd_usb2_disconnect(struct usb_interface *intf)
  919. {
  920. struct esd_usb2 *dev = usb_get_intfdata(intf);
  921. struct net_device *netdev;
  922. int i;
  923. device_remove_file(&intf->dev, &dev_attr_firmware);
  924. device_remove_file(&intf->dev, &dev_attr_hardware);
  925. device_remove_file(&intf->dev, &dev_attr_nets);
  926. usb_set_intfdata(intf, NULL);
  927. if (dev) {
  928. for (i = 0; i < dev->net_count; i++) {
  929. if (dev->nets[i]) {
  930. netdev = dev->nets[i]->netdev;
  931. unregister_netdev(netdev);
  932. free_candev(netdev);
  933. }
  934. }
  935. unlink_all_urbs(dev);
  936. kfree(dev);
  937. }
  938. }
  939. /* usb specific object needed to register this driver with the usb subsystem */
  940. static struct usb_driver esd_usb2_driver = {
  941. .name = "esd_usb2",
  942. .probe = esd_usb2_probe,
  943. .disconnect = esd_usb2_disconnect,
  944. .id_table = esd_usb2_table,
  945. };
  946. module_usb_driver(esd_usb2_driver);