Merge to Fedora kernel-2.6.18-1.2224_FC5 patched with stable patch-2.6.18.1-vs2.0...
[linux-2.6.git] / drivers / isdn / hisax / st5481_init.c
1 /*
2  * Driver for ST5481 USB ISDN modem
3  *
4  * Author       Frode Isaksen
5  * Copyright    2001 by Frode Isaksen      <fisaksen@bewan.com>
6  *              2001 by Kai Germaschewski  <kai.germaschewski@gmx.de>
7  * 
8  * This software may be used and distributed according to the terms
9  * of the GNU General Public License, incorporated herein by reference.
10  *
11  */
12
13 /* 
14  * TODO:
15  *
16  * b layer1 delay?
17  * hotplug / unregister issues
18  * mod_inc/dec_use_count
19  * unify parts of d/b channel usb handling
20  * file header
21  * avoid copy to isoc buffer?
22  * improve usb delay?
23  * merge l1 state machines?
24  * clean up debug
25  */
26
27 #include <linux/module.h>
28 #include <linux/init.h>
29 #include <linux/usb.h>
30 #include <linux/slab.h>
31 #include "st5481.h"
32
33 MODULE_DESCRIPTION("ISDN4Linux: driver for ST5481 USB ISDN adapter");
34 MODULE_AUTHOR("Frode Isaksen");
35 MODULE_LICENSE("GPL");
36
37 static int protocol = 2;       /* EURO-ISDN Default */
38 module_param(protocol, int, 0);
39
40 static int number_of_leds = 2;       /* 2 LEDs on the adpater default */
41 module_param(number_of_leds, int, 0);
42
43 #ifdef CONFIG_HISAX_DEBUG
44 static int debug = 0;
45 module_param(debug, int, 0);
46 #endif
47 int st5481_debug;
48
49 static LIST_HEAD(adapter_list);
50
51 /* ======================================================================
52  * registration/deregistration with the USB layer
53  */
54
55 /*
56  * This function will be called when the adapter is plugged
57  * into the USB bus.
58  */
59 static int probe_st5481(struct usb_interface *intf,
60                         const struct usb_device_id *id)
61 {
62         struct usb_device *dev = interface_to_usbdev(intf);
63         struct st5481_adapter *adapter;
64         struct hisax_b_if *b_if[2];
65         int retval, i;
66
67         printk(KERN_INFO "st541: found adapter VendorId %04x, ProductId %04x, LEDs %d\n",
68              le16_to_cpu(dev->descriptor.idVendor),
69              le16_to_cpu(dev->descriptor.idProduct),
70              number_of_leds);
71
72         adapter = kmalloc(sizeof(struct st5481_adapter), GFP_KERNEL);
73         if (!adapter)
74                 return -ENOMEM;
75
76         memset(adapter, 0, sizeof(struct st5481_adapter));
77
78         adapter->number_of_leds = number_of_leds;
79         adapter->usb_dev = dev;
80
81         adapter->hisax_d_if.owner = THIS_MODULE;
82         adapter->hisax_d_if.ifc.priv = adapter;
83         adapter->hisax_d_if.ifc.l2l1 = st5481_d_l2l1;
84
85         for (i = 0; i < 2; i++) {
86                 adapter->bcs[i].adapter = adapter;
87                 adapter->bcs[i].channel = i;
88                 adapter->bcs[i].b_if.ifc.priv = &adapter->bcs[i];
89                 adapter->bcs[i].b_if.ifc.l2l1 = st5481_b_l2l1;
90         }
91         list_add(&adapter->list, &adapter_list);
92
93         retval = st5481_setup_usb(adapter);
94         if (retval < 0)
95                 goto err;
96
97         retval = st5481_setup_d(adapter);
98         if (retval < 0)
99                 goto err_usb;
100
101         retval = st5481_setup_b(&adapter->bcs[0]);
102         if (retval < 0)
103                 goto err_d;
104
105         retval = st5481_setup_b(&adapter->bcs[1]);
106         if (retval < 0)
107                 goto err_b;
108
109         for (i = 0; i < 2; i++)
110                 b_if[i] = &adapter->bcs[i].b_if;
111
112         hisax_register(&adapter->hisax_d_if, b_if, "st5481_usb", protocol);
113         st5481_start(adapter);
114
115         usb_set_intfdata(intf, adapter);
116         return 0;
117
118  err_b:
119         st5481_release_b(&adapter->bcs[0]);
120  err_d:
121         st5481_release_d(adapter);
122  err_usb:
123         st5481_release_usb(adapter);
124  err:
125         return -EIO;
126 }
127
128 /*
129  * This function will be called when the adapter is removed
130  * from the USB bus.
131  */
132 static void disconnect_st5481(struct usb_interface *intf)
133 {
134         struct st5481_adapter *adapter = usb_get_intfdata(intf);
135
136         DBG(1,"");
137
138         usb_set_intfdata(intf, NULL);
139         if (!adapter)
140                 return;
141         
142         list_del(&adapter->list);
143
144         st5481_stop(adapter);
145         st5481_release_b(&adapter->bcs[1]);
146         st5481_release_b(&adapter->bcs[0]);
147         st5481_release_d(adapter);
148         // we would actually better wait for completion of outstanding urbs
149         mdelay(2);
150         st5481_release_usb(adapter);
151
152         hisax_unregister(&adapter->hisax_d_if);
153
154         kfree(adapter);
155 }
156
157 /*
158  * The last 4 bits in the Product Id is set with 4 pins on the chip.
159  */
160 static struct usb_device_id st5481_ids[] = {
161         { USB_DEVICE(ST_VENDOR_ID, ST5481_PRODUCT_ID+0x0) },
162         { USB_DEVICE(ST_VENDOR_ID, ST5481_PRODUCT_ID+0x1) },
163         { USB_DEVICE(ST_VENDOR_ID, ST5481_PRODUCT_ID+0x2) },
164         { USB_DEVICE(ST_VENDOR_ID, ST5481_PRODUCT_ID+0x3) },
165         { USB_DEVICE(ST_VENDOR_ID, ST5481_PRODUCT_ID+0x4) },
166         { USB_DEVICE(ST_VENDOR_ID, ST5481_PRODUCT_ID+0x5) },
167         { USB_DEVICE(ST_VENDOR_ID, ST5481_PRODUCT_ID+0x6) },
168         { USB_DEVICE(ST_VENDOR_ID, ST5481_PRODUCT_ID+0x7) },
169         { USB_DEVICE(ST_VENDOR_ID, ST5481_PRODUCT_ID+0x8) },
170         { USB_DEVICE(ST_VENDOR_ID, ST5481_PRODUCT_ID+0x9) },
171         { USB_DEVICE(ST_VENDOR_ID, ST5481_PRODUCT_ID+0xA) },
172         { USB_DEVICE(ST_VENDOR_ID, ST5481_PRODUCT_ID+0xB) },
173         { USB_DEVICE(ST_VENDOR_ID, ST5481_PRODUCT_ID+0xC) },
174         { USB_DEVICE(ST_VENDOR_ID, ST5481_PRODUCT_ID+0xD) },
175         { USB_DEVICE(ST_VENDOR_ID, ST5481_PRODUCT_ID+0xE) },
176         { USB_DEVICE(ST_VENDOR_ID, ST5481_PRODUCT_ID+0xF) },
177         { }
178 };
179 MODULE_DEVICE_TABLE (usb, st5481_ids);
180
181 static struct usb_driver st5481_usb_driver = {
182         .name =         "st5481_usb",
183         .probe =        probe_st5481,
184         .disconnect =   disconnect_st5481,
185         .id_table =     st5481_ids,
186 };
187
188 static int __init st5481_usb_init(void)
189 {
190         int retval;
191
192 #ifdef CONFIG_HISAX_DEBUG
193         st5481_debug = debug;
194 #endif
195
196         printk(KERN_INFO "hisax_st5481: ST5481 USB ISDN driver $Revision: 2.4.2.3 $\n");
197
198         retval = st5481_d_init();
199         if (retval < 0)
200                 goto out;
201
202         retval = usb_register(&st5481_usb_driver);
203         if (retval < 0)
204                 goto out_d_exit;
205
206         return 0;
207
208  out_d_exit:
209         st5481_d_exit();
210  out:
211         return retval;
212 }
213
214 static void __exit st5481_usb_exit(void)
215 {
216         usb_deregister(&st5481_usb_driver);
217         st5481_d_exit();
218 }
219
220 module_init(st5481_usb_init);
221 module_exit(st5481_usb_exit);