esd_usb2.c 26 KB

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