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