esd_usb2.c 26 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151
  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);
  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 -ESHUTDOWN:
  337. return;
  338. default:
  339. dev_info(dev->udev->dev.parent,
  340. "Rx URB aborted (%d)\n", urb->status);
  341. goto resubmit_urb;
  342. }
  343. while (pos < urb->actual_length) {
  344. struct esd_usb2_msg *msg;
  345. msg = (struct esd_usb2_msg *)(urb->transfer_buffer + pos);
  346. switch (msg->msg.hdr.cmd) {
  347. case CMD_CAN_RX:
  348. if (msg->msg.rx.net >= dev->net_count) {
  349. dev_err(dev->udev->dev.parent, "format error\n");
  350. break;
  351. }
  352. esd_usb2_rx_can_msg(dev->nets[msg->msg.rx.net], msg);
  353. break;
  354. case CMD_CAN_TX:
  355. if (msg->msg.txdone.net >= dev->net_count) {
  356. dev_err(dev->udev->dev.parent, "format error\n");
  357. break;
  358. }
  359. esd_usb2_tx_done_msg(dev->nets[msg->msg.txdone.net],
  360. msg);
  361. break;
  362. }
  363. pos += msg->msg.hdr.len << 2;
  364. if (pos > urb->actual_length) {
  365. dev_err(dev->udev->dev.parent, "format error\n");
  366. break;
  367. }
  368. }
  369. resubmit_urb:
  370. usb_fill_bulk_urb(urb, dev->udev, usb_rcvbulkpipe(dev->udev, 1),
  371. urb->transfer_buffer, RX_BUFFER_SIZE,
  372. esd_usb2_read_bulk_callback, dev);
  373. retval = usb_submit_urb(urb, GFP_ATOMIC);
  374. if (retval == -ENODEV) {
  375. for (i = 0; i < dev->net_count; i++) {
  376. if (dev->nets[i])
  377. netif_device_detach(dev->nets[i]->netdev);
  378. }
  379. } else if (retval) {
  380. dev_err(dev->udev->dev.parent,
  381. "failed resubmitting read bulk urb: %d\n", retval);
  382. }
  383. return;
  384. }
  385. /*
  386. * callback for bulk IN urb
  387. */
  388. static void esd_usb2_write_bulk_callback(struct urb *urb)
  389. {
  390. struct esd_tx_urb_context *context = urb->context;
  391. struct esd_usb2_net_priv *priv;
  392. struct net_device *netdev;
  393. size_t size = sizeof(struct esd_usb2_msg);
  394. WARN_ON(!context);
  395. priv = context->priv;
  396. netdev = priv->netdev;
  397. /* free up our allocated buffer */
  398. usb_free_coherent(urb->dev, size,
  399. urb->transfer_buffer, urb->transfer_dma);
  400. if (!netif_device_present(netdev))
  401. return;
  402. if (urb->status)
  403. netdev_info(netdev, "Tx URB aborted (%d)\n", urb->status);
  404. netif_trans_update(netdev);
  405. }
  406. static ssize_t show_firmware(struct device *d,
  407. struct device_attribute *attr, char *buf)
  408. {
  409. struct usb_interface *intf = to_usb_interface(d);
  410. struct esd_usb2 *dev = usb_get_intfdata(intf);
  411. return sprintf(buf, "%d.%d.%d\n",
  412. (dev->version >> 12) & 0xf,
  413. (dev->version >> 8) & 0xf,
  414. dev->version & 0xff);
  415. }
  416. static DEVICE_ATTR(firmware, S_IRUGO, show_firmware, NULL);
  417. static ssize_t show_hardware(struct device *d,
  418. struct device_attribute *attr, char *buf)
  419. {
  420. struct usb_interface *intf = to_usb_interface(d);
  421. struct esd_usb2 *dev = usb_get_intfdata(intf);
  422. return sprintf(buf, "%d.%d.%d\n",
  423. (dev->version >> 28) & 0xf,
  424. (dev->version >> 24) & 0xf,
  425. (dev->version >> 16) & 0xff);
  426. }
  427. static DEVICE_ATTR(hardware, S_IRUGO, show_hardware, NULL);
  428. static ssize_t show_nets(struct device *d,
  429. struct device_attribute *attr, char *buf)
  430. {
  431. struct usb_interface *intf = to_usb_interface(d);
  432. struct esd_usb2 *dev = usb_get_intfdata(intf);
  433. return sprintf(buf, "%d", dev->net_count);
  434. }
  435. static DEVICE_ATTR(nets, S_IRUGO, show_nets, NULL);
  436. static int esd_usb2_send_msg(struct esd_usb2 *dev, struct esd_usb2_msg *msg)
  437. {
  438. int actual_length;
  439. return usb_bulk_msg(dev->udev,
  440. usb_sndbulkpipe(dev->udev, 2),
  441. msg,
  442. msg->msg.hdr.len << 2,
  443. &actual_length,
  444. 1000);
  445. }
  446. static int esd_usb2_wait_msg(struct esd_usb2 *dev,
  447. struct esd_usb2_msg *msg)
  448. {
  449. int actual_length;
  450. return usb_bulk_msg(dev->udev,
  451. usb_rcvbulkpipe(dev->udev, 1),
  452. msg,
  453. sizeof(*msg),
  454. &actual_length,
  455. 1000);
  456. }
  457. static int esd_usb2_setup_rx_urbs(struct esd_usb2 *dev)
  458. {
  459. int i, err = 0;
  460. if (dev->rxinitdone)
  461. return 0;
  462. for (i = 0; i < MAX_RX_URBS; i++) {
  463. struct urb *urb = NULL;
  464. u8 *buf = NULL;
  465. /* create a URB, and a buffer for it */
  466. urb = usb_alloc_urb(0, GFP_KERNEL);
  467. if (!urb) {
  468. err = -ENOMEM;
  469. break;
  470. }
  471. buf = usb_alloc_coherent(dev->udev, RX_BUFFER_SIZE, GFP_KERNEL,
  472. &urb->transfer_dma);
  473. if (!buf) {
  474. dev_warn(dev->udev->dev.parent,
  475. "No memory left for USB buffer\n");
  476. err = -ENOMEM;
  477. goto freeurb;
  478. }
  479. usb_fill_bulk_urb(urb, dev->udev,
  480. usb_rcvbulkpipe(dev->udev, 1),
  481. buf, RX_BUFFER_SIZE,
  482. esd_usb2_read_bulk_callback, dev);
  483. urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
  484. usb_anchor_urb(urb, &dev->rx_submitted);
  485. err = usb_submit_urb(urb, GFP_KERNEL);
  486. if (err) {
  487. usb_unanchor_urb(urb);
  488. usb_free_coherent(dev->udev, RX_BUFFER_SIZE, buf,
  489. urb->transfer_dma);
  490. }
  491. freeurb:
  492. /* Drop reference, USB core will take care of freeing it */
  493. usb_free_urb(urb);
  494. if (err)
  495. break;
  496. }
  497. /* Did we submit any URBs */
  498. if (i == 0) {
  499. dev_err(dev->udev->dev.parent, "couldn't setup read URBs\n");
  500. return err;
  501. }
  502. /* Warn if we've couldn't transmit all the URBs */
  503. if (i < MAX_RX_URBS) {
  504. dev_warn(dev->udev->dev.parent,
  505. "rx performance may be slow\n");
  506. }
  507. dev->rxinitdone = 1;
  508. return 0;
  509. }
  510. /*
  511. * Start interface
  512. */
  513. static int esd_usb2_start(struct esd_usb2_net_priv *priv)
  514. {
  515. struct esd_usb2 *dev = priv->usb2;
  516. struct net_device *netdev = priv->netdev;
  517. struct esd_usb2_msg *msg;
  518. int err, i;
  519. msg = kmalloc(sizeof(*msg), GFP_KERNEL);
  520. if (!msg) {
  521. err = -ENOMEM;
  522. goto out;
  523. }
  524. /*
  525. * Enable all IDs
  526. * The IDADD message takes up to 64 32 bit bitmasks (2048 bits).
  527. * Each bit represents one 11 bit CAN identifier. A set bit
  528. * enables reception of the corresponding CAN identifier. A cleared
  529. * bit disabled this identifier. An additional bitmask value
  530. * following the CAN 2.0A bits is used to enable reception of
  531. * extended CAN frames. Only the LSB of this final mask is checked
  532. * for the complete 29 bit ID range. The IDADD message also allows
  533. * filter configuration for an ID subset. In this case you can add
  534. * the number of the starting bitmask (0..64) to the filter.option
  535. * field followed by only some bitmasks.
  536. */
  537. msg->msg.hdr.cmd = CMD_IDADD;
  538. msg->msg.hdr.len = 2 + ESD_MAX_ID_SEGMENT;
  539. msg->msg.filter.net = priv->index;
  540. msg->msg.filter.option = ESD_ID_ENABLE; /* start with segment 0 */
  541. for (i = 0; i < ESD_MAX_ID_SEGMENT; i++)
  542. msg->msg.filter.mask[i] = cpu_to_le32(0xffffffff);
  543. /* enable 29bit extended IDs */
  544. msg->msg.filter.mask[ESD_MAX_ID_SEGMENT] = cpu_to_le32(0x00000001);
  545. err = esd_usb2_send_msg(dev, msg);
  546. if (err)
  547. goto out;
  548. err = esd_usb2_setup_rx_urbs(dev);
  549. if (err)
  550. goto out;
  551. priv->can.state = CAN_STATE_ERROR_ACTIVE;
  552. out:
  553. if (err == -ENODEV)
  554. netif_device_detach(netdev);
  555. if (err)
  556. netdev_err(netdev, "couldn't start device: %d\n", err);
  557. kfree(msg);
  558. return err;
  559. }
  560. static void unlink_all_urbs(struct esd_usb2 *dev)
  561. {
  562. struct esd_usb2_net_priv *priv;
  563. int i, j;
  564. usb_kill_anchored_urbs(&dev->rx_submitted);
  565. for (i = 0; i < dev->net_count; i++) {
  566. priv = dev->nets[i];
  567. if (priv) {
  568. usb_kill_anchored_urbs(&priv->tx_submitted);
  569. atomic_set(&priv->active_tx_jobs, 0);
  570. for (j = 0; j < MAX_TX_URBS; j++)
  571. priv->tx_contexts[j].echo_index = MAX_TX_URBS;
  572. }
  573. }
  574. }
  575. static int esd_usb2_open(struct net_device *netdev)
  576. {
  577. struct esd_usb2_net_priv *priv = netdev_priv(netdev);
  578. int err;
  579. /* common open */
  580. err = open_candev(netdev);
  581. if (err)
  582. return err;
  583. /* finally start device */
  584. err = esd_usb2_start(priv);
  585. if (err) {
  586. netdev_warn(netdev, "couldn't start device: %d\n", err);
  587. close_candev(netdev);
  588. return err;
  589. }
  590. netif_start_queue(netdev);
  591. return 0;
  592. }
  593. static netdev_tx_t esd_usb2_start_xmit(struct sk_buff *skb,
  594. struct net_device *netdev)
  595. {
  596. struct esd_usb2_net_priv *priv = netdev_priv(netdev);
  597. struct esd_usb2 *dev = priv->usb2;
  598. struct esd_tx_urb_context *context = NULL;
  599. struct net_device_stats *stats = &netdev->stats;
  600. struct can_frame *cf = (struct can_frame *)skb->data;
  601. struct esd_usb2_msg *msg;
  602. struct urb *urb;
  603. u8 *buf;
  604. int i, err;
  605. int ret = NETDEV_TX_OK;
  606. size_t size = sizeof(struct esd_usb2_msg);
  607. if (can_dropped_invalid_skb(netdev, skb))
  608. return NETDEV_TX_OK;
  609. /* create a URB, and a buffer for it, and copy the data to the URB */
  610. urb = usb_alloc_urb(0, GFP_ATOMIC);
  611. if (!urb) {
  612. stats->tx_dropped++;
  613. dev_kfree_skb(skb);
  614. goto nourbmem;
  615. }
  616. buf = usb_alloc_coherent(dev->udev, size, GFP_ATOMIC,
  617. &urb->transfer_dma);
  618. if (!buf) {
  619. netdev_err(netdev, "No memory left for USB buffer\n");
  620. stats->tx_dropped++;
  621. dev_kfree_skb(skb);
  622. goto nobufmem;
  623. }
  624. msg = (struct esd_usb2_msg *)buf;
  625. msg->msg.hdr.len = 3; /* minimal length */
  626. msg->msg.hdr.cmd = CMD_CAN_TX;
  627. msg->msg.tx.net = priv->index;
  628. msg->msg.tx.dlc = cf->can_dlc;
  629. msg->msg.tx.id = cpu_to_le32(cf->can_id & CAN_ERR_MASK);
  630. if (cf->can_id & CAN_RTR_FLAG)
  631. msg->msg.tx.dlc |= ESD_RTR;
  632. if (cf->can_id & CAN_EFF_FLAG)
  633. msg->msg.tx.id |= cpu_to_le32(ESD_EXTID);
  634. for (i = 0; i < cf->can_dlc; i++)
  635. msg->msg.tx.data[i] = cf->data[i];
  636. msg->msg.hdr.len += (cf->can_dlc + 3) >> 2;
  637. for (i = 0; i < MAX_TX_URBS; i++) {
  638. if (priv->tx_contexts[i].echo_index == MAX_TX_URBS) {
  639. context = &priv->tx_contexts[i];
  640. break;
  641. }
  642. }
  643. /*
  644. * This may never happen.
  645. */
  646. if (!context) {
  647. netdev_warn(netdev, "couldn't find free context\n");
  648. ret = NETDEV_TX_BUSY;
  649. goto releasebuf;
  650. }
  651. context->priv = priv;
  652. context->echo_index = i;
  653. context->dlc = cf->can_dlc;
  654. /* hnd must not be 0 - MSB is stripped in txdone handling */
  655. msg->msg.tx.hnd = 0x80000000 | i; /* returned in TX done message */
  656. usb_fill_bulk_urb(urb, dev->udev, usb_sndbulkpipe(dev->udev, 2), buf,
  657. msg->msg.hdr.len << 2,
  658. esd_usb2_write_bulk_callback, context);
  659. urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
  660. usb_anchor_urb(urb, &priv->tx_submitted);
  661. can_put_echo_skb(skb, netdev, context->echo_index);
  662. atomic_inc(&priv->active_tx_jobs);
  663. /* Slow down tx path */
  664. if (atomic_read(&priv->active_tx_jobs) >= MAX_TX_URBS)
  665. netif_stop_queue(netdev);
  666. err = usb_submit_urb(urb, GFP_ATOMIC);
  667. if (err) {
  668. can_free_echo_skb(netdev, context->echo_index);
  669. atomic_dec(&priv->active_tx_jobs);
  670. usb_unanchor_urb(urb);
  671. stats->tx_dropped++;
  672. if (err == -ENODEV)
  673. netif_device_detach(netdev);
  674. else
  675. netdev_warn(netdev, "failed tx_urb %d\n", err);
  676. goto releasebuf;
  677. }
  678. netif_trans_update(netdev);
  679. /*
  680. * Release our reference to this URB, the USB core will eventually free
  681. * it entirely.
  682. */
  683. usb_free_urb(urb);
  684. return NETDEV_TX_OK;
  685. releasebuf:
  686. usb_free_coherent(dev->udev, size, buf, urb->transfer_dma);
  687. nobufmem:
  688. usb_free_urb(urb);
  689. nourbmem:
  690. return ret;
  691. }
  692. static int esd_usb2_close(struct net_device *netdev)
  693. {
  694. struct esd_usb2_net_priv *priv = netdev_priv(netdev);
  695. struct esd_usb2_msg *msg;
  696. int i;
  697. msg = kmalloc(sizeof(*msg), GFP_KERNEL);
  698. if (!msg)
  699. return -ENOMEM;
  700. /* Disable all IDs (see esd_usb2_start()) */
  701. msg->msg.hdr.cmd = CMD_IDADD;
  702. msg->msg.hdr.len = 2 + ESD_MAX_ID_SEGMENT;
  703. msg->msg.filter.net = priv->index;
  704. msg->msg.filter.option = ESD_ID_ENABLE; /* start with segment 0 */
  705. for (i = 0; i <= ESD_MAX_ID_SEGMENT; i++)
  706. msg->msg.filter.mask[i] = 0;
  707. if (esd_usb2_send_msg(priv->usb2, msg) < 0)
  708. netdev_err(netdev, "sending idadd message failed\n");
  709. /* set CAN controller to reset mode */
  710. msg->msg.hdr.len = 2;
  711. msg->msg.hdr.cmd = CMD_SETBAUD;
  712. msg->msg.setbaud.net = priv->index;
  713. msg->msg.setbaud.rsvd = 0;
  714. msg->msg.setbaud.baud = cpu_to_le32(ESD_USB2_NO_BAUDRATE);
  715. if (esd_usb2_send_msg(priv->usb2, msg) < 0)
  716. netdev_err(netdev, "sending setbaud message failed\n");
  717. priv->can.state = CAN_STATE_STOPPED;
  718. netif_stop_queue(netdev);
  719. close_candev(netdev);
  720. kfree(msg);
  721. return 0;
  722. }
  723. static const struct net_device_ops esd_usb2_netdev_ops = {
  724. .ndo_open = esd_usb2_open,
  725. .ndo_stop = esd_usb2_close,
  726. .ndo_start_xmit = esd_usb2_start_xmit,
  727. .ndo_change_mtu = can_change_mtu,
  728. };
  729. static const struct can_bittiming_const esd_usb2_bittiming_const = {
  730. .name = "esd_usb2",
  731. .tseg1_min = ESD_USB2_TSEG1_MIN,
  732. .tseg1_max = ESD_USB2_TSEG1_MAX,
  733. .tseg2_min = ESD_USB2_TSEG2_MIN,
  734. .tseg2_max = ESD_USB2_TSEG2_MAX,
  735. .sjw_max = ESD_USB2_SJW_MAX,
  736. .brp_min = ESD_USB2_BRP_MIN,
  737. .brp_max = ESD_USB2_BRP_MAX,
  738. .brp_inc = ESD_USB2_BRP_INC,
  739. };
  740. static int esd_usb2_set_bittiming(struct net_device *netdev)
  741. {
  742. struct esd_usb2_net_priv *priv = netdev_priv(netdev);
  743. struct can_bittiming *bt = &priv->can.bittiming;
  744. struct esd_usb2_msg *msg;
  745. int err;
  746. u32 canbtr;
  747. int sjw_shift;
  748. canbtr = ESD_USB2_UBR;
  749. if (priv->can.ctrlmode & CAN_CTRLMODE_LISTENONLY)
  750. canbtr |= ESD_USB2_LOM;
  751. canbtr |= (bt->brp - 1) & (ESD_USB2_BRP_MAX - 1);
  752. if (le16_to_cpu(priv->usb2->udev->descriptor.idProduct) ==
  753. USB_CANUSBM_PRODUCT_ID)
  754. sjw_shift = ESD_USBM_SJW_SHIFT;
  755. else
  756. sjw_shift = ESD_USB2_SJW_SHIFT;
  757. canbtr |= ((bt->sjw - 1) & (ESD_USB2_SJW_MAX - 1))
  758. << sjw_shift;
  759. canbtr |= ((bt->prop_seg + bt->phase_seg1 - 1)
  760. & (ESD_USB2_TSEG1_MAX - 1))
  761. << ESD_USB2_TSEG1_SHIFT;
  762. canbtr |= ((bt->phase_seg2 - 1) & (ESD_USB2_TSEG2_MAX - 1))
  763. << ESD_USB2_TSEG2_SHIFT;
  764. if (priv->can.ctrlmode & CAN_CTRLMODE_3_SAMPLES)
  765. canbtr |= ESD_USB2_3_SAMPLES;
  766. msg = kmalloc(sizeof(*msg), GFP_KERNEL);
  767. if (!msg)
  768. return -ENOMEM;
  769. msg->msg.hdr.len = 2;
  770. msg->msg.hdr.cmd = CMD_SETBAUD;
  771. msg->msg.setbaud.net = priv->index;
  772. msg->msg.setbaud.rsvd = 0;
  773. msg->msg.setbaud.baud = cpu_to_le32(canbtr);
  774. netdev_info(netdev, "setting BTR=%#x\n", canbtr);
  775. err = esd_usb2_send_msg(priv->usb2, msg);
  776. kfree(msg);
  777. return err;
  778. }
  779. static int esd_usb2_get_berr_counter(const struct net_device *netdev,
  780. struct can_berr_counter *bec)
  781. {
  782. struct esd_usb2_net_priv *priv = netdev_priv(netdev);
  783. bec->txerr = priv->bec.txerr;
  784. bec->rxerr = priv->bec.rxerr;
  785. return 0;
  786. }
  787. static int esd_usb2_set_mode(struct net_device *netdev, enum can_mode mode)
  788. {
  789. switch (mode) {
  790. case CAN_MODE_START:
  791. netif_wake_queue(netdev);
  792. break;
  793. default:
  794. return -EOPNOTSUPP;
  795. }
  796. return 0;
  797. }
  798. static int esd_usb2_probe_one_net(struct usb_interface *intf, int index)
  799. {
  800. struct esd_usb2 *dev = usb_get_intfdata(intf);
  801. struct net_device *netdev;
  802. struct esd_usb2_net_priv *priv;
  803. int err = 0;
  804. int i;
  805. netdev = alloc_candev(sizeof(*priv), MAX_TX_URBS);
  806. if (!netdev) {
  807. dev_err(&intf->dev, "couldn't alloc candev\n");
  808. err = -ENOMEM;
  809. goto done;
  810. }
  811. priv = netdev_priv(netdev);
  812. init_usb_anchor(&priv->tx_submitted);
  813. atomic_set(&priv->active_tx_jobs, 0);
  814. for (i = 0; i < MAX_TX_URBS; i++)
  815. priv->tx_contexts[i].echo_index = MAX_TX_URBS;
  816. priv->usb2 = dev;
  817. priv->netdev = netdev;
  818. priv->index = index;
  819. priv->can.state = CAN_STATE_STOPPED;
  820. priv->can.ctrlmode_supported = CAN_CTRLMODE_LISTENONLY;
  821. if (le16_to_cpu(dev->udev->descriptor.idProduct) ==
  822. USB_CANUSBM_PRODUCT_ID)
  823. priv->can.clock.freq = ESD_USBM_CAN_CLOCK;
  824. else {
  825. priv->can.clock.freq = ESD_USB2_CAN_CLOCK;
  826. priv->can.ctrlmode_supported |= CAN_CTRLMODE_3_SAMPLES;
  827. }
  828. priv->can.bittiming_const = &esd_usb2_bittiming_const;
  829. priv->can.do_set_bittiming = esd_usb2_set_bittiming;
  830. priv->can.do_set_mode = esd_usb2_set_mode;
  831. priv->can.do_get_berr_counter = esd_usb2_get_berr_counter;
  832. netdev->flags |= IFF_ECHO; /* we support local echo */
  833. netdev->netdev_ops = &esd_usb2_netdev_ops;
  834. SET_NETDEV_DEV(netdev, &intf->dev);
  835. netdev->dev_id = index;
  836. err = register_candev(netdev);
  837. if (err) {
  838. dev_err(&intf->dev, "couldn't register CAN device: %d\n", err);
  839. free_candev(netdev);
  840. err = -ENOMEM;
  841. goto done;
  842. }
  843. dev->nets[index] = priv;
  844. netdev_info(netdev, "device %s registered\n", netdev->name);
  845. done:
  846. return err;
  847. }
  848. /*
  849. * probe function for new USB2 devices
  850. *
  851. * check version information and number of available
  852. * CAN interfaces
  853. */
  854. static int esd_usb2_probe(struct usb_interface *intf,
  855. const struct usb_device_id *id)
  856. {
  857. struct esd_usb2 *dev;
  858. struct esd_usb2_msg *msg;
  859. int i, err;
  860. dev = kzalloc(sizeof(*dev), GFP_KERNEL);
  861. if (!dev) {
  862. err = -ENOMEM;
  863. goto done;
  864. }
  865. dev->udev = interface_to_usbdev(intf);
  866. init_usb_anchor(&dev->rx_submitted);
  867. usb_set_intfdata(intf, dev);
  868. msg = kmalloc(sizeof(*msg), GFP_KERNEL);
  869. if (!msg) {
  870. err = -ENOMEM;
  871. goto free_msg;
  872. }
  873. /* query number of CAN interfaces (nets) */
  874. msg->msg.hdr.cmd = CMD_VERSION;
  875. msg->msg.hdr.len = 2;
  876. msg->msg.version.rsvd = 0;
  877. msg->msg.version.flags = 0;
  878. msg->msg.version.drv_version = 0;
  879. err = esd_usb2_send_msg(dev, msg);
  880. if (err < 0) {
  881. dev_err(&intf->dev, "sending version message failed\n");
  882. goto free_msg;
  883. }
  884. err = esd_usb2_wait_msg(dev, msg);
  885. if (err < 0) {
  886. dev_err(&intf->dev, "no version message answer\n");
  887. goto free_msg;
  888. }
  889. dev->net_count = (int)msg->msg.version_reply.nets;
  890. dev->version = le32_to_cpu(msg->msg.version_reply.version);
  891. if (device_create_file(&intf->dev, &dev_attr_firmware))
  892. dev_err(&intf->dev,
  893. "Couldn't create device file for firmware\n");
  894. if (device_create_file(&intf->dev, &dev_attr_hardware))
  895. dev_err(&intf->dev,
  896. "Couldn't create device file for hardware\n");
  897. if (device_create_file(&intf->dev, &dev_attr_nets))
  898. dev_err(&intf->dev,
  899. "Couldn't create device file for nets\n");
  900. /* do per device probing */
  901. for (i = 0; i < dev->net_count; i++)
  902. esd_usb2_probe_one_net(intf, i);
  903. free_msg:
  904. kfree(msg);
  905. if (err)
  906. kfree(dev);
  907. done:
  908. return err;
  909. }
  910. /*
  911. * called by the usb core when the device is removed from the system
  912. */
  913. static void esd_usb2_disconnect(struct usb_interface *intf)
  914. {
  915. struct esd_usb2 *dev = usb_get_intfdata(intf);
  916. struct net_device *netdev;
  917. int i;
  918. device_remove_file(&intf->dev, &dev_attr_firmware);
  919. device_remove_file(&intf->dev, &dev_attr_hardware);
  920. device_remove_file(&intf->dev, &dev_attr_nets);
  921. usb_set_intfdata(intf, NULL);
  922. if (dev) {
  923. for (i = 0; i < dev->net_count; i++) {
  924. if (dev->nets[i]) {
  925. netdev = dev->nets[i]->netdev;
  926. unregister_netdev(netdev);
  927. free_candev(netdev);
  928. }
  929. }
  930. unlink_all_urbs(dev);
  931. kfree(dev);
  932. }
  933. }
  934. /* usb specific object needed to register this driver with the usb subsystem */
  935. static struct usb_driver esd_usb2_driver = {
  936. .name = "esd_usb2",
  937. .probe = esd_usb2_probe,
  938. .disconnect = esd_usb2_disconnect,
  939. .id_table = esd_usb2_table,
  940. };
  941. module_usb_driver(esd_usb2_driver);