Commit | Line | Data |
---|---|---|
0024d8ad BK |
1 | /* |
2 | * CAN driver for "8 devices" USB2CAN converter | |
3 | * | |
4 | * Copyright (C) 2012 Bernd Krumboeck (krumboeck@universalnet.at) | |
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. | |
17 | * | |
18 | * This driver is inspired by the 3.2.0 version of drivers/net/can/usb/ems_usb.c | |
19 | * and drivers/net/can/usb/esd_usb2.c | |
20 | * | |
21 | * Many thanks to Gerhard Bertelsmann (info@gerhard-bertelsmann.de) | |
22 | * for testing and fixing this driver. Also many thanks to "8 devices", | |
23 | * who were very cooperative and answered my questions. | |
24 | */ | |
25 | ||
0024d8ad BK |
26 | #include <linux/signal.h> |
27 | #include <linux/slab.h> | |
28 | #include <linux/module.h> | |
29 | #include <linux/netdevice.h> | |
30 | #include <linux/usb.h> | |
31 | ||
32 | #include <linux/can.h> | |
33 | #include <linux/can/dev.h> | |
34 | #include <linux/can/error.h> | |
e2d5f2c7 | 35 | #include <linux/can/led.h> |
0024d8ad BK |
36 | |
37 | /* driver constants */ | |
38 | #define MAX_RX_URBS 20 | |
39 | #define MAX_TX_URBS 20 | |
40 | #define RX_BUFFER_SIZE 64 | |
41 | ||
42 | /* vendor and product id */ | |
43 | #define USB_8DEV_VENDOR_ID 0x0483 | |
44 | #define USB_8DEV_PRODUCT_ID 0x1234 | |
45 | ||
46 | /* endpoints */ | |
47 | enum usb_8dev_endpoint { | |
48 | USB_8DEV_ENDP_DATA_RX = 1, | |
49 | USB_8DEV_ENDP_DATA_TX, | |
50 | USB_8DEV_ENDP_CMD_RX, | |
51 | USB_8DEV_ENDP_CMD_TX | |
52 | }; | |
53 | ||
54 | /* device CAN clock */ | |
55 | #define USB_8DEV_ABP_CLOCK 32000000 | |
56 | ||
57 | /* setup flags */ | |
58 | #define USB_8DEV_SILENT 0x01 | |
59 | #define USB_8DEV_LOOPBACK 0x02 | |
60 | #define USB_8DEV_DISABLE_AUTO_RESTRANS 0x04 | |
61 | #define USB_8DEV_STATUS_FRAME 0x08 | |
62 | ||
63 | /* commands */ | |
64 | enum usb_8dev_cmd { | |
65 | USB_8DEV_RESET = 1, | |
66 | USB_8DEV_OPEN, | |
67 | USB_8DEV_CLOSE, | |
68 | USB_8DEV_SET_SPEED, | |
69 | USB_8DEV_SET_MASK_FILTER, | |
70 | USB_8DEV_GET_STATUS, | |
71 | USB_8DEV_GET_STATISTICS, | |
72 | USB_8DEV_GET_SERIAL, | |
73 | USB_8DEV_GET_SOFTW_VER, | |
74 | USB_8DEV_GET_HARDW_VER, | |
75 | USB_8DEV_RESET_TIMESTAMP, | |
76 | USB_8DEV_GET_SOFTW_HARDW_VER | |
77 | }; | |
78 | ||
79 | /* command options */ | |
80 | #define USB_8DEV_BAUD_MANUAL 0x09 | |
81 | #define USB_8DEV_CMD_START 0x11 | |
82 | #define USB_8DEV_CMD_END 0x22 | |
83 | ||
84 | #define USB_8DEV_CMD_SUCCESS 0 | |
85 | #define USB_8DEV_CMD_ERROR 255 | |
86 | ||
87 | #define USB_8DEV_CMD_TIMEOUT 1000 | |
88 | ||
89 | /* frames */ | |
90 | #define USB_8DEV_DATA_START 0x55 | |
91 | #define USB_8DEV_DATA_END 0xAA | |
92 | ||
93 | #define USB_8DEV_TYPE_CAN_FRAME 0 | |
94 | #define USB_8DEV_TYPE_ERROR_FRAME 3 | |
95 | ||
96 | #define USB_8DEV_EXTID 0x01 | |
97 | #define USB_8DEV_RTR 0x02 | |
98 | #define USB_8DEV_ERR_FLAG 0x04 | |
99 | ||
100 | /* status */ | |
101 | #define USB_8DEV_STATUSMSG_OK 0x00 /* Normal condition. */ | |
102 | #define USB_8DEV_STATUSMSG_OVERRUN 0x01 /* Overrun occured when sending */ | |
103 | #define USB_8DEV_STATUSMSG_BUSLIGHT 0x02 /* Error counter has reached 96 */ | |
104 | #define USB_8DEV_STATUSMSG_BUSHEAVY 0x03 /* Error count. has reached 128 */ | |
105 | #define USB_8DEV_STATUSMSG_BUSOFF 0x04 /* Device is in BUSOFF */ | |
106 | #define USB_8DEV_STATUSMSG_STUFF 0x20 /* Stuff Error */ | |
107 | #define USB_8DEV_STATUSMSG_FORM 0x21 /* Form Error */ | |
108 | #define USB_8DEV_STATUSMSG_ACK 0x23 /* Ack Error */ | |
109 | #define USB_8DEV_STATUSMSG_BIT0 0x24 /* Bit1 Error */ | |
110 | #define USB_8DEV_STATUSMSG_BIT1 0x25 /* Bit0 Error */ | |
111 | #define USB_8DEV_STATUSMSG_CRC 0x27 /* CRC Error */ | |
112 | ||
113 | #define USB_8DEV_RP_MASK 0x7F /* Mask for Receive Error Bit */ | |
114 | ||
115 | ||
116 | /* table of devices that work with this driver */ | |
117 | static const struct usb_device_id usb_8dev_table[] = { | |
118 | { USB_DEVICE(USB_8DEV_VENDOR_ID, USB_8DEV_PRODUCT_ID) }, | |
119 | { } /* Terminating entry */ | |
120 | }; | |
121 | ||
122 | MODULE_DEVICE_TABLE(usb, usb_8dev_table); | |
123 | ||
124 | struct usb_8dev_tx_urb_context { | |
125 | struct usb_8dev_priv *priv; | |
126 | ||
127 | u32 echo_index; | |
128 | u8 dlc; | |
129 | }; | |
130 | ||
131 | /* Structure to hold all of our device specific stuff */ | |
132 | struct usb_8dev_priv { | |
133 | struct can_priv can; /* must be the first member */ | |
134 | ||
135 | struct sk_buff *echo_skb[MAX_TX_URBS]; | |
136 | ||
137 | struct usb_device *udev; | |
138 | struct net_device *netdev; | |
139 | ||
140 | atomic_t active_tx_urbs; | |
141 | struct usb_anchor tx_submitted; | |
142 | struct usb_8dev_tx_urb_context tx_contexts[MAX_TX_URBS]; | |
143 | ||
144 | struct usb_anchor rx_submitted; | |
145 | ||
146 | struct can_berr_counter bec; | |
147 | ||
148 | u8 *cmd_msg_buffer; | |
149 | ||
150 | struct mutex usb_8dev_cmd_lock; | |
151 | ||
152 | }; | |
153 | ||
154 | /* tx frame */ | |
155 | struct __packed usb_8dev_tx_msg { | |
156 | u8 begin; | |
157 | u8 flags; /* RTR and EXT_ID flag */ | |
158 | __be32 id; /* upper 3 bits not used */ | |
159 | u8 dlc; /* data length code 0-8 bytes */ | |
160 | u8 data[8]; /* 64-bit data */ | |
161 | u8 end; | |
162 | }; | |
163 | ||
164 | /* rx frame */ | |
165 | struct __packed usb_8dev_rx_msg { | |
166 | u8 begin; | |
167 | u8 type; /* frame type */ | |
168 | u8 flags; /* RTR and EXT_ID flag */ | |
169 | __be32 id; /* upper 3 bits not used */ | |
170 | u8 dlc; /* data length code 0-8 bytes */ | |
171 | u8 data[8]; /* 64-bit data */ | |
172 | __be32 timestamp; /* 32-bit timestamp */ | |
173 | u8 end; | |
174 | }; | |
175 | ||
176 | /* command frame */ | |
177 | struct __packed usb_8dev_cmd_msg { | |
178 | u8 begin; | |
179 | u8 channel; /* unkown - always 0 */ | |
180 | u8 command; /* command to execute */ | |
181 | u8 opt1; /* optional parameter / return value */ | |
182 | u8 opt2; /* optional parameter 2 */ | |
183 | u8 data[10]; /* optional parameter and data */ | |
184 | u8 end; | |
185 | }; | |
186 | ||
187 | static int usb_8dev_send_cmd_msg(struct usb_8dev_priv *priv, u8 *msg, int size) | |
188 | { | |
189 | int actual_length; | |
190 | ||
191 | return usb_bulk_msg(priv->udev, | |
192 | usb_sndbulkpipe(priv->udev, USB_8DEV_ENDP_CMD_TX), | |
193 | msg, size, &actual_length, USB_8DEV_CMD_TIMEOUT); | |
194 | } | |
195 | ||
196 | static int usb_8dev_wait_cmd_msg(struct usb_8dev_priv *priv, u8 *msg, int size, | |
197 | int *actual_length) | |
198 | { | |
199 | return usb_bulk_msg(priv->udev, | |
200 | usb_rcvbulkpipe(priv->udev, USB_8DEV_ENDP_CMD_RX), | |
201 | msg, size, actual_length, USB_8DEV_CMD_TIMEOUT); | |
202 | } | |
203 | ||
204 | /* Send command to device and receive result. | |
205 | * Command was successful when opt1 = 0. | |
206 | */ | |
207 | static int usb_8dev_send_cmd(struct usb_8dev_priv *priv, | |
208 | struct usb_8dev_cmd_msg *out, | |
209 | struct usb_8dev_cmd_msg *in) | |
210 | { | |
211 | int err; | |
212 | int num_bytes_read; | |
213 | struct net_device *netdev; | |
214 | ||
215 | netdev = priv->netdev; | |
216 | ||
217 | out->begin = USB_8DEV_CMD_START; | |
218 | out->end = USB_8DEV_CMD_END; | |
219 | ||
220 | mutex_lock(&priv->usb_8dev_cmd_lock); | |
221 | ||
222 | memcpy(priv->cmd_msg_buffer, out, | |
223 | sizeof(struct usb_8dev_cmd_msg)); | |
224 | ||
225 | err = usb_8dev_send_cmd_msg(priv, priv->cmd_msg_buffer, | |
226 | sizeof(struct usb_8dev_cmd_msg)); | |
227 | if (err < 0) { | |
228 | netdev_err(netdev, "sending command message failed\n"); | |
229 | goto failed; | |
230 | } | |
231 | ||
232 | err = usb_8dev_wait_cmd_msg(priv, priv->cmd_msg_buffer, | |
233 | sizeof(struct usb_8dev_cmd_msg), | |
234 | &num_bytes_read); | |
235 | if (err < 0) { | |
236 | netdev_err(netdev, "no command message answer\n"); | |
237 | goto failed; | |
238 | } | |
239 | ||
240 | memcpy(in, priv->cmd_msg_buffer, sizeof(struct usb_8dev_cmd_msg)); | |
241 | ||
242 | if (in->begin != USB_8DEV_CMD_START || in->end != USB_8DEV_CMD_END || | |
243 | num_bytes_read != 16 || in->opt1 != 0) | |
244 | err = -EPROTO; | |
245 | ||
246 | failed: | |
247 | mutex_unlock(&priv->usb_8dev_cmd_lock); | |
248 | return err; | |
249 | } | |
250 | ||
251 | /* Send open command to device */ | |
252 | static int usb_8dev_cmd_open(struct usb_8dev_priv *priv) | |
253 | { | |
254 | struct can_bittiming *bt = &priv->can.bittiming; | |
255 | struct usb_8dev_cmd_msg outmsg; | |
256 | struct usb_8dev_cmd_msg inmsg; | |
257 | u32 ctrlmode = priv->can.ctrlmode; | |
258 | u32 flags = USB_8DEV_STATUS_FRAME; | |
259 | __be32 beflags; | |
260 | __be16 bebrp; | |
261 | ||
262 | memset(&outmsg, 0, sizeof(outmsg)); | |
263 | outmsg.command = USB_8DEV_OPEN; | |
264 | outmsg.opt1 = USB_8DEV_BAUD_MANUAL; | |
265 | outmsg.data[0] = bt->prop_seg + bt->phase_seg1; | |
266 | outmsg.data[1] = bt->phase_seg2; | |
267 | outmsg.data[2] = bt->sjw; | |
268 | ||
269 | /* BRP */ | |
270 | bebrp = cpu_to_be16((u16)bt->brp); | |
271 | memcpy(&outmsg.data[3], &bebrp, sizeof(bebrp)); | |
272 | ||
273 | /* flags */ | |
274 | if (ctrlmode & CAN_CTRLMODE_LOOPBACK) | |
275 | flags |= USB_8DEV_LOOPBACK; | |
276 | if (ctrlmode & CAN_CTRLMODE_LISTENONLY) | |
277 | flags |= USB_8DEV_SILENT; | |
278 | if (ctrlmode & CAN_CTRLMODE_ONE_SHOT) | |
279 | flags |= USB_8DEV_DISABLE_AUTO_RESTRANS; | |
280 | ||
281 | beflags = cpu_to_be32(flags); | |
282 | memcpy(&outmsg.data[5], &beflags, sizeof(beflags)); | |
283 | ||
284 | return usb_8dev_send_cmd(priv, &outmsg, &inmsg); | |
285 | } | |
286 | ||
287 | /* Send close command to device */ | |
288 | static int usb_8dev_cmd_close(struct usb_8dev_priv *priv) | |
289 | { | |
290 | struct usb_8dev_cmd_msg inmsg; | |
291 | struct usb_8dev_cmd_msg outmsg = { | |
292 | .channel = 0, | |
293 | .command = USB_8DEV_CLOSE, | |
294 | .opt1 = 0, | |
295 | .opt2 = 0 | |
296 | }; | |
297 | ||
298 | return usb_8dev_send_cmd(priv, &outmsg, &inmsg); | |
299 | } | |
300 | ||
301 | /* Get firmware and hardware version */ | |
302 | static int usb_8dev_cmd_version(struct usb_8dev_priv *priv, u32 *res) | |
303 | { | |
304 | struct usb_8dev_cmd_msg inmsg; | |
305 | struct usb_8dev_cmd_msg outmsg = { | |
306 | .channel = 0, | |
307 | .command = USB_8DEV_GET_SOFTW_HARDW_VER, | |
308 | .opt1 = 0, | |
309 | .opt2 = 0 | |
310 | }; | |
311 | ||
312 | int err = usb_8dev_send_cmd(priv, &outmsg, &inmsg); | |
313 | if (err) | |
314 | return err; | |
315 | ||
316 | *res = be32_to_cpup((__be32 *)inmsg.data); | |
317 | ||
318 | return err; | |
319 | } | |
320 | ||
321 | /* Set network device mode | |
322 | * | |
323 | * Maybe we should leave this function empty, because the device | |
324 | * set mode variable with open command. | |
325 | */ | |
326 | static int usb_8dev_set_mode(struct net_device *netdev, enum can_mode mode) | |
327 | { | |
328 | struct usb_8dev_priv *priv = netdev_priv(netdev); | |
329 | int err = 0; | |
330 | ||
331 | switch (mode) { | |
332 | case CAN_MODE_START: | |
333 | err = usb_8dev_cmd_open(priv); | |
334 | if (err) | |
335 | netdev_warn(netdev, "couldn't start device"); | |
336 | break; | |
337 | ||
338 | default: | |
339 | return -EOPNOTSUPP; | |
340 | } | |
341 | ||
342 | return err; | |
343 | } | |
344 | ||
345 | /* Read error/status frames */ | |
346 | static void usb_8dev_rx_err_msg(struct usb_8dev_priv *priv, | |
347 | struct usb_8dev_rx_msg *msg) | |
348 | { | |
349 | struct can_frame *cf; | |
350 | struct sk_buff *skb; | |
351 | struct net_device_stats *stats = &priv->netdev->stats; | |
352 | ||
353 | /* Error message: | |
354 | * byte 0: Status | |
355 | * byte 1: bit 7: Receive Passive | |
356 | * byte 1: bit 0-6: Receive Error Counter | |
357 | * byte 2: Transmit Error Counter | |
358 | * byte 3: Always 0 (maybe reserved for future use) | |
359 | */ | |
360 | ||
361 | u8 state = msg->data[0]; | |
362 | u8 rxerr = msg->data[1] & USB_8DEV_RP_MASK; | |
363 | u8 txerr = msg->data[2]; | |
364 | int rx_errors = 0; | |
365 | int tx_errors = 0; | |
366 | ||
367 | skb = alloc_can_err_skb(priv->netdev, &cf); | |
368 | if (!skb) | |
369 | return; | |
370 | ||
371 | switch (state) { | |
372 | case USB_8DEV_STATUSMSG_OK: | |
373 | priv->can.state = CAN_STATE_ERROR_ACTIVE; | |
374 | cf->can_id |= CAN_ERR_PROT; | |
375 | cf->data[2] = CAN_ERR_PROT_ACTIVE; | |
376 | break; | |
377 | case USB_8DEV_STATUSMSG_BUSOFF: | |
378 | priv->can.state = CAN_STATE_BUS_OFF; | |
379 | cf->can_id |= CAN_ERR_BUSOFF; | |
be38a6f9 | 380 | priv->can.can_stats.bus_off++; |
0024d8ad BK |
381 | can_bus_off(priv->netdev); |
382 | break; | |
383 | case USB_8DEV_STATUSMSG_OVERRUN: | |
384 | case USB_8DEV_STATUSMSG_BUSLIGHT: | |
385 | case USB_8DEV_STATUSMSG_BUSHEAVY: | |
386 | cf->can_id |= CAN_ERR_CRTL; | |
387 | break; | |
388 | default: | |
389 | priv->can.state = CAN_STATE_ERROR_WARNING; | |
390 | cf->can_id |= CAN_ERR_PROT | CAN_ERR_BUSERROR; | |
391 | priv->can.can_stats.bus_error++; | |
392 | break; | |
393 | } | |
394 | ||
395 | switch (state) { | |
396 | case USB_8DEV_STATUSMSG_OK: | |
397 | case USB_8DEV_STATUSMSG_BUSOFF: | |
398 | break; | |
399 | case USB_8DEV_STATUSMSG_ACK: | |
400 | cf->can_id |= CAN_ERR_ACK; | |
401 | tx_errors = 1; | |
402 | break; | |
403 | case USB_8DEV_STATUSMSG_CRC: | |
ffd461f8 | 404 | cf->data[3] = CAN_ERR_PROT_LOC_CRC_SEQ; |
0024d8ad BK |
405 | rx_errors = 1; |
406 | break; | |
407 | case USB_8DEV_STATUSMSG_BIT0: | |
408 | cf->data[2] |= CAN_ERR_PROT_BIT0; | |
409 | tx_errors = 1; | |
410 | break; | |
411 | case USB_8DEV_STATUSMSG_BIT1: | |
412 | cf->data[2] |= CAN_ERR_PROT_BIT1; | |
413 | tx_errors = 1; | |
414 | break; | |
415 | case USB_8DEV_STATUSMSG_FORM: | |
416 | cf->data[2] |= CAN_ERR_PROT_FORM; | |
417 | rx_errors = 1; | |
418 | break; | |
419 | case USB_8DEV_STATUSMSG_STUFF: | |
420 | cf->data[2] |= CAN_ERR_PROT_STUFF; | |
421 | rx_errors = 1; | |
422 | break; | |
423 | case USB_8DEV_STATUSMSG_OVERRUN: | |
424 | cf->data[1] = CAN_ERR_CRTL_RX_OVERFLOW; | |
425 | stats->rx_over_errors++; | |
426 | rx_errors = 1; | |
427 | break; | |
428 | case USB_8DEV_STATUSMSG_BUSLIGHT: | |
429 | priv->can.state = CAN_STATE_ERROR_WARNING; | |
430 | cf->data[1] = (txerr > rxerr) ? | |
431 | CAN_ERR_CRTL_TX_WARNING : | |
432 | CAN_ERR_CRTL_RX_WARNING; | |
433 | priv->can.can_stats.error_warning++; | |
434 | break; | |
435 | case USB_8DEV_STATUSMSG_BUSHEAVY: | |
436 | priv->can.state = CAN_STATE_ERROR_PASSIVE; | |
437 | cf->data[1] = (txerr > rxerr) ? | |
438 | CAN_ERR_CRTL_TX_PASSIVE : | |
439 | CAN_ERR_CRTL_RX_PASSIVE; | |
440 | priv->can.can_stats.error_passive++; | |
441 | break; | |
442 | default: | |
443 | netdev_warn(priv->netdev, | |
444 | "Unknown status/error message (%d)\n", state); | |
445 | break; | |
446 | } | |
447 | ||
448 | if (tx_errors) { | |
449 | cf->data[2] |= CAN_ERR_PROT_TX; | |
450 | stats->tx_errors++; | |
451 | } | |
452 | ||
453 | if (rx_errors) | |
454 | stats->rx_errors++; | |
455 | ||
456 | cf->data[6] = txerr; | |
457 | cf->data[7] = rxerr; | |
458 | ||
459 | priv->bec.txerr = txerr; | |
460 | priv->bec.rxerr = rxerr; | |
461 | ||
0024d8ad BK |
462 | stats->rx_packets++; |
463 | stats->rx_bytes += cf->can_dlc; | |
9b721a4c | 464 | netif_rx(skb); |
0024d8ad BK |
465 | } |
466 | ||
467 | /* Read data and status frames */ | |
468 | static void usb_8dev_rx_can_msg(struct usb_8dev_priv *priv, | |
469 | struct usb_8dev_rx_msg *msg) | |
470 | { | |
471 | struct can_frame *cf; | |
472 | struct sk_buff *skb; | |
473 | struct net_device_stats *stats = &priv->netdev->stats; | |
474 | ||
475 | if (msg->type == USB_8DEV_TYPE_ERROR_FRAME && | |
476 | msg->flags == USB_8DEV_ERR_FLAG) { | |
477 | usb_8dev_rx_err_msg(priv, msg); | |
478 | } else if (msg->type == USB_8DEV_TYPE_CAN_FRAME) { | |
479 | skb = alloc_can_skb(priv->netdev, &cf); | |
480 | if (!skb) | |
481 | return; | |
482 | ||
483 | cf->can_id = be32_to_cpu(msg->id); | |
484 | cf->can_dlc = get_can_dlc(msg->dlc & 0xF); | |
485 | ||
486 | if (msg->flags & USB_8DEV_EXTID) | |
487 | cf->can_id |= CAN_EFF_FLAG; | |
488 | ||
489 | if (msg->flags & USB_8DEV_RTR) | |
490 | cf->can_id |= CAN_RTR_FLAG; | |
491 | else | |
492 | memcpy(cf->data, msg->data, cf->can_dlc); | |
493 | ||
0024d8ad BK |
494 | stats->rx_packets++; |
495 | stats->rx_bytes += cf->can_dlc; | |
9b721a4c | 496 | netif_rx(skb); |
e2d5f2c7 BK |
497 | |
498 | can_led_event(priv->netdev, CAN_LED_EVENT_RX); | |
0024d8ad BK |
499 | } else { |
500 | netdev_warn(priv->netdev, "frame type %d unknown", | |
501 | msg->type); | |
502 | } | |
503 | ||
504 | } | |
505 | ||
506 | /* Callback for reading data from device | |
507 | * | |
508 | * Check urb status, call read function and resubmit urb read operation. | |
509 | */ | |
510 | static void usb_8dev_read_bulk_callback(struct urb *urb) | |
511 | { | |
512 | struct usb_8dev_priv *priv = urb->context; | |
513 | struct net_device *netdev; | |
514 | int retval; | |
515 | int pos = 0; | |
516 | ||
517 | netdev = priv->netdev; | |
518 | ||
519 | if (!netif_device_present(netdev)) | |
520 | return; | |
521 | ||
522 | switch (urb->status) { | |
523 | case 0: /* success */ | |
524 | break; | |
525 | ||
526 | case -ENOENT: | |
527 | case -ESHUTDOWN: | |
528 | return; | |
529 | ||
530 | default: | |
531 | netdev_info(netdev, "Rx URB aborted (%d)\n", | |
532 | urb->status); | |
533 | goto resubmit_urb; | |
534 | } | |
535 | ||
536 | while (pos < urb->actual_length) { | |
537 | struct usb_8dev_rx_msg *msg; | |
538 | ||
539 | if (pos + sizeof(struct usb_8dev_rx_msg) > urb->actual_length) { | |
540 | netdev_err(priv->netdev, "format error\n"); | |
541 | break; | |
542 | } | |
543 | ||
544 | msg = (struct usb_8dev_rx_msg *)(urb->transfer_buffer + pos); | |
545 | usb_8dev_rx_can_msg(priv, msg); | |
546 | ||
547 | pos += sizeof(struct usb_8dev_rx_msg); | |
548 | } | |
549 | ||
550 | resubmit_urb: | |
551 | usb_fill_bulk_urb(urb, priv->udev, | |
552 | usb_rcvbulkpipe(priv->udev, USB_8DEV_ENDP_DATA_RX), | |
553 | urb->transfer_buffer, RX_BUFFER_SIZE, | |
554 | usb_8dev_read_bulk_callback, priv); | |
555 | ||
556 | retval = usb_submit_urb(urb, GFP_ATOMIC); | |
557 | ||
558 | if (retval == -ENODEV) | |
559 | netif_device_detach(netdev); | |
560 | else if (retval) | |
561 | netdev_err(netdev, | |
562 | "failed resubmitting read bulk urb: %d\n", retval); | |
563 | } | |
564 | ||
565 | /* Callback handler for write operations | |
566 | * | |
567 | * Free allocated buffers, check transmit status and | |
568 | * calculate statistic. | |
569 | */ | |
570 | static void usb_8dev_write_bulk_callback(struct urb *urb) | |
571 | { | |
572 | struct usb_8dev_tx_urb_context *context = urb->context; | |
573 | struct usb_8dev_priv *priv; | |
574 | struct net_device *netdev; | |
575 | ||
576 | BUG_ON(!context); | |
577 | ||
578 | priv = context->priv; | |
579 | netdev = priv->netdev; | |
580 | ||
581 | /* free up our allocated buffer */ | |
582 | usb_free_coherent(urb->dev, urb->transfer_buffer_length, | |
583 | urb->transfer_buffer, urb->transfer_dma); | |
584 | ||
585 | atomic_dec(&priv->active_tx_urbs); | |
586 | ||
587 | if (!netif_device_present(netdev)) | |
588 | return; | |
589 | ||
590 | if (urb->status) | |
591 | netdev_info(netdev, "Tx URB aborted (%d)\n", | |
592 | urb->status); | |
593 | ||
594 | netdev->stats.tx_packets++; | |
595 | netdev->stats.tx_bytes += context->dlc; | |
596 | ||
597 | can_get_echo_skb(netdev, context->echo_index); | |
598 | ||
e2d5f2c7 BK |
599 | can_led_event(netdev, CAN_LED_EVENT_TX); |
600 | ||
0024d8ad BK |
601 | /* Release context */ |
602 | context->echo_index = MAX_TX_URBS; | |
603 | ||
604 | netif_wake_queue(netdev); | |
605 | } | |
606 | ||
607 | /* Send data to device */ | |
608 | static netdev_tx_t usb_8dev_start_xmit(struct sk_buff *skb, | |
609 | struct net_device *netdev) | |
610 | { | |
611 | struct usb_8dev_priv *priv = netdev_priv(netdev); | |
612 | struct net_device_stats *stats = &netdev->stats; | |
613 | struct can_frame *cf = (struct can_frame *) skb->data; | |
614 | struct usb_8dev_tx_msg *msg; | |
615 | struct urb *urb; | |
616 | struct usb_8dev_tx_urb_context *context = NULL; | |
617 | u8 *buf; | |
618 | int i, err; | |
619 | size_t size = sizeof(struct usb_8dev_tx_msg); | |
620 | ||
621 | if (can_dropped_invalid_skb(netdev, skb)) | |
622 | return NETDEV_TX_OK; | |
623 | ||
624 | /* create a URB, and a buffer for it, and copy the data to the URB */ | |
625 | urb = usb_alloc_urb(0, GFP_ATOMIC); | |
7fe7cfa4 | 626 | if (!urb) |
0024d8ad | 627 | goto nomem; |
0024d8ad BK |
628 | |
629 | buf = usb_alloc_coherent(priv->udev, size, GFP_ATOMIC, | |
630 | &urb->transfer_dma); | |
631 | if (!buf) { | |
632 | netdev_err(netdev, "No memory left for USB buffer\n"); | |
633 | goto nomembuf; | |
634 | } | |
635 | ||
636 | memset(buf, 0, size); | |
637 | ||
638 | msg = (struct usb_8dev_tx_msg *)buf; | |
639 | msg->begin = USB_8DEV_DATA_START; | |
640 | msg->flags = 0x00; | |
641 | ||
642 | if (cf->can_id & CAN_RTR_FLAG) | |
643 | msg->flags |= USB_8DEV_RTR; | |
644 | ||
645 | if (cf->can_id & CAN_EFF_FLAG) | |
646 | msg->flags |= USB_8DEV_EXTID; | |
647 | ||
648 | msg->id = cpu_to_be32(cf->can_id & CAN_ERR_MASK); | |
649 | msg->dlc = cf->can_dlc; | |
650 | memcpy(msg->data, cf->data, cf->can_dlc); | |
651 | msg->end = USB_8DEV_DATA_END; | |
652 | ||
653 | for (i = 0; i < MAX_TX_URBS; i++) { | |
654 | if (priv->tx_contexts[i].echo_index == MAX_TX_URBS) { | |
655 | context = &priv->tx_contexts[i]; | |
656 | break; | |
657 | } | |
658 | } | |
659 | ||
660 | /* May never happen! When this happens we'd more URBs in flight as | |
661 | * allowed (MAX_TX_URBS). | |
662 | */ | |
663 | if (!context) | |
664 | goto nofreecontext; | |
665 | ||
666 | context->priv = priv; | |
667 | context->echo_index = i; | |
668 | context->dlc = cf->can_dlc; | |
669 | ||
670 | usb_fill_bulk_urb(urb, priv->udev, | |
671 | usb_sndbulkpipe(priv->udev, USB_8DEV_ENDP_DATA_TX), | |
672 | buf, size, usb_8dev_write_bulk_callback, context); | |
673 | urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP; | |
674 | usb_anchor_urb(urb, &priv->tx_submitted); | |
675 | ||
676 | can_put_echo_skb(skb, netdev, context->echo_index); | |
677 | ||
678 | atomic_inc(&priv->active_tx_urbs); | |
679 | ||
680 | err = usb_submit_urb(urb, GFP_ATOMIC); | |
681 | if (unlikely(err)) | |
682 | goto failed; | |
683 | else if (atomic_read(&priv->active_tx_urbs) >= MAX_TX_URBS) | |
684 | /* Slow down tx path */ | |
685 | netif_stop_queue(netdev); | |
686 | ||
687 | /* Release our reference to this URB, the USB core will eventually free | |
688 | * it entirely. | |
689 | */ | |
690 | usb_free_urb(urb); | |
691 | ||
692 | return NETDEV_TX_OK; | |
693 | ||
694 | nofreecontext: | |
0024d8ad | 695 | usb_free_coherent(priv->udev, size, buf, urb->transfer_dma); |
636d0375 | 696 | usb_free_urb(urb); |
0024d8ad BK |
697 | |
698 | netdev_warn(netdev, "couldn't find free context"); | |
699 | ||
700 | return NETDEV_TX_BUSY; | |
701 | ||
702 | failed: | |
703 | can_free_echo_skb(netdev, context->echo_index); | |
704 | ||
705 | usb_unanchor_urb(urb); | |
706 | usb_free_coherent(priv->udev, size, buf, urb->transfer_dma); | |
707 | ||
708 | atomic_dec(&priv->active_tx_urbs); | |
709 | ||
710 | if (err == -ENODEV) | |
711 | netif_device_detach(netdev); | |
712 | else | |
713 | netdev_warn(netdev, "failed tx_urb %d\n", err); | |
714 | ||
715 | nomembuf: | |
716 | usb_free_urb(urb); | |
717 | ||
718 | nomem: | |
719 | dev_kfree_skb(skb); | |
720 | stats->tx_dropped++; | |
721 | ||
722 | return NETDEV_TX_OK; | |
723 | } | |
724 | ||
725 | static int usb_8dev_get_berr_counter(const struct net_device *netdev, | |
726 | struct can_berr_counter *bec) | |
727 | { | |
728 | struct usb_8dev_priv *priv = netdev_priv(netdev); | |
729 | ||
730 | bec->txerr = priv->bec.txerr; | |
731 | bec->rxerr = priv->bec.rxerr; | |
732 | ||
733 | return 0; | |
734 | } | |
735 | ||
736 | /* Start USB device */ | |
737 | static int usb_8dev_start(struct usb_8dev_priv *priv) | |
738 | { | |
739 | struct net_device *netdev = priv->netdev; | |
740 | int err, i; | |
741 | ||
742 | for (i = 0; i < MAX_RX_URBS; i++) { | |
743 | struct urb *urb = NULL; | |
744 | u8 *buf; | |
745 | ||
746 | /* create a URB, and a buffer for it */ | |
747 | urb = usb_alloc_urb(0, GFP_KERNEL); | |
748 | if (!urb) { | |
0024d8ad BK |
749 | err = -ENOMEM; |
750 | break; | |
751 | } | |
752 | ||
753 | buf = usb_alloc_coherent(priv->udev, RX_BUFFER_SIZE, GFP_KERNEL, | |
754 | &urb->transfer_dma); | |
755 | if (!buf) { | |
756 | netdev_err(netdev, "No memory left for USB buffer\n"); | |
757 | usb_free_urb(urb); | |
758 | err = -ENOMEM; | |
759 | break; | |
760 | } | |
761 | ||
762 | usb_fill_bulk_urb(urb, priv->udev, | |
763 | usb_rcvbulkpipe(priv->udev, | |
764 | USB_8DEV_ENDP_DATA_RX), | |
765 | buf, RX_BUFFER_SIZE, | |
766 | usb_8dev_read_bulk_callback, priv); | |
767 | urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP; | |
768 | usb_anchor_urb(urb, &priv->rx_submitted); | |
769 | ||
770 | err = usb_submit_urb(urb, GFP_KERNEL); | |
771 | if (err) { | |
772 | usb_unanchor_urb(urb); | |
773 | usb_free_coherent(priv->udev, RX_BUFFER_SIZE, buf, | |
774 | urb->transfer_dma); | |
76719868 | 775 | usb_free_urb(urb); |
0024d8ad BK |
776 | break; |
777 | } | |
778 | ||
779 | /* Drop reference, USB core will take care of freeing it */ | |
780 | usb_free_urb(urb); | |
781 | } | |
782 | ||
783 | /* Did we submit any URBs */ | |
784 | if (i == 0) { | |
785 | netdev_warn(netdev, "couldn't setup read URBs\n"); | |
786 | return err; | |
787 | } | |
788 | ||
789 | /* Warn if we've couldn't transmit all the URBs */ | |
790 | if (i < MAX_RX_URBS) | |
791 | netdev_warn(netdev, "rx performance may be slow\n"); | |
792 | ||
793 | err = usb_8dev_cmd_open(priv); | |
794 | if (err) | |
795 | goto failed; | |
796 | ||
797 | priv->can.state = CAN_STATE_ERROR_ACTIVE; | |
798 | ||
799 | return 0; | |
800 | ||
801 | failed: | |
802 | if (err == -ENODEV) | |
803 | netif_device_detach(priv->netdev); | |
804 | ||
805 | netdev_warn(netdev, "couldn't submit control: %d\n", err); | |
806 | ||
807 | return err; | |
808 | } | |
809 | ||
810 | /* Open USB device */ | |
811 | static int usb_8dev_open(struct net_device *netdev) | |
812 | { | |
813 | struct usb_8dev_priv *priv = netdev_priv(netdev); | |
814 | int err; | |
815 | ||
816 | /* common open */ | |
817 | err = open_candev(netdev); | |
818 | if (err) | |
819 | return err; | |
820 | ||
e2d5f2c7 BK |
821 | can_led_event(netdev, CAN_LED_EVENT_OPEN); |
822 | ||
0024d8ad BK |
823 | /* finally start device */ |
824 | err = usb_8dev_start(priv); | |
825 | if (err) { | |
826 | if (err == -ENODEV) | |
827 | netif_device_detach(priv->netdev); | |
828 | ||
829 | netdev_warn(netdev, "couldn't start device: %d\n", | |
830 | err); | |
831 | ||
832 | close_candev(netdev); | |
833 | ||
834 | return err; | |
835 | } | |
836 | ||
837 | netif_start_queue(netdev); | |
838 | ||
839 | return 0; | |
840 | } | |
841 | ||
842 | static void unlink_all_urbs(struct usb_8dev_priv *priv) | |
843 | { | |
844 | int i; | |
845 | ||
846 | usb_kill_anchored_urbs(&priv->rx_submitted); | |
847 | ||
848 | usb_kill_anchored_urbs(&priv->tx_submitted); | |
849 | atomic_set(&priv->active_tx_urbs, 0); | |
850 | ||
851 | for (i = 0; i < MAX_TX_URBS; i++) | |
852 | priv->tx_contexts[i].echo_index = MAX_TX_URBS; | |
853 | } | |
854 | ||
855 | /* Close USB device */ | |
856 | static int usb_8dev_close(struct net_device *netdev) | |
857 | { | |
858 | struct usb_8dev_priv *priv = netdev_priv(netdev); | |
859 | int err = 0; | |
860 | ||
861 | /* Send CLOSE command to CAN controller */ | |
862 | err = usb_8dev_cmd_close(priv); | |
863 | if (err) | |
864 | netdev_warn(netdev, "couldn't stop device"); | |
865 | ||
866 | priv->can.state = CAN_STATE_STOPPED; | |
867 | ||
868 | netif_stop_queue(netdev); | |
869 | ||
870 | /* Stop polling */ | |
871 | unlink_all_urbs(priv); | |
872 | ||
873 | close_candev(netdev); | |
874 | ||
e2d5f2c7 BK |
875 | can_led_event(netdev, CAN_LED_EVENT_STOP); |
876 | ||
0024d8ad BK |
877 | return err; |
878 | } | |
879 | ||
880 | static const struct net_device_ops usb_8dev_netdev_ops = { | |
881 | .ndo_open = usb_8dev_open, | |
882 | .ndo_stop = usb_8dev_close, | |
883 | .ndo_start_xmit = usb_8dev_start_xmit, | |
c971fa2a | 884 | .ndo_change_mtu = can_change_mtu, |
0024d8ad BK |
885 | }; |
886 | ||
887 | static const struct can_bittiming_const usb_8dev_bittiming_const = { | |
888 | .name = "usb_8dev", | |
889 | .tseg1_min = 1, | |
890 | .tseg1_max = 16, | |
891 | .tseg2_min = 1, | |
892 | .tseg2_max = 8, | |
893 | .sjw_max = 4, | |
894 | .brp_min = 1, | |
895 | .brp_max = 1024, | |
896 | .brp_inc = 1, | |
897 | }; | |
898 | ||
899 | /* Probe USB device | |
900 | * | |
901 | * Check device and firmware. | |
902 | * Set supported modes and bittiming constants. | |
903 | * Allocate some memory. | |
904 | */ | |
905 | static int usb_8dev_probe(struct usb_interface *intf, | |
906 | const struct usb_device_id *id) | |
907 | { | |
908 | struct net_device *netdev; | |
909 | struct usb_8dev_priv *priv; | |
910 | int i, err = -ENOMEM; | |
911 | u32 version; | |
912 | char buf[18]; | |
913 | struct usb_device *usbdev = interface_to_usbdev(intf); | |
914 | ||
915 | /* product id looks strange, better we also check iProduct string */ | |
916 | if (usb_string(usbdev, usbdev->descriptor.iProduct, buf, | |
917 | sizeof(buf)) > 0 && strcmp(buf, "USB2CAN converter")) { | |
918 | dev_info(&usbdev->dev, "ignoring: not an USB2CAN converter\n"); | |
919 | return -ENODEV; | |
920 | } | |
921 | ||
922 | netdev = alloc_candev(sizeof(struct usb_8dev_priv), MAX_TX_URBS); | |
923 | if (!netdev) { | |
924 | dev_err(&intf->dev, "Couldn't alloc candev\n"); | |
925 | return -ENOMEM; | |
926 | } | |
927 | ||
928 | priv = netdev_priv(netdev); | |
929 | ||
930 | priv->udev = usbdev; | |
931 | priv->netdev = netdev; | |
932 | ||
933 | priv->can.state = CAN_STATE_STOPPED; | |
934 | priv->can.clock.freq = USB_8DEV_ABP_CLOCK; | |
935 | priv->can.bittiming_const = &usb_8dev_bittiming_const; | |
936 | priv->can.do_set_mode = usb_8dev_set_mode; | |
937 | priv->can.do_get_berr_counter = usb_8dev_get_berr_counter; | |
938 | priv->can.ctrlmode_supported = CAN_CTRLMODE_LOOPBACK | | |
939 | CAN_CTRLMODE_LISTENONLY | | |
940 | CAN_CTRLMODE_ONE_SHOT; | |
941 | ||
942 | netdev->netdev_ops = &usb_8dev_netdev_ops; | |
943 | ||
944 | netdev->flags |= IFF_ECHO; /* we support local echo */ | |
945 | ||
946 | init_usb_anchor(&priv->rx_submitted); | |
947 | ||
948 | init_usb_anchor(&priv->tx_submitted); | |
949 | atomic_set(&priv->active_tx_urbs, 0); | |
950 | ||
951 | for (i = 0; i < MAX_TX_URBS; i++) | |
952 | priv->tx_contexts[i].echo_index = MAX_TX_URBS; | |
953 | ||
954 | priv->cmd_msg_buffer = kzalloc(sizeof(struct usb_8dev_cmd_msg), | |
955 | GFP_KERNEL); | |
09da6c5f | 956 | if (!priv->cmd_msg_buffer) |
0024d8ad | 957 | goto cleanup_candev; |
0024d8ad BK |
958 | |
959 | usb_set_intfdata(intf, priv); | |
960 | ||
961 | SET_NETDEV_DEV(netdev, &intf->dev); | |
962 | ||
963 | mutex_init(&priv->usb_8dev_cmd_lock); | |
964 | ||
965 | err = register_candev(netdev); | |
966 | if (err) { | |
967 | netdev_err(netdev, | |
968 | "couldn't register CAN device: %d\n", err); | |
969 | goto cleanup_cmd_msg_buffer; | |
970 | } | |
971 | ||
972 | err = usb_8dev_cmd_version(priv, &version); | |
973 | if (err) { | |
974 | netdev_err(netdev, "can't get firmware version\n"); | |
4afe2156 | 975 | goto cleanup_unregister_candev; |
0024d8ad BK |
976 | } else { |
977 | netdev_info(netdev, | |
978 | "firmware: %d.%d, hardware: %d.%d\n", | |
979 | (version>>24) & 0xff, (version>>16) & 0xff, | |
980 | (version>>8) & 0xff, version & 0xff); | |
981 | } | |
982 | ||
e2d5f2c7 BK |
983 | devm_can_led_init(netdev); |
984 | ||
0024d8ad BK |
985 | return 0; |
986 | ||
4afe2156 MKB |
987 | cleanup_unregister_candev: |
988 | unregister_netdev(priv->netdev); | |
989 | ||
0024d8ad BK |
990 | cleanup_cmd_msg_buffer: |
991 | kfree(priv->cmd_msg_buffer); | |
992 | ||
993 | cleanup_candev: | |
994 | free_candev(netdev); | |
995 | ||
996 | return err; | |
997 | ||
998 | } | |
999 | ||
1000 | /* Called by the usb core when driver is unloaded or device is removed */ | |
1001 | static void usb_8dev_disconnect(struct usb_interface *intf) | |
1002 | { | |
1003 | struct usb_8dev_priv *priv = usb_get_intfdata(intf); | |
1004 | ||
1005 | usb_set_intfdata(intf, NULL); | |
1006 | ||
1007 | if (priv) { | |
1008 | netdev_info(priv->netdev, "device disconnected\n"); | |
1009 | ||
1010 | unregister_netdev(priv->netdev); | |
1011 | free_candev(priv->netdev); | |
1012 | ||
1013 | unlink_all_urbs(priv); | |
1014 | } | |
1015 | ||
1016 | } | |
1017 | ||
1018 | static struct usb_driver usb_8dev_driver = { | |
1019 | .name = "usb_8dev", | |
1020 | .probe = usb_8dev_probe, | |
1021 | .disconnect = usb_8dev_disconnect, | |
1022 | .id_table = usb_8dev_table, | |
1023 | }; | |
1024 | ||
1025 | module_usb_driver(usb_8dev_driver); | |
1026 | ||
1027 | MODULE_AUTHOR("Bernd Krumboeck <krumboeck@universalnet.at>"); | |
1028 | MODULE_DESCRIPTION("CAN driver for 8 devices USB2CAN interfaces"); | |
1029 | MODULE_LICENSE("GPL v2"); |