]> git.kernelconcepts.de Git - karo-tx-linux.git/blob - drivers/net/usb/cx82310_eth.c
mmc: pwrseq_simple: fix error path in mmc_pwrseq_simple_alloc
[karo-tx-linux.git] / drivers / net / usb / cx82310_eth.c
1 /*
2  * Driver for USB ethernet port of Conexant CX82310-based ADSL routers
3  * Copyright (C) 2010 by Ondrej Zary
4  * some parts inspired by the cxacru driver
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, see <http://www.gnu.org/licenses/>.
18  */
19
20 #include <linux/module.h>
21 #include <linux/netdevice.h>
22 #include <linux/etherdevice.h>
23 #include <linux/ethtool.h>
24 #include <linux/workqueue.h>
25 #include <linux/mii.h>
26 #include <linux/usb.h>
27 #include <linux/usb/usbnet.h>
28
29 enum cx82310_cmd {
30         CMD_START               = 0x84, /* no effect? */
31         CMD_STOP                = 0x85, /* no effect? */
32         CMD_GET_STATUS          = 0x90, /* returns nothing? */
33         CMD_GET_MAC_ADDR        = 0x91, /* read MAC address */
34         CMD_GET_LINK_STATUS     = 0x92, /* not useful, link is always up */
35         CMD_ETHERNET_MODE       = 0x99, /* unknown, needed during init */
36 };
37
38 enum cx82310_status {
39         STATUS_UNDEFINED,
40         STATUS_SUCCESS,
41         STATUS_ERROR,
42         STATUS_UNSUPPORTED,
43         STATUS_UNIMPLEMENTED,
44         STATUS_PARAMETER_ERROR,
45         STATUS_DBG_LOOPBACK,
46 };
47
48 #define CMD_PACKET_SIZE 64
49 /* first command after power on can take around 8 seconds */
50 #define CMD_TIMEOUT     15000
51 #define CMD_REPLY_RETRY 5
52
53 #define CX82310_MTU     1514
54 #define CMD_EP          0x01
55
56 /*
57  * execute control command
58  *  - optionally send some data (command parameters)
59  *  - optionally wait for the reply
60  *  - optionally read some data from the reply
61  */
62 static int cx82310_cmd(struct usbnet *dev, enum cx82310_cmd cmd, bool reply,
63                        u8 *wdata, int wlen, u8 *rdata, int rlen)
64 {
65         int actual_len, retries, ret;
66         struct usb_device *udev = dev->udev;
67         u8 *buf = kzalloc(CMD_PACKET_SIZE, GFP_KERNEL);
68
69         if (!buf)
70                 return -ENOMEM;
71
72         /* create command packet */
73         buf[0] = cmd;
74         if (wdata)
75                 memcpy(buf + 4, wdata, min_t(int, wlen, CMD_PACKET_SIZE - 4));
76
77         /* send command packet */
78         ret = usb_bulk_msg(udev, usb_sndbulkpipe(udev, CMD_EP), buf,
79                            CMD_PACKET_SIZE, &actual_len, CMD_TIMEOUT);
80         if (ret < 0) {
81                 dev_err(&dev->udev->dev, "send command %#x: error %d\n",
82                         cmd, ret);
83                 goto end;
84         }
85
86         if (reply) {
87                 /* wait for reply, retry if it's empty */
88                 for (retries = 0; retries < CMD_REPLY_RETRY; retries++) {
89                         ret = usb_bulk_msg(udev, usb_rcvbulkpipe(udev, CMD_EP),
90                                            buf, CMD_PACKET_SIZE, &actual_len,
91                                            CMD_TIMEOUT);
92                         if (ret < 0) {
93                                 dev_err(&dev->udev->dev,
94                                         "reply receive error %d\n", ret);
95                                 goto end;
96                         }
97                         if (actual_len > 0)
98                                 break;
99                 }
100                 if (actual_len == 0) {
101                         dev_err(&dev->udev->dev, "no reply to command %#x\n",
102                                 cmd);
103                         ret = -EIO;
104                         goto end;
105                 }
106                 if (buf[0] != cmd) {
107                         dev_err(&dev->udev->dev,
108                                 "got reply to command %#x, expected: %#x\n",
109                                 buf[0], cmd);
110                         ret = -EIO;
111                         goto end;
112                 }
113                 if (buf[1] != STATUS_SUCCESS) {
114                         dev_err(&dev->udev->dev, "command %#x failed: %#x\n",
115                                 cmd, buf[1]);
116                         ret = -EIO;
117                         goto end;
118                 }
119                 if (rdata)
120                         memcpy(rdata, buf + 4,
121                                min_t(int, rlen, CMD_PACKET_SIZE - 4));
122         }
123 end:
124         kfree(buf);
125         return ret;
126 }
127
128 #define partial_len     data[0]         /* length of partial packet data */
129 #define partial_rem     data[1]         /* remaining (missing) data length */
130 #define partial_data    data[2]         /* partial packet data */
131
132 static int cx82310_bind(struct usbnet *dev, struct usb_interface *intf)
133 {
134         int ret;
135         char buf[15];
136         struct usb_device *udev = dev->udev;
137
138         /* avoid ADSL modems - continue only if iProduct is "USB NET CARD" */
139         if (usb_string(udev, udev->descriptor.iProduct, buf, sizeof(buf)) > 0
140             && strcmp(buf, "USB NET CARD")) {
141                 dev_info(&udev->dev, "ignoring: probably an ADSL modem\n");
142                 return -ENODEV;
143         }
144
145         ret = usbnet_get_endpoints(dev, intf);
146         if (ret)
147                 return ret;
148
149         /*
150          * this must not include ethernet header as the device can send partial
151          * packets with no header (and sometimes even empty URBs)
152          */
153         dev->net->hard_header_len = 0;
154         /* we can send at most 1514 bytes of data (+ 2-byte header) per URB */
155         dev->hard_mtu = CX82310_MTU + 2;
156         /* we can receive URBs up to 4KB from the device */
157         dev->rx_urb_size = 4096;
158
159         dev->partial_data = (unsigned long) kmalloc(dev->hard_mtu, GFP_KERNEL);
160         if (!dev->partial_data)
161                 return -ENOMEM;
162
163         /* enable ethernet mode (?) */
164         ret = cx82310_cmd(dev, CMD_ETHERNET_MODE, true, "\x01", 1, NULL, 0);
165         if (ret) {
166                 dev_err(&udev->dev, "unable to enable ethernet mode: %d\n",
167                         ret);
168                 goto err;
169         }
170
171         /* get the MAC address */
172         ret = cx82310_cmd(dev, CMD_GET_MAC_ADDR, true, NULL, 0,
173                           dev->net->dev_addr, ETH_ALEN);
174         if (ret) {
175                 dev_err(&udev->dev, "unable to read MAC address: %d\n", ret);
176                 goto err;
177         }
178
179         /* start (does not seem to have any effect?) */
180         ret = cx82310_cmd(dev, CMD_START, false, NULL, 0, NULL, 0);
181         if (ret)
182                 goto err;
183
184         return 0;
185 err:
186         kfree((void *)dev->partial_data);
187         return ret;
188 }
189
190 static void cx82310_unbind(struct usbnet *dev, struct usb_interface *intf)
191 {
192         kfree((void *)dev->partial_data);
193 }
194
195 /*
196  * RX is NOT easy - we can receive multiple packets per skb, each having 2-byte
197  * packet length at the beginning.
198  * The last packet might be incomplete (when it crosses the 4KB URB size),
199  * continuing in the next skb (without any headers).
200  * If a packet has odd length, there is one extra byte at the end (before next
201  * packet or at the end of the URB).
202  */
203 static int cx82310_rx_fixup(struct usbnet *dev, struct sk_buff *skb)
204 {
205         int len;
206         struct sk_buff *skb2;
207
208         /*
209          * If the last skb ended with an incomplete packet, this skb contains
210          * end of that packet at the beginning.
211          */
212         if (dev->partial_rem) {
213                 len = dev->partial_len + dev->partial_rem;
214                 skb2 = alloc_skb(len, GFP_ATOMIC);
215                 if (!skb2)
216                         return 0;
217                 skb_put(skb2, len);
218                 memcpy(skb2->data, (void *)dev->partial_data,
219                        dev->partial_len);
220                 memcpy(skb2->data + dev->partial_len, skb->data,
221                        dev->partial_rem);
222                 usbnet_skb_return(dev, skb2);
223                 skb_pull(skb, (dev->partial_rem + 1) & ~1);
224                 dev->partial_rem = 0;
225                 if (skb->len < 2)
226                         return 1;
227         }
228
229         /* a skb can contain multiple packets */
230         while (skb->len > 1) {
231                 /* first two bytes are packet length */
232                 len = skb->data[0] | (skb->data[1] << 8);
233                 skb_pull(skb, 2);
234
235                 /* if last packet in the skb, let usbnet to process it */
236                 if (len == skb->len || len + 1 == skb->len) {
237                         skb_trim(skb, len);
238                         break;
239                 }
240
241                 if (len > CX82310_MTU) {
242                         dev_err(&dev->udev->dev, "RX packet too long: %d B\n",
243                                 len);
244                         return 0;
245                 }
246
247                 /* incomplete packet, save it for the next skb */
248                 if (len > skb->len) {
249                         dev->partial_len = skb->len;
250                         dev->partial_rem = len - skb->len;
251                         memcpy((void *)dev->partial_data, skb->data,
252                                dev->partial_len);
253                         skb_pull(skb, skb->len);
254                         break;
255                 }
256
257                 skb2 = alloc_skb(len, GFP_ATOMIC);
258                 if (!skb2)
259                         return 0;
260                 skb_put(skb2, len);
261                 memcpy(skb2->data, skb->data, len);
262                 /* process the packet */
263                 usbnet_skb_return(dev, skb2);
264
265                 skb_pull(skb, (len + 1) & ~1);
266         }
267
268         /* let usbnet process the last packet */
269         return 1;
270 }
271
272 /* TX is easy, just add 2 bytes of length at the beginning */
273 static struct sk_buff *cx82310_tx_fixup(struct usbnet *dev, struct sk_buff *skb,
274                                        gfp_t flags)
275 {
276         int len = skb->len;
277
278         if (skb_headroom(skb) < 2) {
279                 struct sk_buff *skb2 = skb_copy_expand(skb, 2, 0, flags);
280                 dev_kfree_skb_any(skb);
281                 skb = skb2;
282                 if (!skb)
283                         return NULL;
284         }
285         skb_push(skb, 2);
286
287         skb->data[0] = len;
288         skb->data[1] = len >> 8;
289
290         return skb;
291 }
292
293
294 static const struct driver_info cx82310_info = {
295         .description    = "Conexant CX82310 USB ethernet",
296         .flags          = FLAG_ETHER,
297         .bind           = cx82310_bind,
298         .unbind         = cx82310_unbind,
299         .rx_fixup       = cx82310_rx_fixup,
300         .tx_fixup       = cx82310_tx_fixup,
301 };
302
303 static const struct usb_device_id products[] = {
304         {
305                 USB_DEVICE_AND_INTERFACE_INFO(0x0572, 0xcb01, 0xff, 0, 0),
306                 .driver_info = (unsigned long) &cx82310_info
307         },
308         { },
309 };
310 MODULE_DEVICE_TABLE(usb, products);
311
312 static struct usb_driver cx82310_driver = {
313         .name           = "cx82310_eth",
314         .id_table       = products,
315         .probe          = usbnet_probe,
316         .disconnect     = usbnet_disconnect,
317         .suspend        = usbnet_suspend,
318         .resume         = usbnet_resume,
319         .disable_hub_initiated_lpm = 1,
320 };
321
322 module_usb_driver(cx82310_driver);
323
324 MODULE_AUTHOR("Ondrej Zary");
325 MODULE_DESCRIPTION("Conexant CX82310-based ADSL router USB ethernet driver");
326 MODULE_LICENSE("GPL");