ems_usb.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086
  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->data[1] = (txerr > rxerr) ?
  327. CAN_ERR_CRTL_TX_PASSIVE : CAN_ERR_CRTL_RX_PASSIVE;
  328. }
  329. } else if (msg->type == CPC_MSG_TYPE_OVERRUN) {
  330. cf->can_id |= CAN_ERR_CRTL;
  331. cf->data[1] = CAN_ERR_CRTL_RX_OVERFLOW;
  332. stats->rx_over_errors++;
  333. stats->rx_errors++;
  334. }
  335. stats->rx_packets++;
  336. stats->rx_bytes += cf->can_dlc;
  337. netif_rx(skb);
  338. }
  339. /*
  340. * callback for bulk IN urb
  341. */
  342. static void ems_usb_read_bulk_callback(struct urb *urb)
  343. {
  344. struct ems_usb *dev = urb->context;
  345. struct net_device *netdev;
  346. int retval;
  347. netdev = dev->netdev;
  348. if (!netif_device_present(netdev))
  349. return;
  350. switch (urb->status) {
  351. case 0: /* success */
  352. break;
  353. case -ENOENT:
  354. return;
  355. default:
  356. netdev_info(netdev, "Rx URB aborted (%d)\n", urb->status);
  357. goto resubmit_urb;
  358. }
  359. if (urb->actual_length > CPC_HEADER_SIZE) {
  360. struct ems_cpc_msg *msg;
  361. u8 *ibuf = urb->transfer_buffer;
  362. u8 msg_count, start;
  363. msg_count = ibuf[0] & ~0x80;
  364. start = CPC_HEADER_SIZE;
  365. while (msg_count) {
  366. msg = (struct ems_cpc_msg *)&ibuf[start];
  367. switch (msg->type) {
  368. case CPC_MSG_TYPE_CAN_STATE:
  369. /* Process CAN state changes */
  370. ems_usb_rx_err(dev, msg);
  371. break;
  372. case CPC_MSG_TYPE_CAN_FRAME:
  373. case CPC_MSG_TYPE_EXT_CAN_FRAME:
  374. case CPC_MSG_TYPE_RTR_FRAME:
  375. case CPC_MSG_TYPE_EXT_RTR_FRAME:
  376. ems_usb_rx_can_msg(dev, msg);
  377. break;
  378. case CPC_MSG_TYPE_CAN_FRAME_ERROR:
  379. /* Process errorframe */
  380. ems_usb_rx_err(dev, msg);
  381. break;
  382. case CPC_MSG_TYPE_OVERRUN:
  383. /* Message lost while receiving */
  384. ems_usb_rx_err(dev, msg);
  385. break;
  386. }
  387. start += CPC_MSG_HEADER_LEN + msg->length;
  388. msg_count--;
  389. if (start > urb->transfer_buffer_length) {
  390. netdev_err(netdev, "format error\n");
  391. break;
  392. }
  393. }
  394. }
  395. resubmit_urb:
  396. usb_fill_bulk_urb(urb, dev->udev, usb_rcvbulkpipe(dev->udev, 2),
  397. urb->transfer_buffer, RX_BUFFER_SIZE,
  398. ems_usb_read_bulk_callback, dev);
  399. retval = usb_submit_urb(urb, GFP_ATOMIC);
  400. if (retval == -ENODEV)
  401. netif_device_detach(netdev);
  402. else if (retval)
  403. netdev_err(netdev,
  404. "failed resubmitting read bulk urb: %d\n", retval);
  405. }
  406. /*
  407. * callback for bulk IN urb
  408. */
  409. static void ems_usb_write_bulk_callback(struct urb *urb)
  410. {
  411. struct ems_tx_urb_context *context = urb->context;
  412. struct ems_usb *dev;
  413. struct net_device *netdev;
  414. BUG_ON(!context);
  415. dev = context->dev;
  416. netdev = dev->netdev;
  417. /* free up our allocated buffer */
  418. usb_free_coherent(urb->dev, urb->transfer_buffer_length,
  419. urb->transfer_buffer, urb->transfer_dma);
  420. atomic_dec(&dev->active_tx_urbs);
  421. if (!netif_device_present(netdev))
  422. return;
  423. if (urb->status)
  424. netdev_info(netdev, "Tx URB aborted (%d)\n", urb->status);
  425. netif_trans_update(netdev);
  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. }
  433. /*
  434. * Send the given CPC command synchronously
  435. */
  436. static int ems_usb_command_msg(struct ems_usb *dev, struct ems_cpc_msg *msg)
  437. {
  438. int actual_length;
  439. /* Copy payload */
  440. memcpy(&dev->tx_msg_buffer[CPC_HEADER_SIZE], msg,
  441. msg->length + CPC_MSG_HEADER_LEN);
  442. /* Clear header */
  443. memset(&dev->tx_msg_buffer[0], 0, CPC_HEADER_SIZE);
  444. return usb_bulk_msg(dev->udev, usb_sndbulkpipe(dev->udev, 2),
  445. &dev->tx_msg_buffer[0],
  446. msg->length + CPC_MSG_HEADER_LEN + CPC_HEADER_SIZE,
  447. &actual_length, 1000);
  448. }
  449. /*
  450. * Change CAN controllers' mode register
  451. */
  452. static int ems_usb_write_mode(struct ems_usb *dev, u8 mode)
  453. {
  454. dev->active_params.msg.can_params.cc_params.sja1000.mode = mode;
  455. return ems_usb_command_msg(dev, &dev->active_params);
  456. }
  457. /*
  458. * Send a CPC_Control command to change behaviour when interface receives a CAN
  459. * message, bus error or CAN state changed notifications.
  460. */
  461. static int ems_usb_control_cmd(struct ems_usb *dev, u8 val)
  462. {
  463. struct ems_cpc_msg cmd;
  464. cmd.type = CPC_CMD_TYPE_CONTROL;
  465. cmd.length = CPC_MSG_HEADER_LEN + 1;
  466. cmd.msgid = 0;
  467. cmd.msg.generic[0] = val;
  468. return ems_usb_command_msg(dev, &cmd);
  469. }
  470. /*
  471. * Start interface
  472. */
  473. static int ems_usb_start(struct ems_usb *dev)
  474. {
  475. struct net_device *netdev = dev->netdev;
  476. int err, i;
  477. dev->intr_in_buffer[0] = 0;
  478. dev->free_slots = 50; /* initial size */
  479. for (i = 0; i < MAX_RX_URBS; i++) {
  480. struct urb *urb = NULL;
  481. u8 *buf = NULL;
  482. /* create a URB, and a buffer for it */
  483. urb = usb_alloc_urb(0, GFP_KERNEL);
  484. if (!urb) {
  485. err = -ENOMEM;
  486. break;
  487. }
  488. buf = usb_alloc_coherent(dev->udev, RX_BUFFER_SIZE, GFP_KERNEL,
  489. &urb->transfer_dma);
  490. if (!buf) {
  491. netdev_err(netdev, "No memory left for USB buffer\n");
  492. usb_free_urb(urb);
  493. err = -ENOMEM;
  494. break;
  495. }
  496. usb_fill_bulk_urb(urb, dev->udev, usb_rcvbulkpipe(dev->udev, 2),
  497. buf, RX_BUFFER_SIZE,
  498. ems_usb_read_bulk_callback, dev);
  499. urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
  500. usb_anchor_urb(urb, &dev->rx_submitted);
  501. err = usb_submit_urb(urb, GFP_KERNEL);
  502. if (err) {
  503. usb_unanchor_urb(urb);
  504. usb_free_coherent(dev->udev, RX_BUFFER_SIZE, buf,
  505. urb->transfer_dma);
  506. usb_free_urb(urb);
  507. break;
  508. }
  509. /* Drop reference, USB core will take care of freeing it */
  510. usb_free_urb(urb);
  511. }
  512. /* Did we submit any URBs */
  513. if (i == 0) {
  514. netdev_warn(netdev, "couldn't setup read URBs\n");
  515. return err;
  516. }
  517. /* Warn if we've couldn't transmit all the URBs */
  518. if (i < MAX_RX_URBS)
  519. netdev_warn(netdev, "rx performance may be slow\n");
  520. /* Setup and start interrupt URB */
  521. usb_fill_int_urb(dev->intr_urb, dev->udev,
  522. usb_rcvintpipe(dev->udev, 1),
  523. dev->intr_in_buffer,
  524. INTR_IN_BUFFER_SIZE,
  525. ems_usb_read_interrupt_callback, dev, 1);
  526. err = usb_submit_urb(dev->intr_urb, GFP_KERNEL);
  527. if (err) {
  528. netdev_warn(netdev, "intr URB submit failed: %d\n", err);
  529. return err;
  530. }
  531. /* CPC-USB will transfer received message to host */
  532. err = ems_usb_control_cmd(dev, CONTR_CAN_MESSAGE | CONTR_CONT_ON);
  533. if (err)
  534. goto failed;
  535. /* CPC-USB will transfer CAN state changes to host */
  536. err = ems_usb_control_cmd(dev, CONTR_CAN_STATE | CONTR_CONT_ON);
  537. if (err)
  538. goto failed;
  539. /* CPC-USB will transfer bus errors to host */
  540. err = ems_usb_control_cmd(dev, CONTR_BUS_ERROR | CONTR_CONT_ON);
  541. if (err)
  542. goto failed;
  543. err = ems_usb_write_mode(dev, SJA1000_MOD_NORMAL);
  544. if (err)
  545. goto failed;
  546. dev->can.state = CAN_STATE_ERROR_ACTIVE;
  547. return 0;
  548. failed:
  549. netdev_warn(netdev, "couldn't submit control: %d\n", err);
  550. return err;
  551. }
  552. static void unlink_all_urbs(struct ems_usb *dev)
  553. {
  554. int i;
  555. usb_unlink_urb(dev->intr_urb);
  556. usb_kill_anchored_urbs(&dev->rx_submitted);
  557. usb_kill_anchored_urbs(&dev->tx_submitted);
  558. atomic_set(&dev->active_tx_urbs, 0);
  559. for (i = 0; i < MAX_TX_URBS; i++)
  560. dev->tx_contexts[i].echo_index = MAX_TX_URBS;
  561. }
  562. static int ems_usb_open(struct net_device *netdev)
  563. {
  564. struct ems_usb *dev = netdev_priv(netdev);
  565. int err;
  566. err = ems_usb_write_mode(dev, SJA1000_MOD_RM);
  567. if (err)
  568. return err;
  569. /* common open */
  570. err = open_candev(netdev);
  571. if (err)
  572. return err;
  573. /* finally start device */
  574. err = ems_usb_start(dev);
  575. if (err) {
  576. if (err == -ENODEV)
  577. netif_device_detach(dev->netdev);
  578. netdev_warn(netdev, "couldn't start device: %d\n", err);
  579. close_candev(netdev);
  580. return err;
  581. }
  582. netif_start_queue(netdev);
  583. return 0;
  584. }
  585. static netdev_tx_t ems_usb_start_xmit(struct sk_buff *skb, struct net_device *netdev)
  586. {
  587. struct ems_usb *dev = netdev_priv(netdev);
  588. struct ems_tx_urb_context *context = NULL;
  589. struct net_device_stats *stats = &netdev->stats;
  590. struct can_frame *cf = (struct can_frame *)skb->data;
  591. struct ems_cpc_msg *msg;
  592. struct urb *urb;
  593. u8 *buf;
  594. int i, err;
  595. size_t size = CPC_HEADER_SIZE + CPC_MSG_HEADER_LEN
  596. + sizeof(struct cpc_can_msg);
  597. if (can_dropped_invalid_skb(netdev, skb))
  598. return NETDEV_TX_OK;
  599. /* create a URB, and a buffer for it, and copy the data to the URB */
  600. urb = usb_alloc_urb(0, GFP_ATOMIC);
  601. if (!urb)
  602. goto nomem;
  603. buf = usb_alloc_coherent(dev->udev, size, GFP_ATOMIC, &urb->transfer_dma);
  604. if (!buf) {
  605. netdev_err(netdev, "No memory left for USB buffer\n");
  606. usb_free_urb(urb);
  607. goto nomem;
  608. }
  609. msg = (struct ems_cpc_msg *)&buf[CPC_HEADER_SIZE];
  610. msg->msg.can_msg.id = cpu_to_le32(cf->can_id & CAN_ERR_MASK);
  611. msg->msg.can_msg.length = cf->can_dlc;
  612. if (cf->can_id & CAN_RTR_FLAG) {
  613. msg->type = cf->can_id & CAN_EFF_FLAG ?
  614. CPC_CMD_TYPE_EXT_RTR_FRAME : CPC_CMD_TYPE_RTR_FRAME;
  615. msg->length = CPC_CAN_MSG_MIN_SIZE;
  616. } else {
  617. msg->type = cf->can_id & CAN_EFF_FLAG ?
  618. CPC_CMD_TYPE_EXT_CAN_FRAME : CPC_CMD_TYPE_CAN_FRAME;
  619. for (i = 0; i < cf->can_dlc; i++)
  620. msg->msg.can_msg.msg[i] = cf->data[i];
  621. msg->length = CPC_CAN_MSG_MIN_SIZE + cf->can_dlc;
  622. }
  623. for (i = 0; i < MAX_TX_URBS; i++) {
  624. if (dev->tx_contexts[i].echo_index == MAX_TX_URBS) {
  625. context = &dev->tx_contexts[i];
  626. break;
  627. }
  628. }
  629. /*
  630. * May never happen! When this happens we'd more URBs in flight as
  631. * allowed (MAX_TX_URBS).
  632. */
  633. if (!context) {
  634. usb_free_coherent(dev->udev, size, buf, urb->transfer_dma);
  635. usb_free_urb(urb);
  636. netdev_warn(netdev, "couldn't find free context\n");
  637. return NETDEV_TX_BUSY;
  638. }
  639. context->dev = dev;
  640. context->echo_index = i;
  641. context->dlc = cf->can_dlc;
  642. usb_fill_bulk_urb(urb, dev->udev, usb_sndbulkpipe(dev->udev, 2), buf,
  643. size, ems_usb_write_bulk_callback, context);
  644. urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
  645. usb_anchor_urb(urb, &dev->tx_submitted);
  646. can_put_echo_skb(skb, netdev, context->echo_index);
  647. atomic_inc(&dev->active_tx_urbs);
  648. err = usb_submit_urb(urb, GFP_ATOMIC);
  649. if (unlikely(err)) {
  650. can_free_echo_skb(netdev, context->echo_index);
  651. usb_unanchor_urb(urb);
  652. usb_free_coherent(dev->udev, size, buf, urb->transfer_dma);
  653. dev_kfree_skb(skb);
  654. atomic_dec(&dev->active_tx_urbs);
  655. if (err == -ENODEV) {
  656. netif_device_detach(netdev);
  657. } else {
  658. netdev_warn(netdev, "failed tx_urb %d\n", err);
  659. stats->tx_dropped++;
  660. }
  661. } else {
  662. netif_trans_update(netdev);
  663. /* Slow down tx path */
  664. if (atomic_read(&dev->active_tx_urbs) >= MAX_TX_URBS ||
  665. dev->free_slots < CPC_TX_QUEUE_TRIGGER_LOW) {
  666. netif_stop_queue(netdev);
  667. }
  668. }
  669. /*
  670. * Release our reference to this URB, the USB core will eventually free
  671. * it entirely.
  672. */
  673. usb_free_urb(urb);
  674. return NETDEV_TX_OK;
  675. nomem:
  676. dev_kfree_skb(skb);
  677. stats->tx_dropped++;
  678. return NETDEV_TX_OK;
  679. }
  680. static int ems_usb_close(struct net_device *netdev)
  681. {
  682. struct ems_usb *dev = netdev_priv(netdev);
  683. /* Stop polling */
  684. unlink_all_urbs(dev);
  685. netif_stop_queue(netdev);
  686. /* Set CAN controller to reset mode */
  687. if (ems_usb_write_mode(dev, SJA1000_MOD_RM))
  688. netdev_warn(netdev, "couldn't stop device");
  689. close_candev(netdev);
  690. return 0;
  691. }
  692. static const struct net_device_ops ems_usb_netdev_ops = {
  693. .ndo_open = ems_usb_open,
  694. .ndo_stop = ems_usb_close,
  695. .ndo_start_xmit = ems_usb_start_xmit,
  696. .ndo_change_mtu = can_change_mtu,
  697. };
  698. static const struct can_bittiming_const ems_usb_bittiming_const = {
  699. .name = "ems_usb",
  700. .tseg1_min = 1,
  701. .tseg1_max = 16,
  702. .tseg2_min = 1,
  703. .tseg2_max = 8,
  704. .sjw_max = 4,
  705. .brp_min = 1,
  706. .brp_max = 64,
  707. .brp_inc = 1,
  708. };
  709. static int ems_usb_set_mode(struct net_device *netdev, enum can_mode mode)
  710. {
  711. struct ems_usb *dev = netdev_priv(netdev);
  712. switch (mode) {
  713. case CAN_MODE_START:
  714. if (ems_usb_write_mode(dev, SJA1000_MOD_NORMAL))
  715. netdev_warn(netdev, "couldn't start device");
  716. if (netif_queue_stopped(netdev))
  717. netif_wake_queue(netdev);
  718. break;
  719. default:
  720. return -EOPNOTSUPP;
  721. }
  722. return 0;
  723. }
  724. static int ems_usb_set_bittiming(struct net_device *netdev)
  725. {
  726. struct ems_usb *dev = netdev_priv(netdev);
  727. struct can_bittiming *bt = &dev->can.bittiming;
  728. u8 btr0, btr1;
  729. btr0 = ((bt->brp - 1) & 0x3f) | (((bt->sjw - 1) & 0x3) << 6);
  730. btr1 = ((bt->prop_seg + bt->phase_seg1 - 1) & 0xf) |
  731. (((bt->phase_seg2 - 1) & 0x7) << 4);
  732. if (dev->can.ctrlmode & CAN_CTRLMODE_3_SAMPLES)
  733. btr1 |= 0x80;
  734. netdev_info(netdev, "setting BTR0=0x%02x BTR1=0x%02x\n", btr0, btr1);
  735. dev->active_params.msg.can_params.cc_params.sja1000.btr0 = btr0;
  736. dev->active_params.msg.can_params.cc_params.sja1000.btr1 = btr1;
  737. return ems_usb_command_msg(dev, &dev->active_params);
  738. }
  739. static void init_params_sja1000(struct ems_cpc_msg *msg)
  740. {
  741. struct cpc_sja1000_params *sja1000 =
  742. &msg->msg.can_params.cc_params.sja1000;
  743. msg->type = CPC_CMD_TYPE_CAN_PARAMS;
  744. msg->length = sizeof(struct cpc_can_params);
  745. msg->msgid = 0;
  746. msg->msg.can_params.cc_type = CPC_CC_TYPE_SJA1000;
  747. /* Acceptance filter open */
  748. sja1000->acc_code0 = 0x00;
  749. sja1000->acc_code1 = 0x00;
  750. sja1000->acc_code2 = 0x00;
  751. sja1000->acc_code3 = 0x00;
  752. /* Acceptance filter open */
  753. sja1000->acc_mask0 = 0xFF;
  754. sja1000->acc_mask1 = 0xFF;
  755. sja1000->acc_mask2 = 0xFF;
  756. sja1000->acc_mask3 = 0xFF;
  757. sja1000->btr0 = 0;
  758. sja1000->btr1 = 0;
  759. sja1000->outp_contr = SJA1000_DEFAULT_OUTPUT_CONTROL;
  760. sja1000->mode = SJA1000_MOD_RM;
  761. }
  762. /*
  763. * probe function for new CPC-USB devices
  764. */
  765. static int ems_usb_probe(struct usb_interface *intf,
  766. const struct usb_device_id *id)
  767. {
  768. struct net_device *netdev;
  769. struct ems_usb *dev;
  770. int i, err = -ENOMEM;
  771. netdev = alloc_candev(sizeof(struct ems_usb), MAX_TX_URBS);
  772. if (!netdev) {
  773. dev_err(&intf->dev, "ems_usb: Couldn't alloc candev\n");
  774. return -ENOMEM;
  775. }
  776. dev = netdev_priv(netdev);
  777. dev->udev = interface_to_usbdev(intf);
  778. dev->netdev = netdev;
  779. dev->can.state = CAN_STATE_STOPPED;
  780. dev->can.clock.freq = EMS_USB_ARM7_CLOCK;
  781. dev->can.bittiming_const = &ems_usb_bittiming_const;
  782. dev->can.do_set_bittiming = ems_usb_set_bittiming;
  783. dev->can.do_set_mode = ems_usb_set_mode;
  784. dev->can.ctrlmode_supported = CAN_CTRLMODE_3_SAMPLES;
  785. netdev->netdev_ops = &ems_usb_netdev_ops;
  786. netdev->flags |= IFF_ECHO; /* we support local echo */
  787. init_usb_anchor(&dev->rx_submitted);
  788. init_usb_anchor(&dev->tx_submitted);
  789. atomic_set(&dev->active_tx_urbs, 0);
  790. for (i = 0; i < MAX_TX_URBS; i++)
  791. dev->tx_contexts[i].echo_index = MAX_TX_URBS;
  792. dev->intr_urb = usb_alloc_urb(0, GFP_KERNEL);
  793. if (!dev->intr_urb)
  794. goto cleanup_candev;
  795. dev->intr_in_buffer = kzalloc(INTR_IN_BUFFER_SIZE, GFP_KERNEL);
  796. if (!dev->intr_in_buffer)
  797. goto cleanup_intr_urb;
  798. dev->tx_msg_buffer = kzalloc(CPC_HEADER_SIZE +
  799. sizeof(struct ems_cpc_msg), GFP_KERNEL);
  800. if (!dev->tx_msg_buffer)
  801. goto cleanup_intr_in_buffer;
  802. usb_set_intfdata(intf, dev);
  803. SET_NETDEV_DEV(netdev, &intf->dev);
  804. init_params_sja1000(&dev->active_params);
  805. err = ems_usb_command_msg(dev, &dev->active_params);
  806. if (err) {
  807. netdev_err(netdev, "couldn't initialize controller: %d\n", err);
  808. goto cleanup_tx_msg_buffer;
  809. }
  810. err = register_candev(netdev);
  811. if (err) {
  812. netdev_err(netdev, "couldn't register CAN device: %d\n", err);
  813. goto cleanup_tx_msg_buffer;
  814. }
  815. return 0;
  816. cleanup_tx_msg_buffer:
  817. kfree(dev->tx_msg_buffer);
  818. cleanup_intr_in_buffer:
  819. kfree(dev->intr_in_buffer);
  820. cleanup_intr_urb:
  821. usb_free_urb(dev->intr_urb);
  822. cleanup_candev:
  823. free_candev(netdev);
  824. return err;
  825. }
  826. /*
  827. * called by the usb core when the device is removed from the system
  828. */
  829. static void ems_usb_disconnect(struct usb_interface *intf)
  830. {
  831. struct ems_usb *dev = usb_get_intfdata(intf);
  832. usb_set_intfdata(intf, NULL);
  833. if (dev) {
  834. unregister_netdev(dev->netdev);
  835. free_candev(dev->netdev);
  836. unlink_all_urbs(dev);
  837. usb_free_urb(dev->intr_urb);
  838. kfree(dev->intr_in_buffer);
  839. }
  840. }
  841. /* usb specific object needed to register this driver with the usb subsystem */
  842. static struct usb_driver ems_usb_driver = {
  843. .name = "ems_usb",
  844. .probe = ems_usb_probe,
  845. .disconnect = ems_usb_disconnect,
  846. .id_table = ems_usb_table,
  847. };
  848. module_usb_driver(ems_usb_driver);