ems_usb.c 26 KB

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