]> bbs.cooldavid.org Git - net-next-2.6.git/blob - drivers/net/wimax/i2400m/usb.c
wimax: replace uses of __constant_{endian}
[net-next-2.6.git] / drivers / net / wimax / i2400m / usb.c
1 /*
2  * Intel Wireless WiMAX Connection 2400m
3  * Linux driver model glue for USB device, reset & fw upload
4  *
5  *
6  * Copyright (C) 2007-2008 Intel Corporation <linux-wimax@intel.com>
7  * Inaky Perez-Gonzalez <inaky.perez-gonzalez@intel.com>
8  * Yanir Lubetkin <yanirx.lubetkin@intel.com>
9  *
10  * This program is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU General Public License version
12  * 2 as published by the Free Software Foundation.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
22  * 02110-1301, USA.
23  *
24  *
25  * See i2400m-usb.h for a general description of this driver.
26  *
27  * This file implements driver model glue, and hook ups for the
28  * generic driver to implement the bus-specific functions (device
29  * communication setup/tear down, firmware upload and resetting).
30  *
31  * ROADMAP
32  *
33  * i2400mu_probe()
34  *   alloc_netdev()...
35  *     i2400mu_netdev_setup()
36  *       i2400mu_init()
37  *       i2400m_netdev_setup()
38  *   i2400m_setup()...
39  *
40  * i2400mu_disconnect
41  *   i2400m_release()
42  *   free_netdev()
43  *
44  * i2400mu_suspend()
45  *   i2400m_cmd_enter_powersave()
46  *   i2400mu_notification_release()
47  *
48  * i2400mu_resume()
49  *   i2400mu_notification_setup()
50  *
51  * i2400mu_bus_dev_start()        Called by i2400m_dev_start() [who is
52  *   i2400mu_tx_setup()           called by i2400m_setup()]
53  *   i2400mu_rx_setup()
54  *   i2400mu_notification_setup()
55  *
56  * i2400mu_bus_dev_stop()         Called by i2400m_dev_stop() [who is
57  *   i2400mu_notification_release()  called by i2400m_release()]
58  *   i2400mu_rx_release()
59  *   i2400mu_tx_release()
60  *
61  * i2400mu_bus_reset()            Called by i2400m->bus_reset
62  *   __i2400mu_reset()
63  *     __i2400mu_send_barker()
64  *   usb_reset_device()
65  */
66 #include "i2400m-usb.h"
67 #include <linux/wimax/i2400m.h>
68 #include <linux/debugfs.h>
69
70
71 #define D_SUBMODULE usb
72 #include "usb-debug-levels.h"
73
74
75 /* Our firmware file name */
76 #define I2400MU_FW_FILE_NAME "i2400m-fw-usb-" I2400M_FW_VERSION ".sbcf"
77
78 static
79 int i2400mu_bus_dev_start(struct i2400m *i2400m)
80 {
81         int result;
82         struct i2400mu *i2400mu = container_of(i2400m, struct i2400mu, i2400m);
83         struct device *dev = &i2400mu->usb_iface->dev;
84
85         d_fnstart(3, dev, "(i2400m %p)\n", i2400m);
86         result = i2400mu_tx_setup(i2400mu);
87         if (result < 0)
88                 goto error_usb_tx_setup;
89         result = i2400mu_rx_setup(i2400mu);
90         if (result < 0)
91                 goto error_usb_rx_setup;
92         result = i2400mu_notification_setup(i2400mu);
93         if (result < 0)
94                 goto error_notif_setup;
95         d_fnend(3, dev, "(i2400m %p) = %d\n", i2400m, result);
96         return result;
97
98 error_notif_setup:
99         i2400mu_rx_release(i2400mu);
100 error_usb_rx_setup:
101         i2400mu_tx_release(i2400mu);
102 error_usb_tx_setup:
103         d_fnend(3, dev, "(i2400m %p) = void\n", i2400m);
104         return result;
105 }
106
107
108 static
109 void i2400mu_bus_dev_stop(struct i2400m *i2400m)
110 {
111         struct i2400mu *i2400mu = container_of(i2400m, struct i2400mu, i2400m);
112         struct device *dev = &i2400mu->usb_iface->dev;
113
114         d_fnstart(3, dev, "(i2400m %p)\n", i2400m);
115         i2400mu_notification_release(i2400mu);
116         i2400mu_rx_release(i2400mu);
117         i2400mu_tx_release(i2400mu);
118         d_fnend(3, dev, "(i2400m %p) = void\n", i2400m);
119 }
120
121
122 /*
123  * Sends a barker buffer to the device
124  *
125  * This helper will allocate a kmalloced buffer and use it to transmit
126  * (then free it). Reason for this is that other arches cannot use
127  * stack/vmalloc/text areas for DMA transfers.
128  *
129  * Error recovery here is simpler: anything is considered a hard error
130  * and will move the reset code to use a last-resort bus-based reset.
131  */
132 static
133 int __i2400mu_send_barker(struct i2400mu *i2400mu,
134                           const __le32 *barker,
135                           size_t barker_size,
136                           unsigned endpoint)
137 {
138         struct usb_endpoint_descriptor *epd = NULL;
139         int pipe, actual_len, ret;
140         struct device *dev = &i2400mu->usb_iface->dev;
141         void *buffer;
142         int do_autopm = 1;
143
144         ret = usb_autopm_get_interface(i2400mu->usb_iface);
145         if (ret < 0) {
146                 dev_err(dev, "RESET: can't get autopm: %d\n", ret);
147                 do_autopm = 0;
148         }
149         ret = -ENOMEM;
150         buffer = kmalloc(barker_size, GFP_KERNEL);
151         if (buffer == NULL)
152                 goto error_kzalloc;
153         epd = usb_get_epd(i2400mu->usb_iface, endpoint);
154         pipe = usb_sndbulkpipe(i2400mu->usb_dev, epd->bEndpointAddress);
155         memcpy(buffer, barker, barker_size);
156         ret = usb_bulk_msg(i2400mu->usb_dev, pipe, buffer, barker_size,
157                            &actual_len, HZ);
158         if (ret < 0) {
159                 if (ret != -EINVAL)
160                         dev_err(dev, "E: barker error: %d\n", ret);
161         } else if (actual_len != barker_size) {
162                 dev_err(dev, "E: only %d bytes transmitted\n", actual_len);
163                 ret = -EIO;
164         }
165         kfree(buffer);
166 error_kzalloc:
167         if (do_autopm)
168                 usb_autopm_put_interface(i2400mu->usb_iface);
169         return ret;
170 }
171
172
173 /*
174  * Reset a device at different levels (warm, cold or bus)
175  *
176  * @i2400m: device descriptor
177  * @reset_type: soft, warm or bus reset (I2400M_RT_WARM/SOFT/BUS)
178  *
179  * Warm and cold resets get a USB reset if they fail.
180  *
181  * Warm reset:
182  *
183  * The device will be fully reset internally, but won't be
184  * disconnected from the USB bus (so no reenumeration will
185  * happen). Firmware upload will be neccessary.
186  *
187  * The device will send a reboot barker in the notification endpoint
188  * that will trigger the driver to reinitialize the state
189  * automatically from notif.c:i2400m_notification_grok() into
190  * i2400m_dev_bootstrap_delayed().
191  *
192  * Cold and bus (USB) reset:
193  *
194  * The device will be fully reset internally, disconnected from the
195  * USB bus an a reenumeration will happen. Firmware upload will be
196  * neccessary. Thus, we don't do any locking or struct
197  * reinitialization, as we are going to be fully disconnected and
198  * reenumerated.
199  *
200  * Note we need to return -ENODEV if a warm reset was requested and we
201  * had to resort to a bus reset. See i2400m_op_reset(), wimax_reset()
202  * and wimax_dev->op_reset.
203  *
204  * WARNING: no driver state saved/fixed
205  */
206 static
207 int i2400mu_bus_reset(struct i2400m *i2400m, enum i2400m_reset_type rt)
208 {
209         int result;
210         struct i2400mu *i2400mu =
211                 container_of(i2400m, struct i2400mu, i2400m);
212         struct device *dev = i2400m_dev(i2400m);
213         static const __le32 i2400m_WARM_BOOT_BARKER[4] = {
214                 cpu_to_le32(I2400M_WARM_RESET_BARKER),
215                 cpu_to_le32(I2400M_WARM_RESET_BARKER),
216                 cpu_to_le32(I2400M_WARM_RESET_BARKER),
217                 cpu_to_le32(I2400M_WARM_RESET_BARKER),
218         };
219         static const __le32 i2400m_COLD_BOOT_BARKER[4] = {
220                 cpu_to_le32(I2400M_COLD_RESET_BARKER),
221                 cpu_to_le32(I2400M_COLD_RESET_BARKER),
222                 cpu_to_le32(I2400M_COLD_RESET_BARKER),
223                 cpu_to_le32(I2400M_COLD_RESET_BARKER),
224         };
225
226         d_fnstart(3, dev, "(i2400m %p rt %u)\n", i2400m, rt);
227         if (rt == I2400M_RT_WARM)
228                 result = __i2400mu_send_barker(i2400mu, i2400m_WARM_BOOT_BARKER,
229                                                sizeof(i2400m_WARM_BOOT_BARKER),
230                                                I2400MU_EP_BULK_OUT);
231         else if (rt == I2400M_RT_COLD)
232                 result = __i2400mu_send_barker(i2400mu, i2400m_COLD_BOOT_BARKER,
233                                                sizeof(i2400m_COLD_BOOT_BARKER),
234                                                I2400MU_EP_RESET_COLD);
235         else if (rt == I2400M_RT_BUS) {
236 do_bus_reset:
237                 result = usb_reset_device(i2400mu->usb_dev);
238                 switch (result) {
239                 case 0:
240                 case -EINVAL:   /* device is gone */
241                 case -ENODEV:
242                 case -ENOENT:
243                 case -ESHUTDOWN:
244                         result = rt == I2400M_RT_WARM ? -ENODEV : 0;
245                         break;  /* We assume the device is disconnected */
246                 default:
247                         dev_err(dev, "USB reset failed (%d), giving up!\n",
248                                 result);
249                 }
250         } else
251                 BUG();
252         if (result < 0
253             && result != -EINVAL        /* device is gone */
254             && rt != I2400M_RT_BUS) {
255                 dev_err(dev, "%s reset failed (%d); trying USB reset\n",
256                         rt == I2400M_RT_WARM ? "warm" : "cold", result);
257                 rt = I2400M_RT_BUS;
258                 goto do_bus_reset;
259         }
260         d_fnend(3, dev, "(i2400m %p rt %u) = %d\n", i2400m, rt, result);
261         return result;
262 }
263
264
265 static
266 void i2400mu_netdev_setup(struct net_device *net_dev)
267 {
268         struct i2400m *i2400m = net_dev_to_i2400m(net_dev);
269         struct i2400mu *i2400mu = container_of(i2400m, struct i2400mu, i2400m);
270         i2400mu_init(i2400mu);
271         i2400m_netdev_setup(net_dev);
272 }
273
274
275 /*
276  * Debug levels control; see debug.h
277  */
278 struct d_level D_LEVEL[] = {
279         D_SUBMODULE_DEFINE(usb),
280         D_SUBMODULE_DEFINE(fw),
281         D_SUBMODULE_DEFINE(notif),
282         D_SUBMODULE_DEFINE(rx),
283         D_SUBMODULE_DEFINE(tx),
284 };
285 size_t D_LEVEL_SIZE = ARRAY_SIZE(D_LEVEL);
286
287
288 #define __debugfs_register(prefix, name, parent)                        \
289 do {                                                                    \
290         result = d_level_register_debugfs(prefix, name, parent);        \
291         if (result < 0)                                                 \
292                 goto error;                                             \
293 } while (0)
294
295
296 static
297 int i2400mu_debugfs_add(struct i2400mu *i2400mu)
298 {
299         int result;
300         struct device *dev = &i2400mu->usb_iface->dev;
301         struct dentry *dentry = i2400mu->i2400m.wimax_dev.debugfs_dentry;
302         struct dentry *fd;
303
304         dentry = debugfs_create_dir("i2400m-usb", dentry);
305         result = PTR_ERR(dentry);
306         if (IS_ERR(dentry)) {
307                 if (result == -ENODEV)
308                         result = 0;     /* No debugfs support */
309                 goto error;
310         }
311         i2400mu->debugfs_dentry = dentry;
312         __debugfs_register("dl_", usb, dentry);
313         __debugfs_register("dl_", fw, dentry);
314         __debugfs_register("dl_", notif, dentry);
315         __debugfs_register("dl_", rx, dentry);
316         __debugfs_register("dl_", tx, dentry);
317
318         /* Don't touch these if you don't know what you are doing */
319         fd = debugfs_create_u8("rx_size_auto_shrink", 0600, dentry,
320                                &i2400mu->rx_size_auto_shrink);
321         result = PTR_ERR(fd);
322         if (IS_ERR(fd) && result != -ENODEV) {
323                 dev_err(dev, "Can't create debugfs entry "
324                         "rx_size_auto_shrink: %d\n", result);
325                 goto error;
326         }
327
328         fd = debugfs_create_size_t("rx_size", 0600, dentry,
329                                    &i2400mu->rx_size);
330         result = PTR_ERR(fd);
331         if (IS_ERR(fd) && result != -ENODEV) {
332                 dev_err(dev, "Can't create debugfs entry "
333                         "rx_size: %d\n", result);
334                 goto error;
335         }
336
337         return 0;
338
339 error:
340         debugfs_remove_recursive(i2400mu->debugfs_dentry);
341         return result;
342 }
343
344
345 /*
346  * Probe a i2400m interface and register it
347  *
348  * @iface:   USB interface to link to
349  * @id:      USB class/subclass/protocol id
350  * @returns: 0 if ok, < 0 errno code on error.
351  *
352  * Alloc a net device, initialize the bus-specific details and then
353  * calls the bus-generic initialization routine. That will register
354  * the wimax and netdev devices, upload the firmware [using
355  * _bus_bm_*()], call _bus_dev_start() to finalize the setup of the
356  * communication with the device and then will start to talk to it to
357  * finnish setting it up.
358  */
359 static
360 int i2400mu_probe(struct usb_interface *iface,
361                   const struct usb_device_id *id)
362 {
363         int result;
364         struct net_device *net_dev;
365         struct device *dev = &iface->dev;
366         struct i2400m *i2400m;
367         struct i2400mu *i2400mu;
368         struct usb_device *usb_dev = interface_to_usbdev(iface);
369
370         if (usb_dev->speed != USB_SPEED_HIGH)
371                 dev_err(dev, "device not connected as high speed\n");
372
373         /* Allocate instance [calls i2400m_netdev_setup() on it]. */
374         result = -ENOMEM;
375         net_dev = alloc_netdev(sizeof(*i2400mu), "wmx%d",
376                                i2400mu_netdev_setup);
377         if (net_dev == NULL) {
378                 dev_err(dev, "no memory for network device instance\n");
379                 goto error_alloc_netdev;
380         }
381         SET_NETDEV_DEV(net_dev, dev);
382         i2400m = net_dev_to_i2400m(net_dev);
383         i2400mu = container_of(i2400m, struct i2400mu, i2400m);
384         i2400m->wimax_dev.net_dev = net_dev;
385         i2400mu->usb_dev = usb_get_dev(usb_dev);
386         i2400mu->usb_iface = iface;
387         usb_set_intfdata(iface, i2400mu);
388
389         i2400m->bus_tx_block_size = I2400MU_BLK_SIZE;
390         i2400m->bus_pl_size_max = I2400MU_PL_SIZE_MAX;
391         i2400m->bus_dev_start = i2400mu_bus_dev_start;
392         i2400m->bus_dev_stop = i2400mu_bus_dev_stop;
393         i2400m->bus_tx_kick = i2400mu_bus_tx_kick;
394         i2400m->bus_reset = i2400mu_bus_reset;
395         i2400m->bus_bm_cmd_send = i2400mu_bus_bm_cmd_send;
396         i2400m->bus_bm_wait_for_ack = i2400mu_bus_bm_wait_for_ack;
397         i2400m->bus_fw_name = I2400MU_FW_FILE_NAME;
398         i2400m->bus_bm_mac_addr_impaired = 0;
399
400 #ifdef CONFIG_PM
401         iface->needs_remote_wakeup = 1;         /* autosuspend (15s delay) */
402         device_init_wakeup(dev, 1);
403         usb_autopm_enable(i2400mu->usb_iface);
404         usb_dev->autosuspend_delay = 15 * HZ;
405         usb_dev->autosuspend_disabled = 0;
406 #endif
407
408         result = i2400m_setup(i2400m, I2400M_BRI_MAC_REINIT);
409         if (result < 0) {
410                 dev_err(dev, "cannot setup device: %d\n", result);
411                 goto error_setup;
412         }
413         result = i2400mu_debugfs_add(i2400mu);
414         if (result < 0) {
415                 dev_err(dev, "Can't register i2400mu's debugfs: %d\n", result);
416                 goto error_debugfs_add;
417         }
418         return 0;
419
420 error_debugfs_add:
421         i2400m_release(i2400m);
422 error_setup:
423         usb_set_intfdata(iface, NULL);
424         usb_put_dev(i2400mu->usb_dev);
425         free_netdev(net_dev);
426 error_alloc_netdev:
427         return result;
428 }
429
430
431 /*
432  * Disconect a i2400m from the system.
433  *
434  * i2400m_stop() has been called before, so al the rx and tx contexts
435  * have been taken down already. Make sure the queue is stopped,
436  * unregister netdev and i2400m, free and kill.
437  */
438 static
439 void i2400mu_disconnect(struct usb_interface *iface)
440 {
441         struct i2400mu *i2400mu = usb_get_intfdata(iface);
442         struct i2400m *i2400m = &i2400mu->i2400m;
443         struct net_device *net_dev = i2400m->wimax_dev.net_dev;
444         struct device *dev = &iface->dev;
445
446         d_fnstart(3, dev, "(iface %p i2400m %p)\n", iface, i2400m);
447
448         debugfs_remove_recursive(i2400mu->debugfs_dentry);
449         i2400m_release(i2400m);
450         usb_set_intfdata(iface, NULL);
451         usb_put_dev(i2400mu->usb_dev);
452         free_netdev(net_dev);
453         d_fnend(3, dev, "(iface %p i2400m %p) = void\n", iface, i2400m);
454 }
455
456
457 /*
458  * Get the device ready for USB port or system standby and hibernation
459  *
460  * USB port and system standby are handled the same.
461  *
462  * When the system hibernates, the USB device is powered down and then
463  * up, so we don't really have to do much here, as it will be seen as
464  * a reconnect. Still for simplicity we consider this case the same as
465  * suspend, so that the device has a chance to do notify the base
466  * station (if connected).
467  *
468  * So at the end, the three cases require common handling.
469  *
470  * If at the time of this call the device's firmware is not loaded,
471  * nothing has to be done.
472  *
473  * If the firmware is loaded, we need to:
474  *
475  *  - tell the device to go into host interface power save mode, wait
476  *    for it to ack
477  *
478  *    This is quite more interesting than it is; we need to execute a
479  *    command, but this time, we don't want the code in usb-{tx,rx}.c
480  *    to call the usb_autopm_get/put_interface() barriers as it'd
481  *    deadlock, so we need to decrement i2400mu->do_autopm, that acts
482  *    as a poor man's semaphore. Ugly, but it works.
483  *
484  *    As well, the device might refuse going to sleep for whichever
485  *    reason. In this case we just fail. For system suspend/hibernate,
486  *    we *can't* fail. We look at usb_dev->auto_pm to see if the
487  *    suspend call comes from the USB stack or from the system and act
488  *    in consequence.
489  *
490  *  - stop the notification endpoint polling
491  */
492 static
493 int i2400mu_suspend(struct usb_interface *iface, pm_message_t pm_msg)
494 {
495         int result = 0;
496         struct device *dev = &iface->dev;
497         struct i2400mu *i2400mu = usb_get_intfdata(iface);
498 #ifdef CONFIG_PM
499         struct usb_device *usb_dev = i2400mu->usb_dev;
500 #endif
501         struct i2400m *i2400m = &i2400mu->i2400m;
502
503         d_fnstart(3, dev, "(iface %p pm_msg %u)\n", iface, pm_msg.event);
504         if (i2400m->updown == 0)
505                 goto no_firmware;
506         d_printf(1, dev, "fw up, requesting standby\n");
507         atomic_dec(&i2400mu->do_autopm);
508         result = i2400m_cmd_enter_powersave(i2400m);
509         atomic_inc(&i2400mu->do_autopm);
510 #ifdef CONFIG_PM
511         if (result < 0 && usb_dev->auto_pm == 0) {
512                 /* System suspend, can't fail */
513                 dev_err(dev, "failed to suspend, will reset on resume\n");
514                 result = 0;
515         }
516 #endif
517         if (result < 0)
518                 goto error_enter_powersave;
519         i2400mu_notification_release(i2400mu);
520         d_printf(1, dev, "fw up, got standby\n");
521 error_enter_powersave:
522 no_firmware:
523         d_fnend(3, dev, "(iface %p pm_msg %u) = %d\n",
524                 iface, pm_msg.event, result);
525         return result;
526 }
527
528
529 static
530 int i2400mu_resume(struct usb_interface *iface)
531 {
532         int ret = 0;
533         struct device *dev = &iface->dev;
534         struct i2400mu *i2400mu = usb_get_intfdata(iface);
535         struct i2400m *i2400m = &i2400mu->i2400m;
536
537         d_fnstart(3, dev, "(iface %p)\n", iface);
538         if (i2400m->updown == 0) {
539                 d_printf(1, dev, "fw was down, no resume neeed\n");
540                 goto out;
541         }
542         d_printf(1, dev, "fw was up, resuming\n");
543         i2400mu_notification_setup(i2400mu);
544         /* USB has flow control, so we don't need to give it time to
545          * come back; otherwise, we'd use something like a get-state
546          * command... */
547 out:
548         d_fnend(3, dev, "(iface %p) = %d\n", iface, ret);
549         return ret;
550 }
551
552
553 static
554 struct usb_device_id i2400mu_id_table[] = {
555         { USB_DEVICE(0x8086, 0x0181) },
556         { USB_DEVICE(0x8086, 0x1403) },
557         { USB_DEVICE(0x8086, 0x1405) },
558         { USB_DEVICE(0x8086, 0x0180) },
559         { USB_DEVICE(0x8086, 0x0182) },
560         { USB_DEVICE(0x8086, 0x1406) },
561         { USB_DEVICE(0x8086, 0x1403) },
562         { },
563 };
564 MODULE_DEVICE_TABLE(usb, i2400mu_id_table);
565
566
567 static
568 struct usb_driver i2400mu_driver = {
569         .name = KBUILD_MODNAME,
570         .suspend = i2400mu_suspend,
571         .resume = i2400mu_resume,
572         .probe = i2400mu_probe,
573         .disconnect = i2400mu_disconnect,
574         .id_table = i2400mu_id_table,
575         .supports_autosuspend = 1,
576 };
577
578 static
579 int __init i2400mu_driver_init(void)
580 {
581         return usb_register(&i2400mu_driver);
582 }
583 module_init(i2400mu_driver_init);
584
585
586 static
587 void __exit i2400mu_driver_exit(void)
588 {
589         flush_scheduled_work(); /* for the stuff we schedule from sysfs.c */
590         usb_deregister(&i2400mu_driver);
591 }
592 module_exit(i2400mu_driver_exit);
593
594 MODULE_AUTHOR("Intel Corporation <linux-wimax@intel.com>");
595 MODULE_DESCRIPTION("Intel 2400M WiMAX networking for USB");
596 MODULE_LICENSE("GPL");
597 MODULE_FIRMWARE(I2400MU_FW_FILE_NAME);