esd_usb2.c 25 KB

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