ems_usb.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * CAN driver for EMS Dr. Thomas Wuensche CPC-USB/ARM7
  4. *
  5. * Copyright (C) 2004-2009 EMS Dr. Thomas Wuensche
  6. */
  7. #include <linux/signal.h>
  8. #include <linux/slab.h>
  9. #include <linux/module.h>
  10. #include <linux/netdevice.h>
  11. #include <linux/usb.h>
  12. #include <linux/can.h>
  13. #include <linux/can/dev.h>
  14. #include <linux/can/error.h>
  15. MODULE_AUTHOR("Sebastian Haas <haas@ems-wuensche.com>");
  16. MODULE_DESCRIPTION("CAN driver for EMS Dr. Thomas Wuensche CAN/USB interfaces");
  17. MODULE_LICENSE("GPL v2");
  18. /* Control-Values for CPC_Control() Command Subject Selection */
  19. #define CONTR_CAN_MESSAGE 0x04
  20. #define CONTR_CAN_STATE 0x0C
  21. #define CONTR_BUS_ERROR 0x1C
  22. /* Control Command Actions */
  23. #define CONTR_CONT_OFF 0
  24. #define CONTR_CONT_ON 1
  25. #define CONTR_ONCE 2
  26. /* Messages from CPC to PC */
  27. #define CPC_MSG_TYPE_CAN_FRAME 1 /* CAN data frame */
  28. #define CPC_MSG_TYPE_RTR_FRAME 8 /* CAN remote frame */
  29. #define CPC_MSG_TYPE_CAN_PARAMS 12 /* Actual CAN parameters */
  30. #define CPC_MSG_TYPE_CAN_STATE 14 /* CAN state message */
  31. #define CPC_MSG_TYPE_EXT_CAN_FRAME 16 /* Extended CAN data frame */
  32. #define CPC_MSG_TYPE_EXT_RTR_FRAME 17 /* Extended remote frame */
  33. #define CPC_MSG_TYPE_CONTROL 19 /* change interface behavior */
  34. #define CPC_MSG_TYPE_CONFIRM 20 /* command processed confirmation */
  35. #define CPC_MSG_TYPE_OVERRUN 21 /* overrun events */
  36. #define CPC_MSG_TYPE_CAN_FRAME_ERROR 23 /* detected bus errors */
  37. #define CPC_MSG_TYPE_ERR_COUNTER 25 /* RX/TX error counter */
  38. /* Messages from the PC to the CPC interface */
  39. #define CPC_CMD_TYPE_CAN_FRAME 1 /* CAN data frame */
  40. #define CPC_CMD_TYPE_CONTROL 3 /* control of interface behavior */
  41. #define CPC_CMD_TYPE_CAN_PARAMS 6 /* set CAN parameters */
  42. #define CPC_CMD_TYPE_RTR_FRAME 13 /* CAN remote frame */
  43. #define CPC_CMD_TYPE_CAN_STATE 14 /* CAN state message */
  44. #define CPC_CMD_TYPE_EXT_CAN_FRAME 15 /* Extended CAN data frame */
  45. #define CPC_CMD_TYPE_EXT_RTR_FRAME 16 /* Extended CAN remote frame */
  46. #define CPC_CMD_TYPE_CAN_EXIT 200 /* exit the CAN */
  47. #define CPC_CMD_TYPE_INQ_ERR_COUNTER 25 /* request the CAN error counters */
  48. #define CPC_CMD_TYPE_CLEAR_MSG_QUEUE 8 /* clear CPC_MSG queue */
  49. #define CPC_CMD_TYPE_CLEAR_CMD_QUEUE 28 /* clear CPC_CMD queue */
  50. #define CPC_CC_TYPE_SJA1000 2 /* Philips basic CAN controller */
  51. #define CPC_CAN_ECODE_ERRFRAME 0x01 /* Ecode type */
  52. /* Overrun types */
  53. #define CPC_OVR_EVENT_CAN 0x01
  54. #define CPC_OVR_EVENT_CANSTATE 0x02
  55. #define CPC_OVR_EVENT_BUSERROR 0x04
  56. /*
  57. * If the CAN controller lost a message we indicate it with the highest bit
  58. * set in the count field.
  59. */
  60. #define CPC_OVR_HW 0x80
  61. /* Size of the "struct ems_cpc_msg" without the union */
  62. #define CPC_MSG_HEADER_LEN 11
  63. #define CPC_CAN_MSG_MIN_SIZE 5
  64. /* Define these values to match your devices */
  65. #define USB_CPCUSB_VENDOR_ID 0x12D6
  66. #define USB_CPCUSB_ARM7_PRODUCT_ID 0x0444
  67. /* Mode register NXP LPC2119/SJA1000 CAN Controller */
  68. #define SJA1000_MOD_NORMAL 0x00
  69. #define SJA1000_MOD_RM 0x01
  70. /* ECC register NXP LPC2119/SJA1000 CAN Controller */
  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. /* Status register content */
  79. #define SJA1000_SR_BS 0x80
  80. #define SJA1000_SR_ES 0x40
  81. #define SJA1000_DEFAULT_OUTPUT_CONTROL 0xDA
  82. /*
  83. * The device actually uses a 16MHz clock to generate the CAN clock
  84. * but it expects SJA1000 bit settings based on 8MHz (is internally
  85. * converted).
  86. */
  87. #define EMS_USB_ARM7_CLOCK 8000000
  88. #define CPC_TX_QUEUE_TRIGGER_LOW 25
  89. #define CPC_TX_QUEUE_TRIGGER_HIGH 35
  90. /*
  91. * CAN-Message representation in a CPC_MSG. Message object type is
  92. * CPC_MSG_TYPE_CAN_FRAME or CPC_MSG_TYPE_RTR_FRAME or
  93. * CPC_MSG_TYPE_EXT_CAN_FRAME or CPC_MSG_TYPE_EXT_RTR_FRAME.
  94. */
  95. struct cpc_can_msg {
  96. __le32 id;
  97. u8 length;
  98. u8 msg[8];
  99. };
  100. /* Representation of the CAN parameters for the SJA1000 controller */
  101. struct cpc_sja1000_params {
  102. u8 mode;
  103. u8 acc_code0;
  104. u8 acc_code1;
  105. u8 acc_code2;
  106. u8 acc_code3;
  107. u8 acc_mask0;
  108. u8 acc_mask1;
  109. u8 acc_mask2;
  110. u8 acc_mask3;
  111. u8 btr0;
  112. u8 btr1;
  113. u8 outp_contr;
  114. };
  115. /* CAN params message representation */
  116. struct cpc_can_params {
  117. u8 cc_type;
  118. /* Will support M16C CAN controller in the future */
  119. union {
  120. struct cpc_sja1000_params sja1000;
  121. } cc_params;
  122. };
  123. /* Structure for confirmed message handling */
  124. struct cpc_confirm {
  125. u8 error; /* error code */
  126. };
  127. /* Structure for overrun conditions */
  128. struct cpc_overrun {
  129. u8 event;
  130. u8 count;
  131. };
  132. /* SJA1000 CAN errors (compatible to NXP LPC2119) */
  133. struct cpc_sja1000_can_error {
  134. u8 ecc;
  135. u8 rxerr;
  136. u8 txerr;
  137. };
  138. /* structure for CAN error conditions */
  139. struct cpc_can_error {
  140. u8 ecode;
  141. struct {
  142. u8 cc_type;
  143. /* Other controllers may also provide error code capture regs */
  144. union {
  145. struct cpc_sja1000_can_error sja1000;
  146. } regs;
  147. } cc;
  148. };
  149. /*
  150. * Structure containing RX/TX error counter. This structure is used to request
  151. * the values of the CAN controllers TX and RX error counter.
  152. */
  153. struct cpc_can_err_counter {
  154. u8 rx;
  155. u8 tx;
  156. };
  157. /* Main message type used between library and application */
  158. struct __packed ems_cpc_msg {
  159. u8 type; /* type of message */
  160. u8 length; /* length of data within union 'msg' */
  161. u8 msgid; /* confirmation handle */
  162. __le32 ts_sec; /* timestamp in seconds */
  163. __le32 ts_nsec; /* timestamp in nano seconds */
  164. union {
  165. u8 generic[64];
  166. struct cpc_can_msg can_msg;
  167. struct cpc_can_params can_params;
  168. struct cpc_confirm confirmation;
  169. struct cpc_overrun overrun;
  170. struct cpc_can_error error;
  171. struct cpc_can_err_counter err_counter;
  172. u8 can_state;
  173. } msg;
  174. };
  175. /*
  176. * Table of devices that work with this driver
  177. * NOTE: This driver supports only CPC-USB/ARM7 (LPC2119) yet.
  178. */
  179. static struct usb_device_id ems_usb_table[] = {
  180. {USB_DEVICE(USB_CPCUSB_VENDOR_ID, USB_CPCUSB_ARM7_PRODUCT_ID)},
  181. {} /* Terminating entry */
  182. };
  183. MODULE_DEVICE_TABLE(usb, ems_usb_table);
  184. #define RX_BUFFER_SIZE 64
  185. #define CPC_HEADER_SIZE 4
  186. #define INTR_IN_BUFFER_SIZE 4
  187. #define MAX_RX_URBS 10
  188. #define MAX_TX_URBS 10
  189. struct ems_usb;
  190. struct ems_tx_urb_context {
  191. struct ems_usb *dev;
  192. u32 echo_index;
  193. u8 dlc;
  194. };
  195. struct ems_usb {
  196. struct can_priv can; /* must be the first member */
  197. struct sk_buff *echo_skb[MAX_TX_URBS];
  198. struct usb_device *udev;
  199. struct net_device *netdev;
  200. atomic_t active_tx_urbs;
  201. struct usb_anchor tx_submitted;
  202. struct ems_tx_urb_context tx_contexts[MAX_TX_URBS];
  203. struct usb_anchor rx_submitted;
  204. struct urb *intr_urb;
  205. u8 *tx_msg_buffer;
  206. u8 *intr_in_buffer;
  207. unsigned int free_slots; /* remember number of available slots */
  208. struct ems_cpc_msg active_params; /* active controller parameters */
  209. void *rxbuf[MAX_RX_URBS];
  210. dma_addr_t rxbuf_dma[MAX_RX_URBS];
  211. };
  212. static void ems_usb_read_interrupt_callback(struct urb *urb)
  213. {
  214. struct ems_usb *dev = urb->context;
  215. struct net_device *netdev = dev->netdev;
  216. int err;
  217. if (!netif_device_present(netdev))
  218. return;
  219. switch (urb->status) {
  220. case 0:
  221. dev->free_slots = dev->intr_in_buffer[1];
  222. if (dev->free_slots > CPC_TX_QUEUE_TRIGGER_HIGH &&
  223. netif_queue_stopped(netdev))
  224. netif_wake_queue(netdev);
  225. break;
  226. case -ECONNRESET: /* unlink */
  227. case -ENOENT:
  228. case -EPIPE:
  229. case -EPROTO:
  230. case -ESHUTDOWN:
  231. return;
  232. default:
  233. netdev_info(netdev, "Rx interrupt aborted %d\n", urb->status);
  234. break;
  235. }
  236. err = usb_submit_urb(urb, GFP_ATOMIC);
  237. if (err == -ENODEV)
  238. netif_device_detach(netdev);
  239. else if (err)
  240. netdev_err(netdev, "failed resubmitting intr urb: %d\n", err);
  241. }
  242. static void ems_usb_rx_can_msg(struct ems_usb *dev, struct ems_cpc_msg *msg)
  243. {
  244. struct can_frame *cf;
  245. struct sk_buff *skb;
  246. int i;
  247. struct net_device_stats *stats = &dev->netdev->stats;
  248. skb = alloc_can_skb(dev->netdev, &cf);
  249. if (skb == NULL)
  250. return;
  251. cf->can_id = le32_to_cpu(msg->msg.can_msg.id);
  252. cf->can_dlc = get_can_dlc(msg->msg.can_msg.length & 0xF);
  253. if (msg->type == CPC_MSG_TYPE_EXT_CAN_FRAME ||
  254. msg->type == CPC_MSG_TYPE_EXT_RTR_FRAME)
  255. cf->can_id |= CAN_EFF_FLAG;
  256. if (msg->type == CPC_MSG_TYPE_RTR_FRAME ||
  257. msg->type == CPC_MSG_TYPE_EXT_RTR_FRAME) {
  258. cf->can_id |= CAN_RTR_FLAG;
  259. } else {
  260. for (i = 0; i < cf->can_dlc; i++)
  261. cf->data[i] = msg->msg.can_msg.msg[i];
  262. }
  263. stats->rx_packets++;
  264. stats->rx_bytes += cf->can_dlc;
  265. netif_rx(skb);
  266. }
  267. static void ems_usb_rx_err(struct ems_usb *dev, struct ems_cpc_msg *msg)
  268. {
  269. struct can_frame *cf;
  270. struct sk_buff *skb;
  271. struct net_device_stats *stats = &dev->netdev->stats;
  272. skb = alloc_can_err_skb(dev->netdev, &cf);
  273. if (skb == NULL)
  274. return;
  275. if (msg->type == CPC_MSG_TYPE_CAN_STATE) {
  276. u8 state = msg->msg.can_state;
  277. if (state & SJA1000_SR_BS) {
  278. dev->can.state = CAN_STATE_BUS_OFF;
  279. cf->can_id |= CAN_ERR_BUSOFF;
  280. dev->can.can_stats.bus_off++;
  281. can_bus_off(dev->netdev);
  282. } else if (state & SJA1000_SR_ES) {
  283. dev->can.state = CAN_STATE_ERROR_WARNING;
  284. dev->can.can_stats.error_warning++;
  285. } else {
  286. dev->can.state = CAN_STATE_ERROR_ACTIVE;
  287. dev->can.can_stats.error_passive++;
  288. }
  289. } else if (msg->type == CPC_MSG_TYPE_CAN_FRAME_ERROR) {
  290. u8 ecc = msg->msg.error.cc.regs.sja1000.ecc;
  291. u8 txerr = msg->msg.error.cc.regs.sja1000.txerr;
  292. u8 rxerr = msg->msg.error.cc.regs.sja1000.rxerr;
  293. /* bus error interrupt */
  294. dev->can.can_stats.bus_error++;
  295. stats->rx_errors++;
  296. cf->can_id |= CAN_ERR_PROT | CAN_ERR_BUSERROR;
  297. switch (ecc & SJA1000_ECC_MASK) {
  298. case SJA1000_ECC_BIT:
  299. cf->data[2] |= CAN_ERR_PROT_BIT;
  300. break;
  301. case SJA1000_ECC_FORM:
  302. cf->data[2] |= CAN_ERR_PROT_FORM;
  303. break;
  304. case SJA1000_ECC_STUFF:
  305. cf->data[2] |= CAN_ERR_PROT_STUFF;
  306. break;
  307. default:
  308. cf->data[3] = ecc & SJA1000_ECC_SEG;
  309. break;
  310. }
  311. /* Error occurred during transmission? */
  312. if ((ecc & SJA1000_ECC_DIR) == 0)
  313. cf->data[2] |= CAN_ERR_PROT_TX;
  314. if (dev->can.state == CAN_STATE_ERROR_WARNING ||
  315. dev->can.state == CAN_STATE_ERROR_PASSIVE) {
  316. cf->can_id |= CAN_ERR_CRTL;
  317. cf->data[1] = (txerr > rxerr) ?
  318. CAN_ERR_CRTL_TX_PASSIVE : CAN_ERR_CRTL_RX_PASSIVE;
  319. }
  320. } else if (msg->type == CPC_MSG_TYPE_OVERRUN) {
  321. cf->can_id |= CAN_ERR_CRTL;
  322. cf->data[1] = CAN_ERR_CRTL_RX_OVERFLOW;
  323. stats->rx_over_errors++;
  324. stats->rx_errors++;
  325. }
  326. stats->rx_packets++;
  327. stats->rx_bytes += cf->can_dlc;
  328. netif_rx(skb);
  329. }
  330. /*
  331. * callback for bulk IN urb
  332. */
  333. static void ems_usb_read_bulk_callback(struct urb *urb)
  334. {
  335. struct ems_usb *dev = urb->context;
  336. struct net_device *netdev;
  337. int retval;
  338. netdev = dev->netdev;
  339. if (!netif_device_present(netdev))
  340. return;
  341. switch (urb->status) {
  342. case 0: /* success */
  343. break;
  344. case -ENOENT:
  345. return;
  346. default:
  347. netdev_info(netdev, "Rx URB aborted (%d)\n", urb->status);
  348. goto resubmit_urb;
  349. }
  350. if (urb->actual_length > CPC_HEADER_SIZE) {
  351. struct ems_cpc_msg *msg;
  352. u8 *ibuf = urb->transfer_buffer;
  353. u8 msg_count, start;
  354. msg_count = ibuf[0] & ~0x80;
  355. start = CPC_HEADER_SIZE;
  356. while (msg_count) {
  357. msg = (struct ems_cpc_msg *)&ibuf[start];
  358. switch (msg->type) {
  359. case CPC_MSG_TYPE_CAN_STATE:
  360. /* Process CAN state changes */
  361. ems_usb_rx_err(dev, msg);
  362. break;
  363. case CPC_MSG_TYPE_CAN_FRAME:
  364. case CPC_MSG_TYPE_EXT_CAN_FRAME:
  365. case CPC_MSG_TYPE_RTR_FRAME:
  366. case CPC_MSG_TYPE_EXT_RTR_FRAME:
  367. ems_usb_rx_can_msg(dev, msg);
  368. break;
  369. case CPC_MSG_TYPE_CAN_FRAME_ERROR:
  370. /* Process errorframe */
  371. ems_usb_rx_err(dev, msg);
  372. break;
  373. case CPC_MSG_TYPE_OVERRUN:
  374. /* Message lost while receiving */
  375. ems_usb_rx_err(dev, msg);
  376. break;
  377. }
  378. start += CPC_MSG_HEADER_LEN + msg->length;
  379. msg_count--;
  380. if (start > urb->transfer_buffer_length) {
  381. netdev_err(netdev, "format error\n");
  382. break;
  383. }
  384. }
  385. }
  386. resubmit_urb:
  387. usb_fill_bulk_urb(urb, dev->udev, usb_rcvbulkpipe(dev->udev, 2),
  388. urb->transfer_buffer, RX_BUFFER_SIZE,
  389. ems_usb_read_bulk_callback, dev);
  390. retval = usb_submit_urb(urb, GFP_ATOMIC);
  391. if (retval == -ENODEV)
  392. netif_device_detach(netdev);
  393. else if (retval)
  394. netdev_err(netdev,
  395. "failed resubmitting read bulk urb: %d\n", retval);
  396. }
  397. /*
  398. * callback for bulk IN urb
  399. */
  400. static void ems_usb_write_bulk_callback(struct urb *urb)
  401. {
  402. struct ems_tx_urb_context *context = urb->context;
  403. struct ems_usb *dev;
  404. struct net_device *netdev;
  405. BUG_ON(!context);
  406. dev = context->dev;
  407. netdev = dev->netdev;
  408. /* free up our allocated buffer */
  409. usb_free_coherent(urb->dev, urb->transfer_buffer_length,
  410. urb->transfer_buffer, urb->transfer_dma);
  411. atomic_dec(&dev->active_tx_urbs);
  412. if (!netif_device_present(netdev))
  413. return;
  414. if (urb->status)
  415. netdev_info(netdev, "Tx URB aborted (%d)\n", urb->status);
  416. netif_trans_update(netdev);
  417. /* transmission complete interrupt */
  418. netdev->stats.tx_packets++;
  419. netdev->stats.tx_bytes += context->dlc;
  420. can_get_echo_skb(netdev, context->echo_index);
  421. /* Release context */
  422. context->echo_index = MAX_TX_URBS;
  423. }
  424. /*
  425. * Send the given CPC command synchronously
  426. */
  427. static int ems_usb_command_msg(struct ems_usb *dev, struct ems_cpc_msg *msg)
  428. {
  429. int actual_length;
  430. /* Copy payload */
  431. memcpy(&dev->tx_msg_buffer[CPC_HEADER_SIZE], msg,
  432. msg->length + CPC_MSG_HEADER_LEN);
  433. /* Clear header */
  434. memset(&dev->tx_msg_buffer[0], 0, CPC_HEADER_SIZE);
  435. return usb_bulk_msg(dev->udev, usb_sndbulkpipe(dev->udev, 2),
  436. &dev->tx_msg_buffer[0],
  437. msg->length + CPC_MSG_HEADER_LEN + CPC_HEADER_SIZE,
  438. &actual_length, 1000);
  439. }
  440. /*
  441. * Change CAN controllers' mode register
  442. */
  443. static int ems_usb_write_mode(struct ems_usb *dev, u8 mode)
  444. {
  445. dev->active_params.msg.can_params.cc_params.sja1000.mode = mode;
  446. return ems_usb_command_msg(dev, &dev->active_params);
  447. }
  448. /*
  449. * Send a CPC_Control command to change behaviour when interface receives a CAN
  450. * message, bus error or CAN state changed notifications.
  451. */
  452. static int ems_usb_control_cmd(struct ems_usb *dev, u8 val)
  453. {
  454. struct ems_cpc_msg cmd;
  455. cmd.type = CPC_CMD_TYPE_CONTROL;
  456. cmd.length = CPC_MSG_HEADER_LEN + 1;
  457. cmd.msgid = 0;
  458. cmd.msg.generic[0] = val;
  459. return ems_usb_command_msg(dev, &cmd);
  460. }
  461. /*
  462. * Start interface
  463. */
  464. static int ems_usb_start(struct ems_usb *dev)
  465. {
  466. struct net_device *netdev = dev->netdev;
  467. int err, i;
  468. dev->intr_in_buffer[0] = 0;
  469. dev->free_slots = 50; /* initial size */
  470. for (i = 0; i < MAX_RX_URBS; i++) {
  471. struct urb *urb = NULL;
  472. u8 *buf = NULL;
  473. dma_addr_t buf_dma;
  474. /* create a URB, and a buffer for it */
  475. urb = usb_alloc_urb(0, GFP_KERNEL);
  476. if (!urb) {
  477. err = -ENOMEM;
  478. break;
  479. }
  480. buf = usb_alloc_coherent(dev->udev, RX_BUFFER_SIZE, GFP_KERNEL,
  481. &buf_dma);
  482. if (!buf) {
  483. netdev_err(netdev, "No memory left for USB buffer\n");
  484. usb_free_urb(urb);
  485. err = -ENOMEM;
  486. break;
  487. }
  488. urb->transfer_dma = buf_dma;
  489. usb_fill_bulk_urb(urb, dev->udev, usb_rcvbulkpipe(dev->udev, 2),
  490. buf, RX_BUFFER_SIZE,
  491. ems_usb_read_bulk_callback, dev);
  492. urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
  493. usb_anchor_urb(urb, &dev->rx_submitted);
  494. err = usb_submit_urb(urb, GFP_KERNEL);
  495. if (err) {
  496. usb_unanchor_urb(urb);
  497. usb_free_coherent(dev->udev, RX_BUFFER_SIZE, buf,
  498. urb->transfer_dma);
  499. usb_free_urb(urb);
  500. break;
  501. }
  502. dev->rxbuf[i] = buf;
  503. dev->rxbuf_dma[i] = buf_dma;
  504. /* Drop reference, USB core will take care of freeing it */
  505. usb_free_urb(urb);
  506. }
  507. /* Did we submit any URBs */
  508. if (i == 0) {
  509. netdev_warn(netdev, "couldn't setup read URBs\n");
  510. return err;
  511. }
  512. /* Warn if we've couldn't transmit all the URBs */
  513. if (i < MAX_RX_URBS)
  514. netdev_warn(netdev, "rx performance may be slow\n");
  515. /* Setup and start interrupt URB */
  516. usb_fill_int_urb(dev->intr_urb, dev->udev,
  517. usb_rcvintpipe(dev->udev, 1),
  518. dev->intr_in_buffer,
  519. INTR_IN_BUFFER_SIZE,
  520. ems_usb_read_interrupt_callback, dev, 1);
  521. err = usb_submit_urb(dev->intr_urb, GFP_KERNEL);
  522. if (err) {
  523. netdev_warn(netdev, "intr URB submit failed: %d\n", err);
  524. return err;
  525. }
  526. /* CPC-USB will transfer received message to host */
  527. err = ems_usb_control_cmd(dev, CONTR_CAN_MESSAGE | CONTR_CONT_ON);
  528. if (err)
  529. goto failed;
  530. /* CPC-USB will transfer CAN state changes to host */
  531. err = ems_usb_control_cmd(dev, CONTR_CAN_STATE | CONTR_CONT_ON);
  532. if (err)
  533. goto failed;
  534. /* CPC-USB will transfer bus errors to host */
  535. err = ems_usb_control_cmd(dev, CONTR_BUS_ERROR | CONTR_CONT_ON);
  536. if (err)
  537. goto failed;
  538. err = ems_usb_write_mode(dev, SJA1000_MOD_NORMAL);
  539. if (err)
  540. goto failed;
  541. dev->can.state = CAN_STATE_ERROR_ACTIVE;
  542. return 0;
  543. failed:
  544. netdev_warn(netdev, "couldn't submit control: %d\n", err);
  545. return err;
  546. }
  547. static void unlink_all_urbs(struct ems_usb *dev)
  548. {
  549. int i;
  550. usb_unlink_urb(dev->intr_urb);
  551. usb_kill_anchored_urbs(&dev->rx_submitted);
  552. for (i = 0; i < MAX_RX_URBS; ++i)
  553. usb_free_coherent(dev->udev, RX_BUFFER_SIZE,
  554. dev->rxbuf[i], dev->rxbuf_dma[i]);
  555. usb_kill_anchored_urbs(&dev->tx_submitted);
  556. atomic_set(&dev->active_tx_urbs, 0);
  557. for (i = 0; i < MAX_TX_URBS; i++)
  558. dev->tx_contexts[i].echo_index = MAX_TX_URBS;
  559. }
  560. static int ems_usb_open(struct net_device *netdev)
  561. {
  562. struct ems_usb *dev = netdev_priv(netdev);
  563. int err;
  564. err = ems_usb_write_mode(dev, SJA1000_MOD_RM);
  565. if (err)
  566. return err;
  567. /* common open */
  568. err = open_candev(netdev);
  569. if (err)
  570. return err;
  571. /* finally start device */
  572. err = ems_usb_start(dev);
  573. if (err) {
  574. if (err == -ENODEV)
  575. netif_device_detach(dev->netdev);
  576. netdev_warn(netdev, "couldn't start device: %d\n", err);
  577. close_candev(netdev);
  578. return err;
  579. }
  580. netif_start_queue(netdev);
  581. return 0;
  582. }
  583. static netdev_tx_t ems_usb_start_xmit(struct sk_buff *skb, struct net_device *netdev)
  584. {
  585. struct ems_usb *dev = netdev_priv(netdev);
  586. struct ems_tx_urb_context *context = NULL;
  587. struct net_device_stats *stats = &netdev->stats;
  588. struct can_frame *cf = (struct can_frame *)skb->data;
  589. struct ems_cpc_msg *msg;
  590. struct urb *urb;
  591. u8 *buf;
  592. int i, err;
  593. size_t size = CPC_HEADER_SIZE + CPC_MSG_HEADER_LEN
  594. + sizeof(struct cpc_can_msg);
  595. if (can_dropped_invalid_skb(netdev, skb))
  596. return NETDEV_TX_OK;
  597. /* create a URB, and a buffer for it, and copy the data to the URB */
  598. urb = usb_alloc_urb(0, GFP_ATOMIC);
  599. if (!urb)
  600. goto nomem;
  601. buf = usb_alloc_coherent(dev->udev, size, GFP_ATOMIC, &urb->transfer_dma);
  602. if (!buf) {
  603. netdev_err(netdev, "No memory left for USB buffer\n");
  604. usb_free_urb(urb);
  605. goto nomem;
  606. }
  607. msg = (struct ems_cpc_msg *)&buf[CPC_HEADER_SIZE];
  608. msg->msg.can_msg.id = cpu_to_le32(cf->can_id & CAN_ERR_MASK);
  609. msg->msg.can_msg.length = cf->can_dlc;
  610. if (cf->can_id & CAN_RTR_FLAG) {
  611. msg->type = cf->can_id & CAN_EFF_FLAG ?
  612. CPC_CMD_TYPE_EXT_RTR_FRAME : CPC_CMD_TYPE_RTR_FRAME;
  613. msg->length = CPC_CAN_MSG_MIN_SIZE;
  614. } else {
  615. msg->type = cf->can_id & CAN_EFF_FLAG ?
  616. CPC_CMD_TYPE_EXT_CAN_FRAME : CPC_CMD_TYPE_CAN_FRAME;
  617. for (i = 0; i < cf->can_dlc; i++)
  618. msg->msg.can_msg.msg[i] = cf->data[i];
  619. msg->length = CPC_CAN_MSG_MIN_SIZE + cf->can_dlc;
  620. }
  621. for (i = 0; i < MAX_TX_URBS; i++) {
  622. if (dev->tx_contexts[i].echo_index == MAX_TX_URBS) {
  623. context = &dev->tx_contexts[i];
  624. break;
  625. }
  626. }
  627. /*
  628. * May never happen! When this happens we'd more URBs in flight as
  629. * allowed (MAX_TX_URBS).
  630. */
  631. if (!context) {
  632. usb_free_coherent(dev->udev, size, buf, urb->transfer_dma);
  633. usb_free_urb(urb);
  634. netdev_warn(netdev, "couldn't find free context\n");
  635. return NETDEV_TX_BUSY;
  636. }
  637. context->dev = dev;
  638. context->echo_index = i;
  639. context->dlc = cf->can_dlc;
  640. usb_fill_bulk_urb(urb, dev->udev, usb_sndbulkpipe(dev->udev, 2), buf,
  641. size, ems_usb_write_bulk_callback, context);
  642. urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
  643. usb_anchor_urb(urb, &dev->tx_submitted);
  644. can_put_echo_skb(skb, netdev, context->echo_index);
  645. atomic_inc(&dev->active_tx_urbs);
  646. err = usb_submit_urb(urb, GFP_ATOMIC);
  647. if (unlikely(err)) {
  648. can_free_echo_skb(netdev, context->echo_index);
  649. usb_unanchor_urb(urb);
  650. usb_free_coherent(dev->udev, size, buf, urb->transfer_dma);
  651. dev_kfree_skb(skb);
  652. atomic_dec(&dev->active_tx_urbs);
  653. if (err == -ENODEV) {
  654. netif_device_detach(netdev);
  655. } else {
  656. netdev_warn(netdev, "failed tx_urb %d\n", err);
  657. stats->tx_dropped++;
  658. }
  659. } else {
  660. netif_trans_update(netdev);
  661. /* Slow down tx path */
  662. if (atomic_read(&dev->active_tx_urbs) >= MAX_TX_URBS ||
  663. dev->free_slots < CPC_TX_QUEUE_TRIGGER_LOW) {
  664. netif_stop_queue(netdev);
  665. }
  666. }
  667. /*
  668. * Release our reference to this URB, the USB core will eventually free
  669. * it entirely.
  670. */
  671. usb_free_urb(urb);
  672. return NETDEV_TX_OK;
  673. nomem:
  674. dev_kfree_skb(skb);
  675. stats->tx_dropped++;
  676. return NETDEV_TX_OK;
  677. }
  678. static int ems_usb_close(struct net_device *netdev)
  679. {
  680. struct ems_usb *dev = netdev_priv(netdev);
  681. /* Stop polling */
  682. unlink_all_urbs(dev);
  683. netif_stop_queue(netdev);
  684. /* Set CAN controller to reset mode */
  685. if (ems_usb_write_mode(dev, SJA1000_MOD_RM))
  686. netdev_warn(netdev, "couldn't stop device");
  687. close_candev(netdev);
  688. return 0;
  689. }
  690. static const struct net_device_ops ems_usb_netdev_ops = {
  691. .ndo_open = ems_usb_open,
  692. .ndo_stop = ems_usb_close,
  693. .ndo_start_xmit = ems_usb_start_xmit,
  694. .ndo_change_mtu = can_change_mtu,
  695. };
  696. static const struct can_bittiming_const ems_usb_bittiming_const = {
  697. .name = "ems_usb",
  698. .tseg1_min = 1,
  699. .tseg1_max = 16,
  700. .tseg2_min = 1,
  701. .tseg2_max = 8,
  702. .sjw_max = 4,
  703. .brp_min = 1,
  704. .brp_max = 64,
  705. .brp_inc = 1,
  706. };
  707. static int ems_usb_set_mode(struct net_device *netdev, enum can_mode mode)
  708. {
  709. struct ems_usb *dev = netdev_priv(netdev);
  710. switch (mode) {
  711. case CAN_MODE_START:
  712. if (ems_usb_write_mode(dev, SJA1000_MOD_NORMAL))
  713. netdev_warn(netdev, "couldn't start device");
  714. if (netif_queue_stopped(netdev))
  715. netif_wake_queue(netdev);
  716. break;
  717. default:
  718. return -EOPNOTSUPP;
  719. }
  720. return 0;
  721. }
  722. static int ems_usb_set_bittiming(struct net_device *netdev)
  723. {
  724. struct ems_usb *dev = netdev_priv(netdev);
  725. struct can_bittiming *bt = &dev->can.bittiming;
  726. u8 btr0, btr1;
  727. btr0 = ((bt->brp - 1) & 0x3f) | (((bt->sjw - 1) & 0x3) << 6);
  728. btr1 = ((bt->prop_seg + bt->phase_seg1 - 1) & 0xf) |
  729. (((bt->phase_seg2 - 1) & 0x7) << 4);
  730. if (dev->can.ctrlmode & CAN_CTRLMODE_3_SAMPLES)
  731. btr1 |= 0x80;
  732. netdev_info(netdev, "setting BTR0=0x%02x BTR1=0x%02x\n", btr0, btr1);
  733. dev->active_params.msg.can_params.cc_params.sja1000.btr0 = btr0;
  734. dev->active_params.msg.can_params.cc_params.sja1000.btr1 = btr1;
  735. return ems_usb_command_msg(dev, &dev->active_params);
  736. }
  737. static void init_params_sja1000(struct ems_cpc_msg *msg)
  738. {
  739. struct cpc_sja1000_params *sja1000 =
  740. &msg->msg.can_params.cc_params.sja1000;
  741. msg->type = CPC_CMD_TYPE_CAN_PARAMS;
  742. msg->length = sizeof(struct cpc_can_params);
  743. msg->msgid = 0;
  744. msg->msg.can_params.cc_type = CPC_CC_TYPE_SJA1000;
  745. /* Acceptance filter open */
  746. sja1000->acc_code0 = 0x00;
  747. sja1000->acc_code1 = 0x00;
  748. sja1000->acc_code2 = 0x00;
  749. sja1000->acc_code3 = 0x00;
  750. /* Acceptance filter open */
  751. sja1000->acc_mask0 = 0xFF;
  752. sja1000->acc_mask1 = 0xFF;
  753. sja1000->acc_mask2 = 0xFF;
  754. sja1000->acc_mask3 = 0xFF;
  755. sja1000->btr0 = 0;
  756. sja1000->btr1 = 0;
  757. sja1000->outp_contr = SJA1000_DEFAULT_OUTPUT_CONTROL;
  758. sja1000->mode = SJA1000_MOD_RM;
  759. }
  760. /*
  761. * probe function for new CPC-USB devices
  762. */
  763. static int ems_usb_probe(struct usb_interface *intf,
  764. const struct usb_device_id *id)
  765. {
  766. struct net_device *netdev;
  767. struct ems_usb *dev;
  768. int i, err = -ENOMEM;
  769. netdev = alloc_candev(sizeof(struct ems_usb), MAX_TX_URBS);
  770. if (!netdev) {
  771. dev_err(&intf->dev, "ems_usb: Couldn't alloc candev\n");
  772. return -ENOMEM;
  773. }
  774. dev = netdev_priv(netdev);
  775. dev->udev = interface_to_usbdev(intf);
  776. dev->netdev = netdev;
  777. dev->can.state = CAN_STATE_STOPPED;
  778. dev->can.clock.freq = EMS_USB_ARM7_CLOCK;
  779. dev->can.bittiming_const = &ems_usb_bittiming_const;
  780. dev->can.do_set_bittiming = ems_usb_set_bittiming;
  781. dev->can.do_set_mode = ems_usb_set_mode;
  782. dev->can.ctrlmode_supported = CAN_CTRLMODE_3_SAMPLES;
  783. netdev->netdev_ops = &ems_usb_netdev_ops;
  784. netdev->flags |= IFF_ECHO; /* we support local echo */
  785. init_usb_anchor(&dev->rx_submitted);
  786. init_usb_anchor(&dev->tx_submitted);
  787. atomic_set(&dev->active_tx_urbs, 0);
  788. for (i = 0; i < MAX_TX_URBS; i++)
  789. dev->tx_contexts[i].echo_index = MAX_TX_URBS;
  790. dev->intr_urb = usb_alloc_urb(0, GFP_KERNEL);
  791. if (!dev->intr_urb)
  792. goto cleanup_candev;
  793. dev->intr_in_buffer = kzalloc(INTR_IN_BUFFER_SIZE, GFP_KERNEL);
  794. if (!dev->intr_in_buffer)
  795. goto cleanup_intr_urb;
  796. dev->tx_msg_buffer = kzalloc(CPC_HEADER_SIZE +
  797. sizeof(struct ems_cpc_msg), GFP_KERNEL);
  798. if (!dev->tx_msg_buffer)
  799. goto cleanup_intr_in_buffer;
  800. usb_set_intfdata(intf, dev);
  801. SET_NETDEV_DEV(netdev, &intf->dev);
  802. init_params_sja1000(&dev->active_params);
  803. err = ems_usb_command_msg(dev, &dev->active_params);
  804. if (err) {
  805. netdev_err(netdev, "couldn't initialize controller: %d\n", err);
  806. goto cleanup_tx_msg_buffer;
  807. }
  808. err = register_candev(netdev);
  809. if (err) {
  810. netdev_err(netdev, "couldn't register CAN device: %d\n", err);
  811. goto cleanup_tx_msg_buffer;
  812. }
  813. return 0;
  814. cleanup_tx_msg_buffer:
  815. kfree(dev->tx_msg_buffer);
  816. cleanup_intr_in_buffer:
  817. kfree(dev->intr_in_buffer);
  818. cleanup_intr_urb:
  819. usb_free_urb(dev->intr_urb);
  820. cleanup_candev:
  821. free_candev(netdev);
  822. return err;
  823. }
  824. /*
  825. * called by the usb core when the device is removed from the system
  826. */
  827. static void ems_usb_disconnect(struct usb_interface *intf)
  828. {
  829. struct ems_usb *dev = usb_get_intfdata(intf);
  830. usb_set_intfdata(intf, NULL);
  831. if (dev) {
  832. unregister_netdev(dev->netdev);
  833. unlink_all_urbs(dev);
  834. usb_free_urb(dev->intr_urb);
  835. kfree(dev->intr_in_buffer);
  836. kfree(dev->tx_msg_buffer);
  837. free_candev(dev->netdev);
  838. }
  839. }
  840. /* usb specific object needed to register this driver with the usb subsystem */
  841. static struct usb_driver ems_usb_driver = {
  842. .name = "ems_usb",
  843. .probe = ems_usb_probe,
  844. .disconnect = ems_usb_disconnect,
  845. .id_table = ems_usb_table,
  846. };
  847. module_usb_driver(ems_usb_driver);