Commit | Line | Data |
---|---|---|
96d8e903 | 1 | /* |
7653ebd5 | 2 | * CAN driver for esd CAN-USB/2 and CAN-USB/Micro |
96d8e903 | 3 | * |
7653ebd5 | 4 | * Copyright (C) 2010-2012 Matthias Fuchs <matthias.fuchs@esd.eu>, esd gmbh |
96d8e903 MF |
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 | */ | |
96d8e903 MF |
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 | ||
25 | #include <linux/can.h> | |
26 | #include <linux/can/dev.h> | |
27 | #include <linux/can/error.h> | |
28 | ||
29 | MODULE_AUTHOR("Matthias Fuchs <matthias.fuchs@esd.eu>"); | |
7653ebd5 | 30 | MODULE_DESCRIPTION("CAN driver for esd CAN-USB/2 and CAN-USB/Micro interfaces"); |
96d8e903 MF |
31 | MODULE_LICENSE("GPL v2"); |
32 | ||
33 | /* Define these values to match your devices */ | |
34 | #define USB_ESDGMBH_VENDOR_ID 0x0ab4 | |
35 | #define USB_CANUSB2_PRODUCT_ID 0x0010 | |
7653ebd5 | 36 | #define USB_CANUSBM_PRODUCT_ID 0x0011 |
96d8e903 MF |
37 | |
38 | #define ESD_USB2_CAN_CLOCK 60000000 | |
7653ebd5 | 39 | #define ESD_USBM_CAN_CLOCK 36000000 |
96d8e903 MF |
40 | #define ESD_USB2_MAX_NETS 2 |
41 | ||
42 | /* USB2 commands */ | |
43 | #define CMD_VERSION 1 /* also used for VERSION_REPLY */ | |
44 | #define CMD_CAN_RX 2 /* device to host only */ | |
45 | #define CMD_CAN_TX 3 /* also used for TX_DONE */ | |
46 | #define CMD_SETBAUD 4 /* also used for SETBAUD_REPLY */ | |
47 | #define CMD_TS 5 /* also used for TS_REPLY */ | |
48 | #define CMD_IDADD 6 /* also used for IDADD_REPLY */ | |
49 | ||
50 | /* esd CAN message flags - dlc field */ | |
51 | #define ESD_RTR 0x10 | |
52 | ||
53 | /* esd CAN message flags - id field */ | |
54 | #define ESD_EXTID 0x20000000 | |
55 | #define ESD_EVENT 0x40000000 | |
56 | #define ESD_IDMASK 0x1fffffff | |
57 | ||
58 | /* esd CAN event ids used by this driver */ | |
59 | #define ESD_EV_CAN_ERROR_EXT 2 | |
60 | ||
61 | /* baudrate message flags */ | |
62 | #define ESD_USB2_UBR 0x80000000 | |
63 | #define ESD_USB2_LOM 0x40000000 | |
64 | #define ESD_USB2_NO_BAUDRATE 0x7fffffff | |
65 | #define ESD_USB2_TSEG1_MIN 1 | |
66 | #define ESD_USB2_TSEG1_MAX 16 | |
67 | #define ESD_USB2_TSEG1_SHIFT 16 | |
68 | #define ESD_USB2_TSEG2_MIN 1 | |
69 | #define ESD_USB2_TSEG2_MAX 8 | |
70 | #define ESD_USB2_TSEG2_SHIFT 20 | |
71 | #define ESD_USB2_SJW_MAX 4 | |
72 | #define ESD_USB2_SJW_SHIFT 14 | |
7653ebd5 | 73 | #define ESD_USBM_SJW_SHIFT 24 |
96d8e903 MF |
74 | #define ESD_USB2_BRP_MIN 1 |
75 | #define ESD_USB2_BRP_MAX 1024 | |
76 | #define ESD_USB2_BRP_INC 1 | |
77 | #define ESD_USB2_3_SAMPLES 0x00800000 | |
78 | ||
79 | /* esd IDADD message */ | |
80 | #define ESD_ID_ENABLE 0x80 | |
81 | #define ESD_MAX_ID_SEGMENT 64 | |
82 | ||
83 | /* SJA1000 ECC register (emulated by usb2 firmware) */ | |
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 | ||
92 | /* esd bus state event codes */ | |
93 | #define ESD_BUSSTATE_MASK 0xc0 | |
94 | #define ESD_BUSSTATE_WARN 0x40 | |
95 | #define ESD_BUSSTATE_ERRPASSIVE 0x80 | |
96 | #define ESD_BUSSTATE_BUSOFF 0xc0 | |
97 | ||
98 | #define RX_BUFFER_SIZE 1024 | |
99 | #define MAX_RX_URBS 4 | |
100 | #define MAX_TX_URBS 16 /* must be power of 2 */ | |
101 | ||
102 | struct header_msg { | |
103 | u8 len; /* len is always the total message length in 32bit words */ | |
104 | u8 cmd; | |
105 | u8 rsvd[2]; | |
106 | }; | |
107 | ||
108 | struct version_msg { | |
109 | u8 len; | |
110 | u8 cmd; | |
111 | u8 rsvd; | |
112 | u8 flags; | |
113 | __le32 drv_version; | |
114 | }; | |
115 | ||
116 | struct version_reply_msg { | |
117 | u8 len; | |
118 | u8 cmd; | |
119 | u8 nets; | |
120 | u8 features; | |
121 | __le32 version; | |
122 | u8 name[16]; | |
123 | __le32 rsvd; | |
124 | __le32 ts; | |
125 | }; | |
126 | ||
127 | struct rx_msg { | |
128 | u8 len; | |
129 | u8 cmd; | |
130 | u8 net; | |
131 | u8 dlc; | |
132 | __le32 ts; | |
133 | __le32 id; /* upper 3 bits contain flags */ | |
134 | u8 data[8]; | |
135 | }; | |
136 | ||
137 | struct tx_msg { | |
138 | u8 len; | |
139 | u8 cmd; | |
140 | u8 net; | |
141 | u8 dlc; | |
6d5a7a65 | 142 | u32 hnd; /* opaque handle, not used by device */ |
96d8e903 MF |
143 | __le32 id; /* upper 3 bits contain flags */ |
144 | u8 data[8]; | |
145 | }; | |
146 | ||
147 | struct tx_done_msg { | |
148 | u8 len; | |
149 | u8 cmd; | |
150 | u8 net; | |
151 | u8 status; | |
6d5a7a65 | 152 | u32 hnd; /* opaque handle, not used by device */ |
96d8e903 MF |
153 | __le32 ts; |
154 | }; | |
155 | ||
156 | struct id_filter_msg { | |
157 | u8 len; | |
158 | u8 cmd; | |
159 | u8 net; | |
160 | u8 option; | |
161 | __le32 mask[ESD_MAX_ID_SEGMENT + 1]; | |
162 | }; | |
163 | ||
164 | struct set_baudrate_msg { | |
165 | u8 len; | |
166 | u8 cmd; | |
167 | u8 net; | |
168 | u8 rsvd; | |
169 | __le32 baud; | |
170 | }; | |
171 | ||
172 | /* Main message type used between library and application */ | |
173 | struct __attribute__ ((packed)) esd_usb2_msg { | |
174 | union { | |
175 | struct header_msg hdr; | |
176 | struct version_msg version; | |
177 | struct version_reply_msg version_reply; | |
178 | struct rx_msg rx; | |
179 | struct tx_msg tx; | |
180 | struct tx_done_msg txdone; | |
181 | struct set_baudrate_msg setbaud; | |
182 | struct id_filter_msg filter; | |
183 | } msg; | |
184 | }; | |
185 | ||
186 | static struct usb_device_id esd_usb2_table[] = { | |
187 | {USB_DEVICE(USB_ESDGMBH_VENDOR_ID, USB_CANUSB2_PRODUCT_ID)}, | |
7653ebd5 | 188 | {USB_DEVICE(USB_ESDGMBH_VENDOR_ID, USB_CANUSBM_PRODUCT_ID)}, |
96d8e903 MF |
189 | {} |
190 | }; | |
191 | MODULE_DEVICE_TABLE(usb, esd_usb2_table); | |
192 | ||
193 | struct esd_usb2_net_priv; | |
194 | ||
195 | struct esd_tx_urb_context { | |
196 | struct esd_usb2_net_priv *priv; | |
197 | u32 echo_index; | |
198 | int dlc; | |
199 | }; | |
200 | ||
201 | struct esd_usb2 { | |
202 | struct usb_device *udev; | |
203 | struct esd_usb2_net_priv *nets[ESD_USB2_MAX_NETS]; | |
204 | ||
205 | struct usb_anchor rx_submitted; | |
206 | ||
207 | int net_count; | |
208 | u32 version; | |
209 | int rxinitdone; | |
210 | }; | |
211 | ||
212 | struct esd_usb2_net_priv { | |
213 | struct can_priv can; /* must be the first member */ | |
214 | ||
215 | atomic_t active_tx_jobs; | |
216 | struct usb_anchor tx_submitted; | |
217 | struct esd_tx_urb_context tx_contexts[MAX_TX_URBS]; | |
218 | ||
96d8e903 MF |
219 | struct esd_usb2 *usb2; |
220 | struct net_device *netdev; | |
221 | int index; | |
222 | u8 old_state; | |
223 | struct can_berr_counter bec; | |
224 | }; | |
225 | ||
226 | static void esd_usb2_rx_event(struct esd_usb2_net_priv *priv, | |
227 | struct esd_usb2_msg *msg) | |
228 | { | |
229 | struct net_device_stats *stats = &priv->netdev->stats; | |
230 | struct can_frame *cf; | |
231 | struct sk_buff *skb; | |
232 | u32 id = le32_to_cpu(msg->msg.rx.id) & ESD_IDMASK; | |
233 | ||
234 | if (id == ESD_EV_CAN_ERROR_EXT) { | |
235 | u8 state = msg->msg.rx.data[0]; | |
236 | u8 ecc = msg->msg.rx.data[1]; | |
237 | u8 txerr = msg->msg.rx.data[2]; | |
238 | u8 rxerr = msg->msg.rx.data[3]; | |
239 | ||
240 | skb = alloc_can_err_skb(priv->netdev, &cf); | |
241 | if (skb == NULL) { | |
242 | stats->rx_dropped++; | |
243 | return; | |
244 | } | |
245 | ||
246 | if (state != priv->old_state) { | |
247 | priv->old_state = state; | |
248 | ||
249 | switch (state & ESD_BUSSTATE_MASK) { | |
250 | case ESD_BUSSTATE_BUSOFF: | |
251 | priv->can.state = CAN_STATE_BUS_OFF; | |
252 | cf->can_id |= CAN_ERR_BUSOFF; | |
be38a6f9 | 253 | priv->can.can_stats.bus_off++; |
96d8e903 MF |
254 | can_bus_off(priv->netdev); |
255 | break; | |
256 | case ESD_BUSSTATE_WARN: | |
257 | priv->can.state = CAN_STATE_ERROR_WARNING; | |
258 | priv->can.can_stats.error_warning++; | |
259 | break; | |
260 | case ESD_BUSSTATE_ERRPASSIVE: | |
261 | priv->can.state = CAN_STATE_ERROR_PASSIVE; | |
262 | priv->can.can_stats.error_passive++; | |
263 | break; | |
264 | default: | |
265 | priv->can.state = CAN_STATE_ERROR_ACTIVE; | |
266 | break; | |
267 | } | |
268 | } else { | |
269 | priv->can.can_stats.bus_error++; | |
270 | stats->rx_errors++; | |
271 | ||
272 | cf->can_id |= CAN_ERR_PROT | CAN_ERR_BUSERROR; | |
273 | ||
274 | switch (ecc & SJA1000_ECC_MASK) { | |
275 | case SJA1000_ECC_BIT: | |
276 | cf->data[2] |= CAN_ERR_PROT_BIT; | |
277 | break; | |
278 | case SJA1000_ECC_FORM: | |
279 | cf->data[2] |= CAN_ERR_PROT_FORM; | |
280 | break; | |
281 | case SJA1000_ECC_STUFF: | |
282 | cf->data[2] |= CAN_ERR_PROT_STUFF; | |
283 | break; | |
284 | default: | |
285 | cf->data[2] |= CAN_ERR_PROT_UNSPEC; | |
286 | cf->data[3] = ecc & SJA1000_ECC_SEG; | |
287 | break; | |
288 | } | |
289 | ||
25985edc | 290 | /* Error occurred during transmission? */ |
96d8e903 MF |
291 | if (!(ecc & SJA1000_ECC_DIR)) |
292 | cf->data[2] |= CAN_ERR_PROT_TX; | |
293 | ||
294 | if (priv->can.state == CAN_STATE_ERROR_WARNING || | |
295 | priv->can.state == CAN_STATE_ERROR_PASSIVE) { | |
296 | cf->data[1] = (txerr > rxerr) ? | |
297 | CAN_ERR_CRTL_TX_PASSIVE : | |
298 | CAN_ERR_CRTL_RX_PASSIVE; | |
299 | } | |
300 | cf->data[6] = txerr; | |
301 | cf->data[7] = rxerr; | |
302 | } | |
303 | ||
96d8e903 MF |
304 | priv->bec.txerr = txerr; |
305 | priv->bec.rxerr = rxerr; | |
306 | ||
307 | stats->rx_packets++; | |
308 | stats->rx_bytes += cf->can_dlc; | |
296decb6 | 309 | netif_rx(skb); |
96d8e903 MF |
310 | } |
311 | } | |
312 | ||
313 | static void esd_usb2_rx_can_msg(struct esd_usb2_net_priv *priv, | |
314 | struct esd_usb2_msg *msg) | |
315 | { | |
316 | struct net_device_stats *stats = &priv->netdev->stats; | |
317 | struct can_frame *cf; | |
318 | struct sk_buff *skb; | |
319 | int i; | |
320 | u32 id; | |
321 | ||
322 | if (!netif_device_present(priv->netdev)) | |
323 | return; | |
324 | ||
325 | id = le32_to_cpu(msg->msg.rx.id); | |
326 | ||
327 | if (id & ESD_EVENT) { | |
328 | esd_usb2_rx_event(priv, msg); | |
329 | } else { | |
330 | skb = alloc_can_skb(priv->netdev, &cf); | |
331 | if (skb == NULL) { | |
332 | stats->rx_dropped++; | |
333 | return; | |
334 | } | |
335 | ||
336 | cf->can_id = id & ESD_IDMASK; | |
337 | cf->can_dlc = get_can_dlc(msg->msg.rx.dlc); | |
338 | ||
339 | if (id & ESD_EXTID) | |
340 | cf->can_id |= CAN_EFF_FLAG; | |
341 | ||
342 | if (msg->msg.rx.dlc & ESD_RTR) { | |
343 | cf->can_id |= CAN_RTR_FLAG; | |
344 | } else { | |
345 | for (i = 0; i < cf->can_dlc; i++) | |
346 | cf->data[i] = msg->msg.rx.data[i]; | |
347 | } | |
348 | ||
96d8e903 MF |
349 | stats->rx_packets++; |
350 | stats->rx_bytes += cf->can_dlc; | |
296decb6 | 351 | netif_rx(skb); |
96d8e903 MF |
352 | } |
353 | ||
354 | return; | |
355 | } | |
356 | ||
357 | static void esd_usb2_tx_done_msg(struct esd_usb2_net_priv *priv, | |
358 | struct esd_usb2_msg *msg) | |
359 | { | |
360 | struct net_device_stats *stats = &priv->netdev->stats; | |
361 | struct net_device *netdev = priv->netdev; | |
362 | struct esd_tx_urb_context *context; | |
363 | ||
364 | if (!netif_device_present(netdev)) | |
365 | return; | |
366 | ||
367 | context = &priv->tx_contexts[msg->msg.txdone.hnd & (MAX_TX_URBS - 1)]; | |
368 | ||
369 | if (!msg->msg.txdone.status) { | |
370 | stats->tx_packets++; | |
371 | stats->tx_bytes += context->dlc; | |
372 | can_get_echo_skb(netdev, context->echo_index); | |
373 | } else { | |
374 | stats->tx_errors++; | |
375 | can_free_echo_skb(netdev, context->echo_index); | |
376 | } | |
377 | ||
378 | /* Release context */ | |
379 | context->echo_index = MAX_TX_URBS; | |
380 | atomic_dec(&priv->active_tx_jobs); | |
381 | ||
382 | netif_wake_queue(netdev); | |
383 | } | |
384 | ||
385 | static void esd_usb2_read_bulk_callback(struct urb *urb) | |
386 | { | |
387 | struct esd_usb2 *dev = urb->context; | |
388 | int retval; | |
389 | int pos = 0; | |
390 | int i; | |
391 | ||
392 | switch (urb->status) { | |
393 | case 0: /* success */ | |
394 | break; | |
395 | ||
396 | case -ENOENT: | |
397 | case -ESHUTDOWN: | |
398 | return; | |
399 | ||
400 | default: | |
401 | dev_info(dev->udev->dev.parent, | |
402 | "Rx URB aborted (%d)\n", urb->status); | |
403 | goto resubmit_urb; | |
404 | } | |
405 | ||
406 | while (pos < urb->actual_length) { | |
407 | struct esd_usb2_msg *msg; | |
408 | ||
409 | msg = (struct esd_usb2_msg *)(urb->transfer_buffer + pos); | |
410 | ||
411 | switch (msg->msg.hdr.cmd) { | |
412 | case CMD_CAN_RX: | |
233a26e8 MS |
413 | if (msg->msg.rx.net >= dev->net_count) { |
414 | dev_err(dev->udev->dev.parent, "format error\n"); | |
415 | break; | |
416 | } | |
417 | ||
96d8e903 MF |
418 | esd_usb2_rx_can_msg(dev->nets[msg->msg.rx.net], msg); |
419 | break; | |
420 | ||
421 | case CMD_CAN_TX: | |
233a26e8 MS |
422 | if (msg->msg.txdone.net >= dev->net_count) { |
423 | dev_err(dev->udev->dev.parent, "format error\n"); | |
424 | break; | |
425 | } | |
426 | ||
96d8e903 MF |
427 | esd_usb2_tx_done_msg(dev->nets[msg->msg.txdone.net], |
428 | msg); | |
429 | break; | |
430 | } | |
431 | ||
432 | pos += msg->msg.hdr.len << 2; | |
433 | ||
434 | if (pos > urb->actual_length) { | |
435 | dev_err(dev->udev->dev.parent, "format error\n"); | |
436 | break; | |
437 | } | |
438 | } | |
439 | ||
440 | resubmit_urb: | |
441 | usb_fill_bulk_urb(urb, dev->udev, usb_rcvbulkpipe(dev->udev, 1), | |
442 | urb->transfer_buffer, RX_BUFFER_SIZE, | |
443 | esd_usb2_read_bulk_callback, dev); | |
444 | ||
445 | retval = usb_submit_urb(urb, GFP_ATOMIC); | |
446 | if (retval == -ENODEV) { | |
447 | for (i = 0; i < dev->net_count; i++) { | |
448 | if (dev->nets[i]) | |
449 | netif_device_detach(dev->nets[i]->netdev); | |
450 | } | |
451 | } else if (retval) { | |
452 | dev_err(dev->udev->dev.parent, | |
453 | "failed resubmitting read bulk urb: %d\n", retval); | |
454 | } | |
455 | ||
456 | return; | |
457 | } | |
458 | ||
459 | /* | |
460 | * callback for bulk IN urb | |
461 | */ | |
462 | static void esd_usb2_write_bulk_callback(struct urb *urb) | |
463 | { | |
464 | struct esd_tx_urb_context *context = urb->context; | |
465 | struct esd_usb2_net_priv *priv; | |
96d8e903 MF |
466 | struct net_device *netdev; |
467 | size_t size = sizeof(struct esd_usb2_msg); | |
468 | ||
469 | WARN_ON(!context); | |
470 | ||
471 | priv = context->priv; | |
472 | netdev = priv->netdev; | |
96d8e903 MF |
473 | |
474 | /* free up our allocated buffer */ | |
475 | usb_free_coherent(urb->dev, size, | |
476 | urb->transfer_buffer, urb->transfer_dma); | |
477 | ||
478 | if (!netif_device_present(netdev)) | |
479 | return; | |
480 | ||
481 | if (urb->status) | |
aabdfd6a | 482 | netdev_info(netdev, "Tx URB aborted (%d)\n", urb->status); |
96d8e903 MF |
483 | |
484 | netdev->trans_start = jiffies; | |
485 | } | |
486 | ||
487 | static ssize_t show_firmware(struct device *d, | |
488 | struct device_attribute *attr, char *buf) | |
489 | { | |
490 | struct usb_interface *intf = to_usb_interface(d); | |
491 | struct esd_usb2 *dev = usb_get_intfdata(intf); | |
492 | ||
493 | return sprintf(buf, "%d.%d.%d\n", | |
494 | (dev->version >> 12) & 0xf, | |
495 | (dev->version >> 8) & 0xf, | |
496 | dev->version & 0xff); | |
497 | } | |
498 | static DEVICE_ATTR(firmware, S_IRUGO, show_firmware, NULL); | |
499 | ||
500 | static ssize_t show_hardware(struct device *d, | |
501 | struct device_attribute *attr, char *buf) | |
502 | { | |
503 | struct usb_interface *intf = to_usb_interface(d); | |
504 | struct esd_usb2 *dev = usb_get_intfdata(intf); | |
505 | ||
506 | return sprintf(buf, "%d.%d.%d\n", | |
507 | (dev->version >> 28) & 0xf, | |
508 | (dev->version >> 24) & 0xf, | |
509 | (dev->version >> 16) & 0xff); | |
510 | } | |
511 | static DEVICE_ATTR(hardware, S_IRUGO, show_hardware, NULL); | |
512 | ||
513 | static ssize_t show_nets(struct device *d, | |
514 | struct device_attribute *attr, char *buf) | |
515 | { | |
516 | struct usb_interface *intf = to_usb_interface(d); | |
517 | struct esd_usb2 *dev = usb_get_intfdata(intf); | |
518 | ||
519 | return sprintf(buf, "%d", dev->net_count); | |
520 | } | |
521 | static DEVICE_ATTR(nets, S_IRUGO, show_nets, NULL); | |
522 | ||
523 | static int esd_usb2_send_msg(struct esd_usb2 *dev, struct esd_usb2_msg *msg) | |
524 | { | |
525 | int actual_length; | |
526 | ||
527 | return usb_bulk_msg(dev->udev, | |
528 | usb_sndbulkpipe(dev->udev, 2), | |
529 | msg, | |
530 | msg->msg.hdr.len << 2, | |
531 | &actual_length, | |
532 | 1000); | |
533 | } | |
534 | ||
535 | static int esd_usb2_wait_msg(struct esd_usb2 *dev, | |
536 | struct esd_usb2_msg *msg) | |
537 | { | |
538 | int actual_length; | |
539 | ||
540 | return usb_bulk_msg(dev->udev, | |
541 | usb_rcvbulkpipe(dev->udev, 1), | |
542 | msg, | |
543 | sizeof(*msg), | |
544 | &actual_length, | |
545 | 1000); | |
546 | } | |
547 | ||
548 | static int esd_usb2_setup_rx_urbs(struct esd_usb2 *dev) | |
549 | { | |
550 | int i, err = 0; | |
551 | ||
552 | if (dev->rxinitdone) | |
553 | return 0; | |
554 | ||
555 | for (i = 0; i < MAX_RX_URBS; i++) { | |
556 | struct urb *urb = NULL; | |
557 | u8 *buf = NULL; | |
558 | ||
559 | /* create a URB, and a buffer for it */ | |
560 | urb = usb_alloc_urb(0, GFP_KERNEL); | |
561 | if (!urb) { | |
562 | dev_warn(dev->udev->dev.parent, | |
563 | "No memory left for URBs\n"); | |
564 | err = -ENOMEM; | |
565 | break; | |
566 | } | |
567 | ||
568 | buf = usb_alloc_coherent(dev->udev, RX_BUFFER_SIZE, GFP_KERNEL, | |
569 | &urb->transfer_dma); | |
570 | if (!buf) { | |
571 | dev_warn(dev->udev->dev.parent, | |
572 | "No memory left for USB buffer\n"); | |
573 | err = -ENOMEM; | |
574 | goto freeurb; | |
575 | } | |
576 | ||
577 | usb_fill_bulk_urb(urb, dev->udev, | |
578 | usb_rcvbulkpipe(dev->udev, 1), | |
579 | buf, RX_BUFFER_SIZE, | |
580 | esd_usb2_read_bulk_callback, dev); | |
581 | urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP; | |
582 | usb_anchor_urb(urb, &dev->rx_submitted); | |
583 | ||
584 | err = usb_submit_urb(urb, GFP_KERNEL); | |
585 | if (err) { | |
586 | usb_unanchor_urb(urb); | |
587 | usb_free_coherent(dev->udev, RX_BUFFER_SIZE, buf, | |
588 | urb->transfer_dma); | |
589 | } | |
590 | ||
591 | freeurb: | |
592 | /* Drop reference, USB core will take care of freeing it */ | |
593 | usb_free_urb(urb); | |
594 | if (err) | |
595 | break; | |
596 | } | |
597 | ||
598 | /* Did we submit any URBs */ | |
599 | if (i == 0) { | |
600 | dev_err(dev->udev->dev.parent, "couldn't setup read URBs\n"); | |
601 | return err; | |
602 | } | |
603 | ||
604 | /* Warn if we've couldn't transmit all the URBs */ | |
605 | if (i < MAX_RX_URBS) { | |
606 | dev_warn(dev->udev->dev.parent, | |
607 | "rx performance may be slow\n"); | |
608 | } | |
609 | ||
610 | dev->rxinitdone = 1; | |
611 | return 0; | |
612 | } | |
613 | ||
614 | /* | |
615 | * Start interface | |
616 | */ | |
617 | static int esd_usb2_start(struct esd_usb2_net_priv *priv) | |
618 | { | |
619 | struct esd_usb2 *dev = priv->usb2; | |
620 | struct net_device *netdev = priv->netdev; | |
fae37f81 | 621 | struct esd_usb2_msg *msg; |
96d8e903 MF |
622 | int err, i; |
623 | ||
fae37f81 OS |
624 | msg = kmalloc(sizeof(*msg), GFP_KERNEL); |
625 | if (!msg) { | |
626 | err = -ENOMEM; | |
627 | goto out; | |
628 | } | |
629 | ||
96d8e903 MF |
630 | /* |
631 | * Enable all IDs | |
632 | * The IDADD message takes up to 64 32 bit bitmasks (2048 bits). | |
633 | * Each bit represents one 11 bit CAN identifier. A set bit | |
634 | * enables reception of the corresponding CAN identifier. A cleared | |
635 | * bit disabled this identifier. An additional bitmask value | |
636 | * following the CAN 2.0A bits is used to enable reception of | |
637 | * extended CAN frames. Only the LSB of this final mask is checked | |
638 | * for the complete 29 bit ID range. The IDADD message also allows | |
639 | * filter configuration for an ID subset. In this case you can add | |
640 | * the number of the starting bitmask (0..64) to the filter.option | |
641 | * field followed by only some bitmasks. | |
642 | */ | |
fae37f81 OS |
643 | msg->msg.hdr.cmd = CMD_IDADD; |
644 | msg->msg.hdr.len = 2 + ESD_MAX_ID_SEGMENT; | |
645 | msg->msg.filter.net = priv->index; | |
646 | msg->msg.filter.option = ESD_ID_ENABLE; /* start with segment 0 */ | |
96d8e903 | 647 | for (i = 0; i < ESD_MAX_ID_SEGMENT; i++) |
fae37f81 | 648 | msg->msg.filter.mask[i] = cpu_to_le32(0xffffffff); |
96d8e903 | 649 | /* enable 29bit extended IDs */ |
fae37f81 | 650 | msg->msg.filter.mask[ESD_MAX_ID_SEGMENT] = cpu_to_le32(0x00000001); |
96d8e903 | 651 | |
fae37f81 | 652 | err = esd_usb2_send_msg(dev, msg); |
96d8e903 | 653 | if (err) |
fae37f81 | 654 | goto out; |
96d8e903 MF |
655 | |
656 | err = esd_usb2_setup_rx_urbs(dev); | |
657 | if (err) | |
fae37f81 | 658 | goto out; |
96d8e903 MF |
659 | |
660 | priv->can.state = CAN_STATE_ERROR_ACTIVE; | |
661 | ||
fae37f81 | 662 | out: |
96d8e903 MF |
663 | if (err == -ENODEV) |
664 | netif_device_detach(netdev); | |
fae37f81 OS |
665 | if (err) |
666 | netdev_err(netdev, "couldn't start device: %d\n", err); | |
96d8e903 | 667 | |
fae37f81 | 668 | kfree(msg); |
96d8e903 MF |
669 | return err; |
670 | } | |
671 | ||
672 | static void unlink_all_urbs(struct esd_usb2 *dev) | |
673 | { | |
674 | struct esd_usb2_net_priv *priv; | |
0b322111 | 675 | int i, j; |
96d8e903 MF |
676 | |
677 | usb_kill_anchored_urbs(&dev->rx_submitted); | |
678 | for (i = 0; i < dev->net_count; i++) { | |
679 | priv = dev->nets[i]; | |
680 | if (priv) { | |
681 | usb_kill_anchored_urbs(&priv->tx_submitted); | |
682 | atomic_set(&priv->active_tx_jobs, 0); | |
683 | ||
0b322111 | 684 | for (j = 0; j < MAX_TX_URBS; j++) |
685 | priv->tx_contexts[j].echo_index = MAX_TX_URBS; | |
96d8e903 MF |
686 | } |
687 | } | |
688 | } | |
689 | ||
690 | static int esd_usb2_open(struct net_device *netdev) | |
691 | { | |
692 | struct esd_usb2_net_priv *priv = netdev_priv(netdev); | |
693 | int err; | |
694 | ||
695 | /* common open */ | |
696 | err = open_candev(netdev); | |
697 | if (err) | |
698 | return err; | |
699 | ||
700 | /* finally start device */ | |
701 | err = esd_usb2_start(priv); | |
702 | if (err) { | |
aabdfd6a | 703 | netdev_warn(netdev, "couldn't start device: %d\n", err); |
96d8e903 MF |
704 | close_candev(netdev); |
705 | return err; | |
706 | } | |
707 | ||
96d8e903 MF |
708 | netif_start_queue(netdev); |
709 | ||
710 | return 0; | |
711 | } | |
712 | ||
713 | static netdev_tx_t esd_usb2_start_xmit(struct sk_buff *skb, | |
714 | struct net_device *netdev) | |
715 | { | |
716 | struct esd_usb2_net_priv *priv = netdev_priv(netdev); | |
717 | struct esd_usb2 *dev = priv->usb2; | |
718 | struct esd_tx_urb_context *context = NULL; | |
719 | struct net_device_stats *stats = &netdev->stats; | |
720 | struct can_frame *cf = (struct can_frame *)skb->data; | |
721 | struct esd_usb2_msg *msg; | |
722 | struct urb *urb; | |
723 | u8 *buf; | |
724 | int i, err; | |
725 | int ret = NETDEV_TX_OK; | |
726 | size_t size = sizeof(struct esd_usb2_msg); | |
727 | ||
728 | if (can_dropped_invalid_skb(netdev, skb)) | |
729 | return NETDEV_TX_OK; | |
730 | ||
731 | /* create a URB, and a buffer for it, and copy the data to the URB */ | |
732 | urb = usb_alloc_urb(0, GFP_ATOMIC); | |
733 | if (!urb) { | |
aabdfd6a | 734 | netdev_err(netdev, "No memory left for URBs\n"); |
96d8e903 MF |
735 | stats->tx_dropped++; |
736 | dev_kfree_skb(skb); | |
737 | goto nourbmem; | |
738 | } | |
739 | ||
740 | buf = usb_alloc_coherent(dev->udev, size, GFP_ATOMIC, | |
741 | &urb->transfer_dma); | |
742 | if (!buf) { | |
aabdfd6a | 743 | netdev_err(netdev, "No memory left for USB buffer\n"); |
96d8e903 MF |
744 | stats->tx_dropped++; |
745 | dev_kfree_skb(skb); | |
746 | goto nobufmem; | |
747 | } | |
748 | ||
749 | msg = (struct esd_usb2_msg *)buf; | |
750 | ||
751 | msg->msg.hdr.len = 3; /* minimal length */ | |
752 | msg->msg.hdr.cmd = CMD_CAN_TX; | |
753 | msg->msg.tx.net = priv->index; | |
754 | msg->msg.tx.dlc = cf->can_dlc; | |
755 | msg->msg.tx.id = cpu_to_le32(cf->can_id & CAN_ERR_MASK); | |
756 | ||
757 | if (cf->can_id & CAN_RTR_FLAG) | |
758 | msg->msg.tx.dlc |= ESD_RTR; | |
759 | ||
760 | if (cf->can_id & CAN_EFF_FLAG) | |
761 | msg->msg.tx.id |= cpu_to_le32(ESD_EXTID); | |
762 | ||
763 | for (i = 0; i < cf->can_dlc; i++) | |
764 | msg->msg.tx.data[i] = cf->data[i]; | |
765 | ||
766 | msg->msg.hdr.len += (cf->can_dlc + 3) >> 2; | |
767 | ||
768 | for (i = 0; i < MAX_TX_URBS; i++) { | |
769 | if (priv->tx_contexts[i].echo_index == MAX_TX_URBS) { | |
770 | context = &priv->tx_contexts[i]; | |
771 | break; | |
772 | } | |
773 | } | |
774 | ||
775 | /* | |
776 | * This may never happen. | |
777 | */ | |
778 | if (!context) { | |
aabdfd6a | 779 | netdev_warn(netdev, "couldn't find free context\n"); |
96d8e903 MF |
780 | ret = NETDEV_TX_BUSY; |
781 | goto releasebuf; | |
782 | } | |
783 | ||
784 | context->priv = priv; | |
785 | context->echo_index = i; | |
786 | context->dlc = cf->can_dlc; | |
787 | ||
788 | /* hnd must not be 0 - MSB is stripped in txdone handling */ | |
789 | msg->msg.tx.hnd = 0x80000000 | i; /* returned in TX done message */ | |
790 | ||
791 | usb_fill_bulk_urb(urb, dev->udev, usb_sndbulkpipe(dev->udev, 2), buf, | |
792 | msg->msg.hdr.len << 2, | |
793 | esd_usb2_write_bulk_callback, context); | |
794 | ||
795 | urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP; | |
796 | ||
797 | usb_anchor_urb(urb, &priv->tx_submitted); | |
798 | ||
799 | can_put_echo_skb(skb, netdev, context->echo_index); | |
800 | ||
801 | atomic_inc(&priv->active_tx_jobs); | |
802 | ||
803 | /* Slow down tx path */ | |
804 | if (atomic_read(&priv->active_tx_jobs) >= MAX_TX_URBS) | |
805 | netif_stop_queue(netdev); | |
806 | ||
807 | err = usb_submit_urb(urb, GFP_ATOMIC); | |
808 | if (err) { | |
809 | can_free_echo_skb(netdev, context->echo_index); | |
810 | ||
811 | atomic_dec(&priv->active_tx_jobs); | |
812 | usb_unanchor_urb(urb); | |
813 | ||
814 | stats->tx_dropped++; | |
815 | ||
816 | if (err == -ENODEV) | |
817 | netif_device_detach(netdev); | |
818 | else | |
aabdfd6a | 819 | netdev_warn(netdev, "failed tx_urb %d\n", err); |
96d8e903 MF |
820 | |
821 | goto releasebuf; | |
822 | } | |
823 | ||
824 | netdev->trans_start = jiffies; | |
825 | ||
826 | /* | |
827 | * Release our reference to this URB, the USB core will eventually free | |
828 | * it entirely. | |
829 | */ | |
830 | usb_free_urb(urb); | |
831 | ||
832 | return NETDEV_TX_OK; | |
833 | ||
834 | releasebuf: | |
835 | usb_free_coherent(dev->udev, size, buf, urb->transfer_dma); | |
836 | ||
837 | nobufmem: | |
838 | usb_free_urb(urb); | |
839 | ||
840 | nourbmem: | |
841 | return ret; | |
842 | } | |
843 | ||
844 | static int esd_usb2_close(struct net_device *netdev) | |
845 | { | |
846 | struct esd_usb2_net_priv *priv = netdev_priv(netdev); | |
fae37f81 | 847 | struct esd_usb2_msg *msg; |
96d8e903 MF |
848 | int i; |
849 | ||
fae37f81 OS |
850 | msg = kmalloc(sizeof(*msg), GFP_KERNEL); |
851 | if (!msg) | |
852 | return -ENOMEM; | |
853 | ||
96d8e903 | 854 | /* Disable all IDs (see esd_usb2_start()) */ |
fae37f81 OS |
855 | msg->msg.hdr.cmd = CMD_IDADD; |
856 | msg->msg.hdr.len = 2 + ESD_MAX_ID_SEGMENT; | |
857 | msg->msg.filter.net = priv->index; | |
858 | msg->msg.filter.option = ESD_ID_ENABLE; /* start with segment 0 */ | |
96d8e903 | 859 | for (i = 0; i <= ESD_MAX_ID_SEGMENT; i++) |
fae37f81 OS |
860 | msg->msg.filter.mask[i] = 0; |
861 | if (esd_usb2_send_msg(priv->usb2, msg) < 0) | |
aabdfd6a | 862 | netdev_err(netdev, "sending idadd message failed\n"); |
96d8e903 MF |
863 | |
864 | /* set CAN controller to reset mode */ | |
fae37f81 OS |
865 | msg->msg.hdr.len = 2; |
866 | msg->msg.hdr.cmd = CMD_SETBAUD; | |
867 | msg->msg.setbaud.net = priv->index; | |
868 | msg->msg.setbaud.rsvd = 0; | |
869 | msg->msg.setbaud.baud = cpu_to_le32(ESD_USB2_NO_BAUDRATE); | |
870 | if (esd_usb2_send_msg(priv->usb2, msg) < 0) | |
aabdfd6a | 871 | netdev_err(netdev, "sending setbaud message failed\n"); |
96d8e903 MF |
872 | |
873 | priv->can.state = CAN_STATE_STOPPED; | |
874 | ||
875 | netif_stop_queue(netdev); | |
876 | ||
877 | close_candev(netdev); | |
878 | ||
fae37f81 OS |
879 | kfree(msg); |
880 | ||
96d8e903 MF |
881 | return 0; |
882 | } | |
883 | ||
884 | static const struct net_device_ops esd_usb2_netdev_ops = { | |
885 | .ndo_open = esd_usb2_open, | |
886 | .ndo_stop = esd_usb2_close, | |
887 | .ndo_start_xmit = esd_usb2_start_xmit, | |
c971fa2a | 888 | .ndo_change_mtu = can_change_mtu, |
96d8e903 MF |
889 | }; |
890 | ||
194b9a4c | 891 | static const struct can_bittiming_const esd_usb2_bittiming_const = { |
96d8e903 MF |
892 | .name = "esd_usb2", |
893 | .tseg1_min = ESD_USB2_TSEG1_MIN, | |
894 | .tseg1_max = ESD_USB2_TSEG1_MAX, | |
895 | .tseg2_min = ESD_USB2_TSEG2_MIN, | |
896 | .tseg2_max = ESD_USB2_TSEG2_MAX, | |
897 | .sjw_max = ESD_USB2_SJW_MAX, | |
898 | .brp_min = ESD_USB2_BRP_MIN, | |
899 | .brp_max = ESD_USB2_BRP_MAX, | |
900 | .brp_inc = ESD_USB2_BRP_INC, | |
901 | }; | |
902 | ||
903 | static int esd_usb2_set_bittiming(struct net_device *netdev) | |
904 | { | |
905 | struct esd_usb2_net_priv *priv = netdev_priv(netdev); | |
906 | struct can_bittiming *bt = &priv->can.bittiming; | |
fae37f81 OS |
907 | struct esd_usb2_msg *msg; |
908 | int err; | |
96d8e903 | 909 | u32 canbtr; |
7653ebd5 | 910 | int sjw_shift; |
96d8e903 MF |
911 | |
912 | canbtr = ESD_USB2_UBR; | |
a5f8f0e1 MF |
913 | if (priv->can.ctrlmode & CAN_CTRLMODE_LISTENONLY) |
914 | canbtr |= ESD_USB2_LOM; | |
915 | ||
96d8e903 | 916 | canbtr |= (bt->brp - 1) & (ESD_USB2_BRP_MAX - 1); |
7653ebd5 MF |
917 | |
918 | if (le16_to_cpu(priv->usb2->udev->descriptor.idProduct) == | |
919 | USB_CANUSBM_PRODUCT_ID) | |
920 | sjw_shift = ESD_USBM_SJW_SHIFT; | |
921 | else | |
922 | sjw_shift = ESD_USB2_SJW_SHIFT; | |
923 | ||
96d8e903 | 924 | canbtr |= ((bt->sjw - 1) & (ESD_USB2_SJW_MAX - 1)) |
7653ebd5 | 925 | << sjw_shift; |
96d8e903 MF |
926 | canbtr |= ((bt->prop_seg + bt->phase_seg1 - 1) |
927 | & (ESD_USB2_TSEG1_MAX - 1)) | |
928 | << ESD_USB2_TSEG1_SHIFT; | |
929 | canbtr |= ((bt->phase_seg2 - 1) & (ESD_USB2_TSEG2_MAX - 1)) | |
930 | << ESD_USB2_TSEG2_SHIFT; | |
931 | if (priv->can.ctrlmode & CAN_CTRLMODE_3_SAMPLES) | |
932 | canbtr |= ESD_USB2_3_SAMPLES; | |
933 | ||
fae37f81 OS |
934 | msg = kmalloc(sizeof(*msg), GFP_KERNEL); |
935 | if (!msg) | |
936 | return -ENOMEM; | |
937 | ||
938 | msg->msg.hdr.len = 2; | |
939 | msg->msg.hdr.cmd = CMD_SETBAUD; | |
940 | msg->msg.setbaud.net = priv->index; | |
941 | msg->msg.setbaud.rsvd = 0; | |
942 | msg->msg.setbaud.baud = cpu_to_le32(canbtr); | |
96d8e903 | 943 | |
aabdfd6a | 944 | netdev_info(netdev, "setting BTR=%#x\n", canbtr); |
96d8e903 | 945 | |
fae37f81 OS |
946 | err = esd_usb2_send_msg(priv->usb2, msg); |
947 | ||
948 | kfree(msg); | |
949 | return err; | |
96d8e903 MF |
950 | } |
951 | ||
952 | static int esd_usb2_get_berr_counter(const struct net_device *netdev, | |
953 | struct can_berr_counter *bec) | |
954 | { | |
955 | struct esd_usb2_net_priv *priv = netdev_priv(netdev); | |
956 | ||
957 | bec->txerr = priv->bec.txerr; | |
958 | bec->rxerr = priv->bec.rxerr; | |
959 | ||
960 | return 0; | |
961 | } | |
962 | ||
963 | static int esd_usb2_set_mode(struct net_device *netdev, enum can_mode mode) | |
964 | { | |
96d8e903 MF |
965 | switch (mode) { |
966 | case CAN_MODE_START: | |
967 | netif_wake_queue(netdev); | |
968 | break; | |
969 | ||
970 | default: | |
971 | return -EOPNOTSUPP; | |
972 | } | |
973 | ||
974 | return 0; | |
975 | } | |
976 | ||
977 | static int esd_usb2_probe_one_net(struct usb_interface *intf, int index) | |
978 | { | |
979 | struct esd_usb2 *dev = usb_get_intfdata(intf); | |
980 | struct net_device *netdev; | |
981 | struct esd_usb2_net_priv *priv; | |
982 | int err = 0; | |
983 | int i; | |
984 | ||
985 | netdev = alloc_candev(sizeof(*priv), MAX_TX_URBS); | |
986 | if (!netdev) { | |
987 | dev_err(&intf->dev, "couldn't alloc candev\n"); | |
988 | err = -ENOMEM; | |
989 | goto done; | |
990 | } | |
991 | ||
992 | priv = netdev_priv(netdev); | |
993 | ||
994 | init_usb_anchor(&priv->tx_submitted); | |
995 | atomic_set(&priv->active_tx_jobs, 0); | |
996 | ||
997 | for (i = 0; i < MAX_TX_URBS; i++) | |
998 | priv->tx_contexts[i].echo_index = MAX_TX_URBS; | |
999 | ||
1000 | priv->usb2 = dev; | |
1001 | priv->netdev = netdev; | |
1002 | priv->index = index; | |
1003 | ||
1004 | priv->can.state = CAN_STATE_STOPPED; | |
a5f8f0e1 | 1005 | priv->can.ctrlmode_supported = CAN_CTRLMODE_LISTENONLY; |
7653ebd5 MF |
1006 | |
1007 | if (le16_to_cpu(dev->udev->descriptor.idProduct) == | |
1008 | USB_CANUSBM_PRODUCT_ID) | |
1009 | priv->can.clock.freq = ESD_USBM_CAN_CLOCK; | |
1010 | else { | |
1011 | priv->can.clock.freq = ESD_USB2_CAN_CLOCK; | |
1012 | priv->can.ctrlmode_supported |= CAN_CTRLMODE_3_SAMPLES; | |
1013 | } | |
1014 | ||
96d8e903 MF |
1015 | priv->can.bittiming_const = &esd_usb2_bittiming_const; |
1016 | priv->can.do_set_bittiming = esd_usb2_set_bittiming; | |
1017 | priv->can.do_set_mode = esd_usb2_set_mode; | |
1018 | priv->can.do_get_berr_counter = esd_usb2_get_berr_counter; | |
96d8e903 MF |
1019 | |
1020 | netdev->flags |= IFF_ECHO; /* we support local echo */ | |
1021 | ||
1022 | netdev->netdev_ops = &esd_usb2_netdev_ops; | |
1023 | ||
1024 | SET_NETDEV_DEV(netdev, &intf->dev); | |
3e66d013 | 1025 | netdev->dev_id = index; |
96d8e903 MF |
1026 | |
1027 | err = register_candev(netdev); | |
1028 | if (err) { | |
aabdfd6a | 1029 | dev_err(&intf->dev, "couldn't register CAN device: %d\n", err); |
96d8e903 MF |
1030 | free_candev(netdev); |
1031 | err = -ENOMEM; | |
1032 | goto done; | |
1033 | } | |
1034 | ||
1035 | dev->nets[index] = priv; | |
aabdfd6a | 1036 | netdev_info(netdev, "device %s registered\n", netdev->name); |
96d8e903 MF |
1037 | |
1038 | done: | |
1039 | return err; | |
1040 | } | |
1041 | ||
1042 | /* | |
1043 | * probe function for new USB2 devices | |
1044 | * | |
1045 | * check version information and number of available | |
1046 | * CAN interfaces | |
1047 | */ | |
1048 | static int esd_usb2_probe(struct usb_interface *intf, | |
1049 | const struct usb_device_id *id) | |
1050 | { | |
1051 | struct esd_usb2 *dev; | |
fae37f81 | 1052 | struct esd_usb2_msg *msg; |
96d8e903 MF |
1053 | int i, err; |
1054 | ||
1055 | dev = kzalloc(sizeof(*dev), GFP_KERNEL); | |
1056 | if (!dev) { | |
1057 | err = -ENOMEM; | |
1058 | goto done; | |
1059 | } | |
1060 | ||
1061 | dev->udev = interface_to_usbdev(intf); | |
1062 | ||
1063 | init_usb_anchor(&dev->rx_submitted); | |
1064 | ||
1065 | usb_set_intfdata(intf, dev); | |
1066 | ||
fae37f81 OS |
1067 | msg = kmalloc(sizeof(*msg), GFP_KERNEL); |
1068 | if (!msg) { | |
1069 | err = -ENOMEM; | |
1070 | goto free_msg; | |
1071 | } | |
1072 | ||
96d8e903 | 1073 | /* query number of CAN interfaces (nets) */ |
fae37f81 OS |
1074 | msg->msg.hdr.cmd = CMD_VERSION; |
1075 | msg->msg.hdr.len = 2; | |
1076 | msg->msg.version.rsvd = 0; | |
1077 | msg->msg.version.flags = 0; | |
1078 | msg->msg.version.drv_version = 0; | |
96d8e903 | 1079 | |
fae37f81 | 1080 | err = esd_usb2_send_msg(dev, msg); |
96d8e903 MF |
1081 | if (err < 0) { |
1082 | dev_err(&intf->dev, "sending version message failed\n"); | |
fae37f81 | 1083 | goto free_msg; |
96d8e903 MF |
1084 | } |
1085 | ||
fae37f81 | 1086 | err = esd_usb2_wait_msg(dev, msg); |
96d8e903 MF |
1087 | if (err < 0) { |
1088 | dev_err(&intf->dev, "no version message answer\n"); | |
fae37f81 | 1089 | goto free_msg; |
96d8e903 MF |
1090 | } |
1091 | ||
fae37f81 OS |
1092 | dev->net_count = (int)msg->msg.version_reply.nets; |
1093 | dev->version = le32_to_cpu(msg->msg.version_reply.version); | |
96d8e903 MF |
1094 | |
1095 | if (device_create_file(&intf->dev, &dev_attr_firmware)) | |
1096 | dev_err(&intf->dev, | |
1097 | "Couldn't create device file for firmware\n"); | |
1098 | ||
1099 | if (device_create_file(&intf->dev, &dev_attr_hardware)) | |
1100 | dev_err(&intf->dev, | |
1101 | "Couldn't create device file for hardware\n"); | |
1102 | ||
1103 | if (device_create_file(&intf->dev, &dev_attr_nets)) | |
1104 | dev_err(&intf->dev, | |
1105 | "Couldn't create device file for nets\n"); | |
1106 | ||
1107 | /* do per device probing */ | |
1108 | for (i = 0; i < dev->net_count; i++) | |
1109 | esd_usb2_probe_one_net(intf, i); | |
1110 | ||
fae37f81 OS |
1111 | free_msg: |
1112 | kfree(msg); | |
1113 | if (err) | |
1114 | kfree(dev); | |
96d8e903 MF |
1115 | done: |
1116 | return err; | |
1117 | } | |
1118 | ||
1119 | /* | |
1120 | * called by the usb core when the device is removed from the system | |
1121 | */ | |
1122 | static void esd_usb2_disconnect(struct usb_interface *intf) | |
1123 | { | |
1124 | struct esd_usb2 *dev = usb_get_intfdata(intf); | |
1125 | struct net_device *netdev; | |
1126 | int i; | |
1127 | ||
1128 | device_remove_file(&intf->dev, &dev_attr_firmware); | |
1129 | device_remove_file(&intf->dev, &dev_attr_hardware); | |
1130 | device_remove_file(&intf->dev, &dev_attr_nets); | |
1131 | ||
1132 | usb_set_intfdata(intf, NULL); | |
1133 | ||
1134 | if (dev) { | |
1135 | for (i = 0; i < dev->net_count; i++) { | |
1136 | if (dev->nets[i]) { | |
1137 | netdev = dev->nets[i]->netdev; | |
1138 | unregister_netdev(netdev); | |
1139 | free_candev(netdev); | |
1140 | } | |
1141 | } | |
1142 | unlink_all_urbs(dev); | |
efbd50d2 | 1143 | kfree(dev); |
96d8e903 MF |
1144 | } |
1145 | } | |
1146 | ||
1147 | /* usb specific object needed to register this driver with the usb subsystem */ | |
1148 | static struct usb_driver esd_usb2_driver = { | |
1149 | .name = "esd_usb2", | |
1150 | .probe = esd_usb2_probe, | |
1151 | .disconnect = esd_usb2_disconnect, | |
1152 | .id_table = esd_usb2_table, | |
1153 | }; | |
1154 | ||
d632eb1b | 1155 | module_usb_driver(esd_usb2_driver); |