esd_usb2.c 26 KB

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