ems_usb.c 26 KB

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