diff options
author | Duncan Sands <duncan.sands@math.u-psud.fr> | 2005-05-11 14:24:03 -0400 |
---|---|---|
committer | Greg Kroah-Hartman <gregkh@suse.de> | 2005-06-27 17:43:55 -0400 |
commit | c59bba75fa500f13ef14215d599ee0d7faa1b954 (patch) | |
tree | b37daba6cdb31635173e781a2c742f4190ff08e3 | |
parent | d49d431744007cec0ee1a3ade96f9e0f100c7907 (diff) |
[PATCH] USB ATM: new usbatm core
Rework the core usbatm code: minidrivers (i.e. drivers for particular
modems) now register themselves with the usbatm core, supplying methods
for binding/unbinding etc. The design was inspired by usb-serial and
usbnet. At the same time, more common code from the speedtch and
cxacru (patch 3/5) drivers was generalized and moved into the core. The
transmission and reception parts have been unified and simplified. Since
this is a major change and I don't like underscores in file names,
usb_atm.[ch] has been renamed usbatm.[ch].
Many thanks to Roman Kagan, who did a lot of the coding.
Signed-off-by: Duncan Sands <baldrick@free.fr>
Signed-off-by: Greg Kroah-Hartman <gregkh@suse.de>
-rw-r--r-- | drivers/usb/atm/usb_atm.c | 1188 | ||||
-rw-r--r-- | drivers/usb/atm/usb_atm.h | 176 | ||||
-rw-r--r-- | drivers/usb/atm/usbatm.c | 1231 | ||||
-rw-r--r-- | drivers/usb/atm/usbatm.h | 183 |
4 files changed, 1414 insertions, 1364 deletions
diff --git a/drivers/usb/atm/usb_atm.c b/drivers/usb/atm/usb_atm.c deleted file mode 100644 index a4cd4476d49..00000000000 --- a/drivers/usb/atm/usb_atm.c +++ /dev/null | |||
@@ -1,1188 +0,0 @@ | |||
1 | /****************************************************************************** | ||
2 | * usb_atm.c - Generic USB xDSL driver core | ||
3 | * | ||
4 | * Copyright (C) 2001, Alcatel | ||
5 | * Copyright (C) 2003, Duncan Sands, SolNegro, Josep Comas | ||
6 | * Copyright (C) 2004, David Woodhouse | ||
7 | * | ||
8 | * This program is free software; you can redistribute it and/or modify it | ||
9 | * under the terms of the GNU General Public License as published by the Free | ||
10 | * Software Foundation; either version 2 of the License, or (at your option) | ||
11 | * any later version. | ||
12 | * | ||
13 | * This program is distributed in the hope that it will be useful, but WITHOUT | ||
14 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | ||
15 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for | ||
16 | * more details. | ||
17 | * | ||
18 | * You should have received a copy of the GNU General Public License along with | ||
19 | * this program; if not, write to the Free Software Foundation, Inc., 59 | ||
20 | * Temple Place - Suite 330, Boston, MA 02111-1307, USA. | ||
21 | * | ||
22 | ******************************************************************************/ | ||
23 | |||
24 | /* | ||
25 | * Written by Johan Verrept, maintained by Duncan Sands (duncan.sands@free.fr) | ||
26 | * | ||
27 | * 1.7+: - See the check-in logs | ||
28 | * | ||
29 | * 1.6: - No longer opens a connection if the firmware is not loaded | ||
30 | * - Added support for the speedtouch 330 | ||
31 | * - Removed the limit on the number of devices | ||
32 | * - Module now autoloads on device plugin | ||
33 | * - Merged relevant parts of sarlib | ||
34 | * - Replaced the kernel thread with a tasklet | ||
35 | * - New packet transmission code | ||
36 | * - Changed proc file contents | ||
37 | * - Fixed all known SMP races | ||
38 | * - Many fixes and cleanups | ||
39 | * - Various fixes by Oliver Neukum (oliver@neukum.name) | ||
40 | * | ||
41 | * 1.5A: - Version for inclusion in 2.5 series kernel | ||
42 | * - Modifications by Richard Purdie (rpurdie@rpsys.net) | ||
43 | * - made compatible with kernel 2.5.6 onwards by changing | ||
44 | * udsl_usb_send_data_context->urb to a pointer and adding code | ||
45 | * to alloc and free it | ||
46 | * - remove_wait_queue() added to udsl_atm_processqueue_thread() | ||
47 | * | ||
48 | * 1.5: - fixed memory leak when atmsar_decode_aal5 returned NULL. | ||
49 | * (reported by stephen.robinson@zen.co.uk) | ||
50 | * | ||
51 | * 1.4: - changed the spin_lock() under interrupt to spin_lock_irqsave() | ||
52 | * - unlink all active send urbs of a vcc that is being closed. | ||
53 | * | ||
54 | * 1.3.1: - added the version number | ||
55 | * | ||
56 | * 1.3: - Added multiple send urb support | ||
57 | * - fixed memory leak and vcc->tx_inuse starvation bug | ||
58 | * when not enough memory left in vcc. | ||
59 | * | ||
60 | * 1.2: - Fixed race condition in udsl_usb_send_data() | ||
61 | * 1.1: - Turned off packet debugging | ||
62 | * | ||
63 | */ | ||
64 | |||
65 | #include <linux/module.h> | ||
66 | #include <linux/moduleparam.h> | ||
67 | #include <linux/kernel.h> | ||
68 | #include <linux/sched.h> | ||
69 | #include <linux/timer.h> | ||
70 | #include <linux/errno.h> | ||
71 | #include <linux/proc_fs.h> | ||
72 | #include <linux/slab.h> | ||
73 | #include <linux/wait.h> | ||
74 | #include <linux/list.h> | ||
75 | #include <asm/uaccess.h> | ||
76 | #include <linux/smp_lock.h> | ||
77 | #include <linux/interrupt.h> | ||
78 | #include <linux/atm.h> | ||
79 | #include <linux/atmdev.h> | ||
80 | #include <linux/crc32.h> | ||
81 | #include <linux/init.h> | ||
82 | #include <linux/firmware.h> | ||
83 | |||
84 | #include "usb_atm.h" | ||
85 | |||
86 | #ifdef VERBOSE_DEBUG | ||
87 | static int udsl_print_packet(const unsigned char *data, int len); | ||
88 | #define PACKETDEBUG(arg...) udsl_print_packet (arg) | ||
89 | #define vdbg(arg...) dbg (arg) | ||
90 | #else | ||
91 | #define PACKETDEBUG(arg...) | ||
92 | #define vdbg(arg...) | ||
93 | #endif | ||
94 | |||
95 | #define DRIVER_AUTHOR "Johan Verrept, Duncan Sands <duncan.sands@free.fr>" | ||
96 | #define DRIVER_VERSION "1.8" | ||
97 | #define DRIVER_DESC "Generic USB ATM/DSL I/O, version " DRIVER_VERSION | ||
98 | |||
99 | static unsigned int num_rcv_urbs = UDSL_DEFAULT_RCV_URBS; | ||
100 | static unsigned int num_snd_urbs = UDSL_DEFAULT_SND_URBS; | ||
101 | static unsigned int num_rcv_bufs = UDSL_DEFAULT_RCV_BUFS; | ||
102 | static unsigned int num_snd_bufs = UDSL_DEFAULT_SND_BUFS; | ||
103 | static unsigned int rcv_buf_size = UDSL_DEFAULT_RCV_BUF_SIZE; | ||
104 | static unsigned int snd_buf_size = UDSL_DEFAULT_SND_BUF_SIZE; | ||
105 | |||
106 | module_param(num_rcv_urbs, uint, 0444); | ||
107 | MODULE_PARM_DESC(num_rcv_urbs, | ||
108 | "Number of urbs used for reception (range: 0-" | ||
109 | __MODULE_STRING(UDSL_MAX_RCV_URBS) ", default: " | ||
110 | __MODULE_STRING(UDSL_DEFAULT_RCV_URBS) ")"); | ||
111 | |||
112 | module_param(num_snd_urbs, uint, 0444); | ||
113 | MODULE_PARM_DESC(num_snd_urbs, | ||
114 | "Number of urbs used for transmission (range: 0-" | ||
115 | __MODULE_STRING(UDSL_MAX_SND_URBS) ", default: " | ||
116 | __MODULE_STRING(UDSL_DEFAULT_SND_URBS) ")"); | ||
117 | |||
118 | module_param(num_rcv_bufs, uint, 0444); | ||
119 | MODULE_PARM_DESC(num_rcv_bufs, | ||
120 | "Number of buffers used for reception (range: 0-" | ||
121 | __MODULE_STRING(UDSL_MAX_RCV_BUFS) ", default: " | ||
122 | __MODULE_STRING(UDSL_DEFAULT_RCV_BUFS) ")"); | ||
123 | |||
124 | module_param(num_snd_bufs, uint, 0444); | ||
125 | MODULE_PARM_DESC(num_snd_bufs, | ||
126 | "Number of buffers used for transmission (range: 0-" | ||
127 | __MODULE_STRING(UDSL_MAX_SND_BUFS) ", default: " | ||
128 | __MODULE_STRING(UDSL_DEFAULT_SND_BUFS) ")"); | ||
129 | |||
130 | module_param(rcv_buf_size, uint, 0444); | ||
131 | MODULE_PARM_DESC(rcv_buf_size, | ||
132 | "Size of the buffers used for reception (range: 0-" | ||
133 | __MODULE_STRING(UDSL_MAX_RCV_BUF_SIZE) ", default: " | ||
134 | __MODULE_STRING(UDSL_DEFAULT_RCV_BUF_SIZE) ")"); | ||
135 | |||
136 | module_param(snd_buf_size, uint, 0444); | ||
137 | MODULE_PARM_DESC(snd_buf_size, | ||
138 | "Size of the buffers used for transmission (range: 0-" | ||
139 | __MODULE_STRING(UDSL_MAX_SND_BUF_SIZE) ", default: " | ||
140 | __MODULE_STRING(UDSL_DEFAULT_SND_BUF_SIZE) ")"); | ||
141 | |||
142 | /* ATM */ | ||
143 | |||
144 | static void udsl_atm_dev_close(struct atm_dev *dev); | ||
145 | static int udsl_atm_open(struct atm_vcc *vcc); | ||
146 | static void udsl_atm_close(struct atm_vcc *vcc); | ||
147 | static int udsl_atm_ioctl(struct atm_dev *dev, unsigned int cmd, void __user * arg); | ||
148 | static int udsl_atm_send(struct atm_vcc *vcc, struct sk_buff *skb); | ||
149 | static int udsl_atm_proc_read(struct atm_dev *atm_dev, loff_t * pos, char *page); | ||
150 | |||
151 | static struct atmdev_ops udsl_atm_devops = { | ||
152 | .dev_close = udsl_atm_dev_close, | ||
153 | .open = udsl_atm_open, | ||
154 | .close = udsl_atm_close, | ||
155 | .ioctl = udsl_atm_ioctl, | ||
156 | .send = udsl_atm_send, | ||
157 | .proc_read = udsl_atm_proc_read, | ||
158 | .owner = THIS_MODULE, | ||
159 | }; | ||
160 | |||
161 | /*********** | ||
162 | ** misc ** | ||
163 | ***********/ | ||
164 | |||
165 | static inline void udsl_pop(struct atm_vcc *vcc, struct sk_buff *skb) | ||
166 | { | ||
167 | if (vcc->pop) | ||
168 | vcc->pop(vcc, skb); | ||
169 | else | ||
170 | dev_kfree_skb(skb); | ||
171 | } | ||
172 | |||
173 | /************* | ||
174 | ** decode ** | ||
175 | *************/ | ||
176 | |||
177 | static inline struct udsl_vcc_data *udsl_find_vcc(struct udsl_instance_data *instance, | ||
178 | short vpi, int vci) | ||
179 | { | ||
180 | struct udsl_vcc_data *vcc; | ||
181 | |||
182 | list_for_each_entry(vcc, &instance->vcc_list, list) | ||
183 | if ((vcc->vci == vci) && (vcc->vpi == vpi)) | ||
184 | return vcc; | ||
185 | return NULL; | ||
186 | } | ||
187 | |||
188 | static void udsl_extract_cells(struct udsl_instance_data *instance, | ||
189 | unsigned char *source, unsigned int howmany) | ||
190 | { | ||
191 | struct udsl_vcc_data *cached_vcc = NULL; | ||
192 | struct atm_vcc *vcc; | ||
193 | struct sk_buff *sarb; | ||
194 | struct udsl_vcc_data *vcc_data; | ||
195 | int cached_vci = 0; | ||
196 | unsigned int i; | ||
197 | int pti; | ||
198 | int vci; | ||
199 | short cached_vpi = 0; | ||
200 | short vpi; | ||
201 | |||
202 | for (i = 0; i < howmany; | ||
203 | i++, source += ATM_CELL_SIZE + instance->rcv_padding) { | ||
204 | vpi = ((source[0] & 0x0f) << 4) | (source[1] >> 4); | ||
205 | vci = ((source[1] & 0x0f) << 12) | (source[2] << 4) | (source[3] >> 4); | ||
206 | pti = (source[3] & 0x2) != 0; | ||
207 | |||
208 | vdbg("udsl_extract_cells: vpi %hd, vci %d, pti %d", vpi, vci, pti); | ||
209 | |||
210 | if (cached_vcc && (vci == cached_vci) && (vpi == cached_vpi)) | ||
211 | vcc_data = cached_vcc; | ||
212 | else if ((vcc_data = udsl_find_vcc(instance, vpi, vci))) { | ||
213 | cached_vcc = vcc_data; | ||
214 | cached_vpi = vpi; | ||
215 | cached_vci = vci; | ||
216 | } else { | ||
217 | dbg("udsl_extract_cells: unknown vpi/vci (%hd/%d)!", vpi, vci); | ||
218 | continue; | ||
219 | } | ||
220 | |||
221 | vcc = vcc_data->vcc; | ||
222 | sarb = vcc_data->sarb; | ||
223 | |||
224 | if (sarb->tail + ATM_CELL_PAYLOAD > sarb->end) { | ||
225 | dbg("udsl_extract_cells: buffer overrun (sarb->len %u, vcc: 0x%p)!", sarb->len, vcc); | ||
226 | /* discard cells already received */ | ||
227 | skb_trim(sarb, 0); | ||
228 | } | ||
229 | |||
230 | memcpy(sarb->tail, source + ATM_CELL_HEADER, ATM_CELL_PAYLOAD); | ||
231 | __skb_put(sarb, ATM_CELL_PAYLOAD); | ||
232 | |||
233 | if (pti) { | ||
234 | struct sk_buff *skb; | ||
235 | unsigned int length; | ||
236 | unsigned int pdu_length; | ||
237 | |||
238 | length = (source[ATM_CELL_SIZE - 6] << 8) + source[ATM_CELL_SIZE - 5]; | ||
239 | |||
240 | /* guard against overflow */ | ||
241 | if (length > ATM_MAX_AAL5_PDU) { | ||
242 | dbg("udsl_extract_cells: bogus length %u (vcc: 0x%p)!", length, vcc); | ||
243 | atomic_inc(&vcc->stats->rx_err); | ||
244 | goto out; | ||
245 | } | ||
246 | |||
247 | pdu_length = UDSL_NUM_CELLS(length) * ATM_CELL_PAYLOAD; | ||
248 | |||
249 | if (sarb->len < pdu_length) { | ||
250 | dbg("udsl_extract_cells: bogus pdu_length %u (sarb->len: %u, vcc: 0x%p)!", pdu_length, sarb->len, vcc); | ||
251 | atomic_inc(&vcc->stats->rx_err); | ||
252 | goto out; | ||
253 | } | ||
254 | |||
255 | if (crc32_be(~0, sarb->tail - pdu_length, pdu_length) != 0xc704dd7b) { | ||
256 | dbg("udsl_extract_cells: packet failed crc check (vcc: 0x%p)!", vcc); | ||
257 | atomic_inc(&vcc->stats->rx_err); | ||
258 | goto out; | ||
259 | } | ||
260 | |||
261 | vdbg("udsl_extract_cells: got packet (length: %u, pdu_length: %u, vcc: 0x%p)", length, pdu_length, vcc); | ||
262 | |||
263 | if (!(skb = dev_alloc_skb(length))) { | ||
264 | dbg("udsl_extract_cells: no memory for skb (length: %u)!", length); | ||
265 | atomic_inc(&vcc->stats->rx_drop); | ||
266 | goto out; | ||
267 | } | ||
268 | |||
269 | vdbg("udsl_extract_cells: allocated new sk_buff (skb: 0x%p, skb->truesize: %u)", skb, skb->truesize); | ||
270 | |||
271 | if (!atm_charge(vcc, skb->truesize)) { | ||
272 | dbg("udsl_extract_cells: failed atm_charge (skb->truesize: %u)!", skb->truesize); | ||
273 | dev_kfree_skb(skb); | ||
274 | goto out; /* atm_charge increments rx_drop */ | ||
275 | } | ||
276 | |||
277 | memcpy(skb->data, sarb->tail - pdu_length, length); | ||
278 | __skb_put(skb, length); | ||
279 | |||
280 | vdbg("udsl_extract_cells: sending skb 0x%p, skb->len %u, skb->truesize %u", skb, skb->len, skb->truesize); | ||
281 | |||
282 | PACKETDEBUG(skb->data, skb->len); | ||
283 | |||
284 | vcc->push(vcc, skb); | ||
285 | |||
286 | atomic_inc(&vcc->stats->rx); | ||
287 | out: | ||
288 | skb_trim(sarb, 0); | ||
289 | } | ||
290 | } | ||
291 | } | ||
292 | |||
293 | /************* | ||
294 | ** encode ** | ||
295 | *************/ | ||
296 | |||
297 | static inline void udsl_fill_cell_header(unsigned char *target, struct atm_vcc *vcc) | ||
298 | { | ||
299 | target[0] = vcc->vpi >> 4; | ||
300 | target[1] = (vcc->vpi << 4) | (vcc->vci >> 12); | ||
301 | target[2] = vcc->vci >> 4; | ||
302 | target[3] = vcc->vci << 4; | ||
303 | target[4] = 0xec; | ||
304 | } | ||
305 | |||
306 | static const unsigned char zeros[ATM_CELL_PAYLOAD]; | ||
307 | |||
308 | static void udsl_groom_skb(struct atm_vcc *vcc, struct sk_buff *skb) | ||
309 | { | ||
310 | struct udsl_control *ctrl = UDSL_SKB(skb); | ||
311 | unsigned int zero_padding; | ||
312 | u32 crc; | ||
313 | |||
314 | ctrl->atm_data.vcc = vcc; | ||
315 | |||
316 | ctrl->num_cells = UDSL_NUM_CELLS(skb->len); | ||
317 | ctrl->num_entire = skb->len / ATM_CELL_PAYLOAD; | ||
318 | |||
319 | zero_padding = ctrl->num_cells * ATM_CELL_PAYLOAD - skb->len - ATM_AAL5_TRAILER; | ||
320 | |||
321 | if (ctrl->num_entire + 1 < ctrl->num_cells) | ||
322 | ctrl->pdu_padding = zero_padding - (ATM_CELL_PAYLOAD - ATM_AAL5_TRAILER); | ||
323 | else | ||
324 | ctrl->pdu_padding = zero_padding; | ||
325 | |||
326 | ctrl->aal5_trailer[0] = 0; /* UU = 0 */ | ||
327 | ctrl->aal5_trailer[1] = 0; /* CPI = 0 */ | ||
328 | ctrl->aal5_trailer[2] = skb->len >> 8; | ||
329 | ctrl->aal5_trailer[3] = skb->len; | ||
330 | |||
331 | crc = crc32_be(~0, skb->data, skb->len); | ||
332 | crc = crc32_be(crc, zeros, zero_padding); | ||
333 | crc = crc32_be(crc, ctrl->aal5_trailer, 4); | ||
334 | crc = ~crc; | ||
335 | |||
336 | ctrl->aal5_trailer[4] = crc >> 24; | ||
337 | ctrl->aal5_trailer[5] = crc >> 16; | ||
338 | ctrl->aal5_trailer[6] = crc >> 8; | ||
339 | ctrl->aal5_trailer[7] = crc; | ||
340 | } | ||
341 | |||
342 | static unsigned int udsl_write_cells(struct udsl_instance_data *instance, | ||
343 | unsigned int howmany, struct sk_buff *skb, | ||
344 | unsigned char **target_p) | ||
345 | { | ||
346 | struct udsl_control *ctrl = UDSL_SKB(skb); | ||
347 | unsigned char *target = *target_p; | ||
348 | unsigned int nc, ne, i; | ||
349 | |||
350 | vdbg("udsl_write_cells: howmany=%u, skb->len=%d, num_cells=%u, num_entire=%u, pdu_padding=%u", howmany, skb->len, ctrl->num_cells, ctrl->num_entire, ctrl->pdu_padding); | ||
351 | |||
352 | nc = ctrl->num_cells; | ||
353 | ne = min(howmany, ctrl->num_entire); | ||
354 | |||
355 | for (i = 0; i < ne; i++) { | ||
356 | udsl_fill_cell_header(target, ctrl->atm_data.vcc); | ||
357 | target += ATM_CELL_HEADER; | ||
358 | memcpy(target, skb->data, ATM_CELL_PAYLOAD); | ||
359 | target += ATM_CELL_PAYLOAD; | ||
360 | if (instance->snd_padding) { | ||
361 | memset(target, 0, instance->snd_padding); | ||
362 | target += instance->snd_padding; | ||
363 | } | ||
364 | __skb_pull(skb, ATM_CELL_PAYLOAD); | ||
365 | } | ||
366 | |||
367 | ctrl->num_entire -= ne; | ||
368 | |||
369 | if (!(ctrl->num_cells -= ne) || !(howmany -= ne)) | ||
370 | goto out; | ||
371 | |||
372 | udsl_fill_cell_header(target, ctrl->atm_data.vcc); | ||
373 | target += ATM_CELL_HEADER; | ||
374 | memcpy(target, skb->data, skb->len); | ||
375 | target += skb->len; | ||
376 | __skb_pull(skb, skb->len); | ||
377 | memset(target, 0, ctrl->pdu_padding); | ||
378 | target += ctrl->pdu_padding; | ||
379 | |||
380 | if (--ctrl->num_cells) { | ||
381 | if (!--howmany) { | ||
382 | ctrl->pdu_padding = ATM_CELL_PAYLOAD - ATM_AAL5_TRAILER; | ||
383 | goto out; | ||
384 | } | ||
385 | |||
386 | if (instance->snd_padding) { | ||
387 | memset(target, 0, instance->snd_padding); | ||
388 | target += instance->snd_padding; | ||
389 | } | ||
390 | udsl_fill_cell_header(target, ctrl->atm_data.vcc); | ||
391 | target += ATM_CELL_HEADER; | ||
392 | memset(target, 0, ATM_CELL_PAYLOAD - ATM_AAL5_TRAILER); | ||
393 | target += ATM_CELL_PAYLOAD - ATM_AAL5_TRAILER; | ||
394 | |||
395 | --ctrl->num_cells; | ||
396 | UDSL_ASSERT(!ctrl->num_cells); | ||
397 | } | ||
398 | |||
399 | memcpy(target, ctrl->aal5_trailer, ATM_AAL5_TRAILER); | ||
400 | target += ATM_AAL5_TRAILER; | ||
401 | /* set pti bit in last cell */ | ||
402 | *(target + 3 - ATM_CELL_SIZE) |= 0x2; | ||
403 | if (instance->snd_padding) { | ||
404 | memset(target, 0, instance->snd_padding); | ||
405 | target += instance->snd_padding; | ||
406 | } | ||
407 | out: | ||
408 | *target_p = target; | ||
409 | return nc - ctrl->num_cells; | ||
410 | } | ||
411 | |||
412 | /************** | ||
413 | ** receive ** | ||
414 | **************/ | ||
415 | |||
416 | static void udsl_complete_receive(struct urb *urb, struct pt_regs *regs) | ||
417 | { | ||
418 | struct udsl_receive_buffer *buf; | ||
419 | struct udsl_instance_data *instance; | ||
420 | struct udsl_receiver *rcv; | ||
421 | unsigned long flags; | ||
422 | |||
423 | if (!urb || !(rcv = urb->context)) { | ||
424 | dbg("udsl_complete_receive: bad urb!"); | ||
425 | return; | ||
426 | } | ||
427 | |||
428 | instance = rcv->instance; | ||
429 | buf = rcv->buffer; | ||
430 | |||
431 | buf->filled_cells = urb->actual_length / (ATM_CELL_SIZE + instance->rcv_padding); | ||
432 | |||
433 | vdbg("udsl_complete_receive: urb 0x%p, status %d, actual_length %d, filled_cells %u, rcv 0x%p, buf 0x%p", urb, urb->status, urb->actual_length, buf->filled_cells, rcv, buf); | ||
434 | |||
435 | UDSL_ASSERT(buf->filled_cells <= rcv_buf_size); | ||
436 | |||
437 | /* may not be in_interrupt() */ | ||
438 | spin_lock_irqsave(&instance->receive_lock, flags); | ||
439 | list_add(&rcv->list, &instance->spare_receivers); | ||
440 | list_add_tail(&buf->list, &instance->filled_receive_buffers); | ||
441 | if (likely(!urb->status)) | ||
442 | tasklet_schedule(&instance->receive_tasklet); | ||
443 | spin_unlock_irqrestore(&instance->receive_lock, flags); | ||
444 | } | ||
445 | |||
446 | static void udsl_process_receive(unsigned long data) | ||
447 | { | ||
448 | struct udsl_receive_buffer *buf; | ||
449 | struct udsl_instance_data *instance = (struct udsl_instance_data *)data; | ||
450 | struct udsl_receiver *rcv; | ||
451 | int err; | ||
452 | |||
453 | made_progress: | ||
454 | while (!list_empty(&instance->spare_receive_buffers)) { | ||
455 | spin_lock_irq(&instance->receive_lock); | ||
456 | if (list_empty(&instance->spare_receivers)) { | ||
457 | spin_unlock_irq(&instance->receive_lock); | ||
458 | break; | ||
459 | } | ||
460 | rcv = list_entry(instance->spare_receivers.next, | ||
461 | struct udsl_receiver, list); | ||
462 | list_del(&rcv->list); | ||
463 | spin_unlock_irq(&instance->receive_lock); | ||
464 | |||
465 | buf = list_entry(instance->spare_receive_buffers.next, | ||
466 | struct udsl_receive_buffer, list); | ||
467 | list_del(&buf->list); | ||
468 | |||
469 | rcv->buffer = buf; | ||
470 | |||
471 | usb_fill_bulk_urb(rcv->urb, instance->usb_dev, | ||
472 | usb_rcvbulkpipe(instance->usb_dev, instance->data_endpoint), | ||
473 | buf->base, | ||
474 | rcv_buf_size * (ATM_CELL_SIZE + instance->rcv_padding), | ||
475 | udsl_complete_receive, rcv); | ||
476 | |||
477 | vdbg("udsl_process_receive: sending urb 0x%p, rcv 0x%p, buf 0x%p", | ||
478 | rcv->urb, rcv, buf); | ||
479 | |||
480 | if ((err = usb_submit_urb(rcv->urb, GFP_ATOMIC)) < 0) { | ||
481 | dbg("udsl_process_receive: urb submission failed (%d)!", err); | ||
482 | list_add(&buf->list, &instance->spare_receive_buffers); | ||
483 | spin_lock_irq(&instance->receive_lock); | ||
484 | list_add(&rcv->list, &instance->spare_receivers); | ||
485 | spin_unlock_irq(&instance->receive_lock); | ||
486 | break; | ||
487 | } | ||
488 | } | ||
489 | |||
490 | spin_lock_irq(&instance->receive_lock); | ||
491 | if (list_empty(&instance->filled_receive_buffers)) { | ||
492 | spin_unlock_irq(&instance->receive_lock); | ||
493 | return; /* done - no more buffers */ | ||
494 | } | ||
495 | buf = list_entry(instance->filled_receive_buffers.next, | ||
496 | struct udsl_receive_buffer, list); | ||
497 | list_del(&buf->list); | ||
498 | spin_unlock_irq(&instance->receive_lock); | ||
499 | |||
500 | vdbg("udsl_process_receive: processing buf 0x%p", buf); | ||
501 | udsl_extract_cells(instance, buf->base, buf->filled_cells); | ||
502 | list_add(&buf->list, &instance->spare_receive_buffers); | ||
503 | goto made_progress; | ||
504 | } | ||
505 | |||
506 | /*********** | ||
507 | ** send ** | ||
508 | ***********/ | ||
509 | |||
510 | static void udsl_complete_send(struct urb *urb, struct pt_regs *regs) | ||
511 | { | ||
512 | struct udsl_instance_data *instance; | ||
513 | struct udsl_sender *snd; | ||
514 | unsigned long flags; | ||
515 | |||
516 | if (!urb || !(snd = urb->context) || !(instance = snd->instance)) { | ||
517 | dbg("udsl_complete_send: bad urb!"); | ||
518 | return; | ||
519 | } | ||
520 | |||
521 | vdbg("udsl_complete_send: urb 0x%p, status %d, snd 0x%p, buf 0x%p", urb, | ||
522 | urb->status, snd, snd->buffer); | ||
523 | |||
524 | /* may not be in_interrupt() */ | ||
525 | spin_lock_irqsave(&instance->send_lock, flags); | ||
526 | list_add(&snd->list, &instance->spare_senders); | ||
527 | list_add(&snd->buffer->list, &instance->spare_send_buffers); | ||
528 | tasklet_schedule(&instance->send_tasklet); | ||
529 | spin_unlock_irqrestore(&instance->send_lock, flags); | ||
530 | } | ||
531 | |||
532 | static void udsl_process_send(unsigned long data) | ||
533 | { | ||
534 | struct udsl_send_buffer *buf; | ||
535 | struct udsl_instance_data *instance = (struct udsl_instance_data *)data; | ||
536 | struct sk_buff *skb; | ||
537 | struct udsl_sender *snd; | ||
538 | int err; | ||
539 | unsigned int num_written; | ||
540 | |||
541 | made_progress: | ||
542 | spin_lock_irq(&instance->send_lock); | ||
543 | while (!list_empty(&instance->spare_senders)) { | ||
544 | if (!list_empty(&instance->filled_send_buffers)) { | ||
545 | buf = list_entry(instance->filled_send_buffers.next, | ||
546 | struct udsl_send_buffer, list); | ||
547 | list_del(&buf->list); | ||
548 | } else if ((buf = instance->current_buffer)) { | ||
549 | instance->current_buffer = NULL; | ||
550 | } else /* all buffers empty */ | ||
551 | break; | ||
552 | |||
553 | snd = list_entry(instance->spare_senders.next, | ||
554 | struct udsl_sender, list); | ||
555 | list_del(&snd->list); | ||
556 | spin_unlock_irq(&instance->send_lock); | ||
557 | |||
558 | snd->buffer = buf; | ||
559 | usb_fill_bulk_urb(snd->urb, instance->usb_dev, | ||
560 | usb_sndbulkpipe(instance->usb_dev, instance->data_endpoint), | ||
561 | buf->base, | ||
562 | (snd_buf_size - buf->free_cells) * (ATM_CELL_SIZE + instance->snd_padding), | ||
563 | udsl_complete_send, snd); | ||
564 | |||
565 | vdbg("udsl_process_send: submitting urb 0x%p (%d cells), snd 0x%p, buf 0x%p", | ||
566 | snd->urb, snd_buf_size - buf->free_cells, snd, buf); | ||
567 | |||
568 | if ((err = usb_submit_urb(snd->urb, GFP_ATOMIC)) < 0) { | ||
569 | dbg("udsl_process_send: urb submission failed (%d)!", err); | ||
570 | spin_lock_irq(&instance->send_lock); | ||
571 | list_add(&snd->list, &instance->spare_senders); | ||
572 | spin_unlock_irq(&instance->send_lock); | ||
573 | list_add(&buf->list, &instance->filled_send_buffers); | ||
574 | return; /* bail out */ | ||
575 | } | ||
576 | |||
577 | spin_lock_irq(&instance->send_lock); | ||
578 | } /* while */ | ||
579 | spin_unlock_irq(&instance->send_lock); | ||
580 | |||
581 | if (!instance->current_skb) | ||
582 | instance->current_skb = skb_dequeue(&instance->sndqueue); | ||
583 | if (!instance->current_skb) | ||
584 | return; /* done - no more skbs */ | ||
585 | |||
586 | skb = instance->current_skb; | ||
587 | |||
588 | if (!(buf = instance->current_buffer)) { | ||
589 | spin_lock_irq(&instance->send_lock); | ||
590 | if (list_empty(&instance->spare_send_buffers)) { | ||
591 | instance->current_buffer = NULL; | ||
592 | spin_unlock_irq(&instance->send_lock); | ||
593 | return; /* done - no more buffers */ | ||
594 | } | ||
595 | buf = list_entry(instance->spare_send_buffers.next, | ||
596 | struct udsl_send_buffer, list); | ||
597 | list_del(&buf->list); | ||
598 | spin_unlock_irq(&instance->send_lock); | ||
599 | |||
600 | buf->free_start = buf->base; | ||
601 | buf->free_cells = snd_buf_size; | ||
602 | |||
603 | instance->current_buffer = buf; | ||
604 | } | ||
605 | |||
606 | num_written = udsl_write_cells(instance, buf->free_cells, skb, &buf->free_start); | ||
607 | |||
608 | vdbg("udsl_process_send: wrote %u cells from skb 0x%p to buffer 0x%p", | ||
609 | num_written, skb, buf); | ||
610 | |||
611 | if (!(buf->free_cells -= num_written)) { | ||
612 | list_add_tail(&buf->list, &instance->filled_send_buffers); | ||
613 | instance->current_buffer = NULL; | ||
614 | } | ||
615 | |||
616 | vdbg("udsl_process_send: buffer contains %d cells, %d left", | ||
617 | snd_buf_size - buf->free_cells, buf->free_cells); | ||
618 | |||
619 | if (!UDSL_SKB(skb)->num_cells) { | ||
620 | struct atm_vcc *vcc = UDSL_SKB(skb)->atm_data.vcc; | ||
621 | |||
622 | udsl_pop(vcc, skb); | ||
623 | instance->current_skb = NULL; | ||
624 | |||
625 | atomic_inc(&vcc->stats->tx); | ||
626 | } | ||
627 | |||
628 | goto made_progress; | ||
629 | } | ||
630 | |||
631 | static void udsl_cancel_send(struct udsl_instance_data *instance, | ||
632 | struct atm_vcc *vcc) | ||
633 | { | ||
634 | struct sk_buff *skb, *n; | ||
635 | |||
636 | dbg("udsl_cancel_send entered"); | ||
637 | spin_lock_irq(&instance->sndqueue.lock); | ||
638 | for (skb = instance->sndqueue.next, n = skb->next; | ||
639 | skb != (struct sk_buff *)&instance->sndqueue; | ||
640 | skb = n, n = skb->next) | ||
641 | if (UDSL_SKB(skb)->atm_data.vcc == vcc) { | ||
642 | dbg("udsl_cancel_send: popping skb 0x%p", skb); | ||
643 | __skb_unlink(skb, &instance->sndqueue); | ||
644 | udsl_pop(vcc, skb); | ||
645 | } | ||
646 | spin_unlock_irq(&instance->sndqueue.lock); | ||
647 | |||
648 | tasklet_disable(&instance->send_tasklet); | ||
649 | if ((skb = instance->current_skb) && (UDSL_SKB(skb)->atm_data.vcc == vcc)) { | ||
650 | dbg("udsl_cancel_send: popping current skb (0x%p)", skb); | ||
651 | instance->current_skb = NULL; | ||
652 | udsl_pop(vcc, skb); | ||
653 | } | ||
654 | tasklet_enable(&instance->send_tasklet); | ||
655 | dbg("udsl_cancel_send done"); | ||
656 | } | ||
657 | |||
658 | static int udsl_atm_send(struct atm_vcc *vcc, struct sk_buff *skb) | ||
659 | { | ||
660 | struct udsl_instance_data *instance = vcc->dev->dev_data; | ||
661 | int err; | ||
662 | |||
663 | vdbg("udsl_atm_send called (skb 0x%p, len %u)", skb, skb->len); | ||
664 | |||
665 | if (!instance) { | ||
666 | dbg("udsl_atm_send: NULL data!"); | ||
667 | err = -ENODEV; | ||
668 | goto fail; | ||
669 | } | ||
670 | |||
671 | if (vcc->qos.aal != ATM_AAL5) { | ||
672 | dbg("udsl_atm_send: unsupported ATM type %d!", vcc->qos.aal); | ||
673 | err = -EINVAL; | ||
674 | goto fail; | ||
675 | } | ||
676 | |||
677 | if (skb->len > ATM_MAX_AAL5_PDU) { | ||
678 | dbg("udsl_atm_send: packet too long (%d vs %d)!", skb->len, | ||
679 | ATM_MAX_AAL5_PDU); | ||
680 | err = -EINVAL; | ||
681 | goto fail; | ||
682 | } | ||
683 | |||
684 | PACKETDEBUG(skb->data, skb->len); | ||
685 | |||
686 | udsl_groom_skb(vcc, skb); | ||
687 | skb_queue_tail(&instance->sndqueue, skb); | ||
688 | tasklet_schedule(&instance->send_tasklet); | ||
689 | |||
690 | return 0; | ||
691 | |||
692 | fail: | ||
693 | udsl_pop(vcc, skb); | ||
694 | return err; | ||
695 | } | ||
696 | |||
697 | /******************** | ||
698 | ** bean counting ** | ||
699 | ********************/ | ||
700 | |||
701 | static void udsl_destroy_instance(struct kref *kref) | ||
702 | { | ||
703 | struct udsl_instance_data *instance = | ||
704 | container_of(kref, struct udsl_instance_data, refcount); | ||
705 | |||
706 | tasklet_kill(&instance->receive_tasklet); | ||
707 | tasklet_kill(&instance->send_tasklet); | ||
708 | usb_put_dev(instance->usb_dev); | ||
709 | kfree(instance); | ||
710 | } | ||
711 | |||
712 | void udsl_get_instance(struct udsl_instance_data *instance) | ||
713 | { | ||
714 | kref_get(&instance->refcount); | ||
715 | } | ||
716 | |||
717 | void udsl_put_instance(struct udsl_instance_data *instance) | ||
718 | { | ||
719 | kref_put(&instance->refcount, udsl_destroy_instance); | ||
720 | } | ||
721 | |||
722 | /********** | ||
723 | ** ATM ** | ||
724 | **********/ | ||
725 | |||
726 | static void udsl_atm_dev_close(struct atm_dev *dev) | ||
727 | { | ||
728 | struct udsl_instance_data *instance = dev->dev_data; | ||
729 | |||
730 | dev->dev_data = NULL; | ||
731 | udsl_put_instance(instance); | ||
732 | } | ||
733 | |||
734 | static int udsl_atm_proc_read(struct atm_dev *atm_dev, loff_t * pos, char *page) | ||
735 | { | ||
736 | struct udsl_instance_data *instance = atm_dev->dev_data; | ||
737 | int left = *pos; | ||
738 | |||
739 | if (!instance) { | ||
740 | dbg("udsl_atm_proc_read: NULL instance!"); | ||
741 | return -ENODEV; | ||
742 | } | ||
743 | |||
744 | if (!left--) | ||
745 | return sprintf(page, "%s\n", instance->description); | ||
746 | |||
747 | if (!left--) | ||
748 | return sprintf(page, "MAC: %02x:%02x:%02x:%02x:%02x:%02x\n", | ||
749 | atm_dev->esi[0], atm_dev->esi[1], | ||
750 | atm_dev->esi[2], atm_dev->esi[3], | ||
751 | atm_dev->esi[4], atm_dev->esi[5]); | ||
752 | |||
753 | if (!left--) | ||
754 | return sprintf(page, | ||
755 | "AAL5: tx %d ( %d err ), rx %d ( %d err, %d drop )\n", | ||
756 | atomic_read(&atm_dev->stats.aal5.tx), | ||
757 | atomic_read(&atm_dev->stats.aal5.tx_err), | ||
758 | atomic_read(&atm_dev->stats.aal5.rx), | ||
759 | atomic_read(&atm_dev->stats.aal5.rx_err), | ||
760 | atomic_read(&atm_dev->stats.aal5.rx_drop)); | ||
761 | |||
762 | if (!left--) { | ||
763 | switch (atm_dev->signal) { | ||
764 | case ATM_PHY_SIG_FOUND: | ||
765 | sprintf(page, "Line up"); | ||
766 | break; | ||
767 | case ATM_PHY_SIG_LOST: | ||
768 | sprintf(page, "Line down"); | ||
769 | break; | ||
770 | default: | ||
771 | sprintf(page, "Line state unknown"); | ||
772 | break; | ||
773 | } | ||
774 | |||
775 | if (instance->usb_dev->state == USB_STATE_NOTATTACHED) | ||
776 | strcat(page, ", disconnected\n"); | ||
777 | else { | ||
778 | if (instance->status == UDSL_LOADED_FIRMWARE) | ||
779 | strcat(page, ", firmware loaded\n"); | ||
780 | else if (instance->status == UDSL_LOADING_FIRMWARE) | ||
781 | strcat(page, ", firmware loading\n"); | ||
782 | else | ||
783 | strcat(page, ", no firmware\n"); | ||
784 | } | ||
785 | |||
786 | return strlen(page); | ||
787 | } | ||
788 | |||
789 | return 0; | ||
790 | } | ||
791 | |||
792 | static int udsl_atm_open(struct atm_vcc *vcc) | ||
793 | { | ||
794 | struct udsl_instance_data *instance = vcc->dev->dev_data; | ||
795 | struct udsl_vcc_data *new; | ||
796 | unsigned int max_pdu; | ||
797 | int vci = vcc->vci; | ||
798 | short vpi = vcc->vpi; | ||
799 | int err; | ||
800 | |||
801 | dbg("udsl_atm_open: vpi %hd, vci %d", vpi, vci); | ||
802 | |||
803 | if (!instance) { | ||
804 | dbg("udsl_atm_open: NULL data!"); | ||
805 | return -ENODEV; | ||
806 | } | ||
807 | |||
808 | /* only support AAL5 */ | ||
809 | if ((vcc->qos.aal != ATM_AAL5) || (vcc->qos.rxtp.max_sdu < 0) | ||
810 | || (vcc->qos.rxtp.max_sdu > ATM_MAX_AAL5_PDU)) { | ||
811 | dbg("udsl_atm_open: unsupported ATM type %d!", vcc->qos.aal); | ||
812 | return -EINVAL; | ||
813 | } | ||
814 | |||
815 | if (instance->firmware_wait && | ||
816 | (err = instance->firmware_wait(instance)) < 0) { | ||
817 | dbg("udsl_atm_open: firmware not loaded (%d)!", err); | ||
818 | return err; | ||
819 | } | ||
820 | |||
821 | down(&instance->serialize); /* vs self, udsl_atm_close */ | ||
822 | |||
823 | if (udsl_find_vcc(instance, vpi, vci)) { | ||
824 | dbg("udsl_atm_open: %hd/%d already in use!", vpi, vci); | ||
825 | up(&instance->serialize); | ||
826 | return -EADDRINUSE; | ||
827 | } | ||
828 | |||
829 | if (!(new = kmalloc(sizeof(struct udsl_vcc_data), GFP_KERNEL))) { | ||
830 | dbg("udsl_atm_open: no memory for vcc_data!"); | ||
831 | up(&instance->serialize); | ||
832 | return -ENOMEM; | ||
833 | } | ||
834 | |||
835 | memset(new, 0, sizeof(struct udsl_vcc_data)); | ||
836 | new->vcc = vcc; | ||
837 | new->vpi = vpi; | ||
838 | new->vci = vci; | ||
839 | |||
840 | /* udsl_extract_cells requires at least one cell */ | ||
841 | max_pdu = max(1, UDSL_NUM_CELLS(vcc->qos.rxtp.max_sdu)) * ATM_CELL_PAYLOAD; | ||
842 | if (!(new->sarb = alloc_skb(max_pdu, GFP_KERNEL))) { | ||
843 | dbg("udsl_atm_open: no memory for SAR buffer!"); | ||
844 | kfree(new); | ||
845 | up(&instance->serialize); | ||
846 | return -ENOMEM; | ||
847 | } | ||
848 | |||
849 | vcc->dev_data = new; | ||
850 | |||
851 | tasklet_disable(&instance->receive_tasklet); | ||
852 | list_add(&new->list, &instance->vcc_list); | ||
853 | tasklet_enable(&instance->receive_tasklet); | ||
854 | |||
855 | set_bit(ATM_VF_ADDR, &vcc->flags); | ||
856 | set_bit(ATM_VF_PARTIAL, &vcc->flags); | ||
857 | set_bit(ATM_VF_READY, &vcc->flags); | ||
858 | |||
859 | up(&instance->serialize); | ||
860 | |||
861 | tasklet_schedule(&instance->receive_tasklet); | ||
862 | |||
863 | dbg("udsl_atm_open: allocated vcc data 0x%p (max_pdu: %u)", new, max_pdu); | ||
864 | |||
865 | return 0; | ||
866 | } | ||
867 | |||
868 | static void udsl_atm_close(struct atm_vcc *vcc) | ||
869 | { | ||
870 | struct udsl_instance_data *instance = vcc->dev->dev_data; | ||
871 | struct udsl_vcc_data *vcc_data = vcc->dev_data; | ||
872 | |||
873 | dbg("udsl_atm_close called"); | ||
874 | |||
875 | if (!instance || !vcc_data) { | ||
876 | dbg("udsl_atm_close: NULL data!"); | ||
877 | return; | ||
878 | } | ||
879 | |||
880 | dbg("udsl_atm_close: deallocating vcc 0x%p with vpi %d vci %d", | ||
881 | vcc_data, vcc_data->vpi, vcc_data->vci); | ||
882 | |||
883 | udsl_cancel_send(instance, vcc); | ||
884 | |||
885 | down(&instance->serialize); /* vs self, udsl_atm_open */ | ||
886 | |||
887 | tasklet_disable(&instance->receive_tasklet); | ||
888 | list_del(&vcc_data->list); | ||
889 | tasklet_enable(&instance->receive_tasklet); | ||
890 | |||
891 | kfree_skb(vcc_data->sarb); | ||
892 | vcc_data->sarb = NULL; | ||
893 | |||
894 | kfree(vcc_data); | ||
895 | vcc->dev_data = NULL; | ||
896 | |||
897 | vcc->vpi = ATM_VPI_UNSPEC; | ||
898 | vcc->vci = ATM_VCI_UNSPEC; | ||
899 | clear_bit(ATM_VF_READY, &vcc->flags); | ||
900 | clear_bit(ATM_VF_PARTIAL, &vcc->flags); | ||
901 | clear_bit(ATM_VF_ADDR, &vcc->flags); | ||
902 | |||
903 | up(&instance->serialize); | ||
904 | |||
905 | dbg("udsl_atm_close successful"); | ||
906 | } | ||
907 | |||
908 | static int udsl_atm_ioctl(struct atm_dev *dev, unsigned int cmd, | ||
909 | void __user * arg) | ||
910 | { | ||
911 | switch (cmd) { | ||
912 | case ATM_QUERYLOOP: | ||
913 | return put_user(ATM_LM_NONE, (int __user *)arg) ? -EFAULT : 0; | ||
914 | default: | ||
915 | return -ENOIOCTLCMD; | ||
916 | } | ||
917 | } | ||
918 | |||
919 | /********** | ||
920 | ** USB ** | ||
921 | **********/ | ||
922 | |||
923 | int udsl_instance_setup(struct usb_device *dev, | ||
924 | struct udsl_instance_data *instance) | ||
925 | { | ||
926 | char *buf; | ||
927 | int i, length; | ||
928 | |||
929 | kref_init(&instance->refcount); /* one for USB */ | ||
930 | udsl_get_instance(instance); /* one for ATM */ | ||
931 | |||
932 | init_MUTEX(&instance->serialize); | ||
933 | |||
934 | instance->usb_dev = dev; | ||
935 | |||
936 | INIT_LIST_HEAD(&instance->vcc_list); | ||
937 | |||
938 | instance->status = UDSL_NO_FIRMWARE; | ||
939 | init_waitqueue_head(&instance->firmware_waiters); | ||
940 | |||
941 | spin_lock_init(&instance->receive_lock); | ||
942 | INIT_LIST_HEAD(&instance->spare_receivers); | ||
943 | INIT_LIST_HEAD(&instance->filled_receive_buffers); | ||
944 | |||
945 | tasklet_init(&instance->receive_tasklet, udsl_process_receive, (unsigned long)instance); | ||
946 | INIT_LIST_HEAD(&instance->spare_receive_buffers); | ||
947 | |||
948 | skb_queue_head_init(&instance->sndqueue); | ||
949 | |||
950 | spin_lock_init(&instance->send_lock); | ||
951 | INIT_LIST_HEAD(&instance->spare_senders); | ||
952 | INIT_LIST_HEAD(&instance->spare_send_buffers); | ||
953 | |||
954 | tasklet_init(&instance->send_tasklet, udsl_process_send, | ||
955 | (unsigned long)instance); | ||
956 | INIT_LIST_HEAD(&instance->filled_send_buffers); | ||
957 | |||
958 | /* receive init */ | ||
959 | for (i = 0; i < num_rcv_urbs; i++) { | ||
960 | struct udsl_receiver *rcv = &(instance->receivers[i]); | ||
961 | |||
962 | if (!(rcv->urb = usb_alloc_urb(0, GFP_KERNEL))) { | ||
963 | dbg("udsl_usb_probe: no memory for receive urb %d!", i); | ||
964 | goto fail; | ||
965 | } | ||
966 | |||
967 | rcv->instance = instance; | ||
968 | |||
969 | list_add(&rcv->list, &instance->spare_receivers); | ||
970 | } | ||
971 | |||
972 | for (i = 0; i < num_rcv_bufs; i++) { | ||
973 | struct udsl_receive_buffer *buf = | ||
974 | &(instance->receive_buffers[i]); | ||
975 | |||
976 | buf->base = kmalloc(rcv_buf_size * (ATM_CELL_SIZE + instance->rcv_padding), | ||
977 | GFP_KERNEL); | ||
978 | if (!buf->base) { | ||
979 | dbg("udsl_usb_probe: no memory for receive buffer %d!", i); | ||
980 | goto fail; | ||
981 | } | ||
982 | |||
983 | list_add(&buf->list, &instance->spare_receive_buffers); | ||
984 | } | ||
985 | |||
986 | /* send init */ | ||
987 | for (i = 0; i < num_snd_urbs; i++) { | ||
988 | struct udsl_sender *snd = &(instance->senders[i]); | ||
989 | |||
990 | if (!(snd->urb = usb_alloc_urb(0, GFP_KERNEL))) { | ||
991 | dbg("udsl_usb_probe: no memory for send urb %d!", i); | ||
992 | goto fail; | ||
993 | } | ||
994 | |||
995 | snd->instance = instance; | ||
996 | |||
997 | list_add(&snd->list, &instance->spare_senders); | ||
998 | } | ||
999 | |||
1000 | for (i = 0; i < num_snd_bufs; i++) { | ||
1001 | struct udsl_send_buffer *buf = &(instance->send_buffers[i]); | ||
1002 | |||
1003 | buf->base = kmalloc(snd_buf_size * (ATM_CELL_SIZE + instance->snd_padding), | ||
1004 | GFP_KERNEL); | ||
1005 | if (!buf->base) { | ||
1006 | dbg("udsl_usb_probe: no memory for send buffer %d!", i); | ||
1007 | goto fail; | ||
1008 | } | ||
1009 | |||
1010 | list_add(&buf->list, &instance->spare_send_buffers); | ||
1011 | } | ||
1012 | |||
1013 | /* ATM init */ | ||
1014 | instance->atm_dev = atm_dev_register(instance->driver_name, | ||
1015 | &udsl_atm_devops, -1, NULL); | ||
1016 | if (!instance->atm_dev) { | ||
1017 | dbg("udsl_usb_probe: failed to register ATM device!"); | ||
1018 | goto fail; | ||
1019 | } | ||
1020 | |||
1021 | instance->atm_dev->ci_range.vpi_bits = ATM_CI_MAX; | ||
1022 | instance->atm_dev->ci_range.vci_bits = ATM_CI_MAX; | ||
1023 | instance->atm_dev->signal = ATM_PHY_SIG_UNKNOWN; | ||
1024 | |||
1025 | /* temp init ATM device, set to 128kbit */ | ||
1026 | instance->atm_dev->link_rate = 128 * 1000 / 424; | ||
1027 | |||
1028 | /* device description */ | ||
1029 | buf = instance->description; | ||
1030 | length = sizeof(instance->description); | ||
1031 | |||
1032 | if ((i = usb_string(dev, dev->descriptor.iProduct, buf, length)) < 0) | ||
1033 | goto finish; | ||
1034 | |||
1035 | buf += i; | ||
1036 | length -= i; | ||
1037 | |||
1038 | i = scnprintf(buf, length, " ("); | ||
1039 | buf += i; | ||
1040 | length -= i; | ||
1041 | |||
1042 | if (length <= 0 || (i = usb_make_path(dev, buf, length)) < 0) | ||
1043 | goto finish; | ||
1044 | |||
1045 | buf += i; | ||
1046 | length -= i; | ||
1047 | |||
1048 | snprintf(buf, length, ")"); | ||
1049 | |||
1050 | finish: | ||
1051 | /* ready for ATM callbacks */ | ||
1052 | wmb(); | ||
1053 | instance->atm_dev->dev_data = instance; | ||
1054 | |||
1055 | usb_get_dev(dev); | ||
1056 | |||
1057 | return 0; | ||
1058 | |||
1059 | fail: | ||
1060 | for (i = 0; i < num_snd_bufs; i++) | ||
1061 | kfree(instance->send_buffers[i].base); | ||
1062 | |||
1063 | for (i = 0; i < num_snd_urbs; i++) | ||
1064 | usb_free_urb(instance->senders[i].urb); | ||
1065 | |||
1066 | for (i = 0; i < num_rcv_bufs; i++) | ||
1067 | kfree(instance->receive_buffers[i].base); | ||
1068 | |||
1069 | for (i = 0; i < num_rcv_urbs; i++) | ||
1070 | usb_free_urb(instance->receivers[i].urb); | ||
1071 | |||
1072 | return -ENOMEM; | ||
1073 | } | ||
1074 | |||
1075 | void udsl_instance_disconnect(struct udsl_instance_data *instance) | ||
1076 | { | ||
1077 | int i; | ||
1078 | |||
1079 | dbg("udsl_instance_disconnect entered"); | ||
1080 | |||
1081 | if (!instance) { | ||
1082 | dbg("udsl_instance_disconnect: NULL instance!"); | ||
1083 | return; | ||
1084 | } | ||
1085 | |||
1086 | /* receive finalize */ | ||
1087 | tasklet_disable(&instance->receive_tasklet); | ||
1088 | |||
1089 | for (i = 0; i < num_rcv_urbs; i++) | ||
1090 | usb_kill_urb(instance->receivers[i].urb); | ||
1091 | |||
1092 | /* no need to take the spinlock */ | ||
1093 | INIT_LIST_HEAD(&instance->filled_receive_buffers); | ||
1094 | INIT_LIST_HEAD(&instance->spare_receive_buffers); | ||
1095 | |||
1096 | tasklet_enable(&instance->receive_tasklet); | ||
1097 | |||
1098 | for (i = 0; i < num_rcv_urbs; i++) | ||
1099 | usb_free_urb(instance->receivers[i].urb); | ||
1100 | |||
1101 | for (i = 0; i < num_rcv_bufs; i++) | ||
1102 | kfree(instance->receive_buffers[i].base); | ||
1103 | |||
1104 | /* send finalize */ | ||
1105 | tasklet_disable(&instance->send_tasklet); | ||
1106 | |||
1107 | for (i = 0; i < num_snd_urbs; i++) | ||
1108 | usb_kill_urb(instance->senders[i].urb); | ||
1109 | |||
1110 | /* no need to take the spinlock */ | ||
1111 | INIT_LIST_HEAD(&instance->spare_senders); | ||
1112 | INIT_LIST_HEAD(&instance->spare_send_buffers); | ||
1113 | instance->current_buffer = NULL; | ||
1114 | |||
1115 | tasklet_enable(&instance->send_tasklet); | ||
1116 | |||
1117 | for (i = 0; i < num_snd_urbs; i++) | ||
1118 | usb_free_urb(instance->senders[i].urb); | ||
1119 | |||
1120 | for (i = 0; i < num_snd_bufs; i++) | ||
1121 | kfree(instance->send_buffers[i].base); | ||
1122 | |||
1123 | /* ATM finalize */ | ||
1124 | shutdown_atm_dev(instance->atm_dev); | ||
1125 | } | ||
1126 | |||
1127 | EXPORT_SYMBOL_GPL(udsl_get_instance); | ||
1128 | EXPORT_SYMBOL_GPL(udsl_put_instance); | ||
1129 | EXPORT_SYMBOL_GPL(udsl_instance_setup); | ||
1130 | EXPORT_SYMBOL_GPL(udsl_instance_disconnect); | ||
1131 | |||
1132 | /*********** | ||
1133 | ** init ** | ||
1134 | ***********/ | ||
1135 | |||
1136 | static int __init udsl_usb_init(void) | ||
1137 | { | ||
1138 | dbg("udsl_usb_init: driver version " DRIVER_VERSION); | ||
1139 | |||
1140 | if (sizeof(struct udsl_control) > sizeof(((struct sk_buff *) 0)->cb)) { | ||
1141 | printk(KERN_ERR __FILE__ ": unusable with this kernel!\n"); | ||
1142 | return -EIO; | ||
1143 | } | ||
1144 | |||
1145 | if ((num_rcv_urbs > UDSL_MAX_RCV_URBS) | ||
1146 | || (num_snd_urbs > UDSL_MAX_SND_URBS) | ||
1147 | || (num_rcv_bufs > UDSL_MAX_RCV_BUFS) | ||
1148 | || (num_snd_bufs > UDSL_MAX_SND_BUFS) | ||
1149 | || (rcv_buf_size > UDSL_MAX_RCV_BUF_SIZE) | ||
1150 | || (snd_buf_size > UDSL_MAX_SND_BUF_SIZE)) | ||
1151 | return -EINVAL; | ||
1152 | |||
1153 | return 0; | ||
1154 | } | ||
1155 | |||
1156 | static void __exit udsl_usb_exit(void) | ||
1157 | { | ||
1158 | } | ||
1159 | |||
1160 | module_init(udsl_usb_init); | ||
1161 | module_exit(udsl_usb_exit); | ||
1162 | |||
1163 | MODULE_AUTHOR(DRIVER_AUTHOR); | ||
1164 | MODULE_DESCRIPTION(DRIVER_DESC); | ||
1165 | MODULE_LICENSE("GPL"); | ||
1166 | MODULE_VERSION(DRIVER_VERSION); | ||
1167 | |||
1168 | /************ | ||
1169 | ** debug ** | ||
1170 | ************/ | ||
1171 | |||
1172 | #ifdef VERBOSE_DEBUG | ||
1173 | static int udsl_print_packet(const unsigned char *data, int len) | ||
1174 | { | ||
1175 | unsigned char buffer[256]; | ||
1176 | int i = 0, j = 0; | ||
1177 | |||
1178 | for (i = 0; i < len;) { | ||
1179 | buffer[0] = '\0'; | ||
1180 | sprintf(buffer, "%.3d :", i); | ||
1181 | for (j = 0; (j < 16) && (i < len); j++, i++) { | ||
1182 | sprintf(buffer, "%s %2.2x", buffer, data[i]); | ||
1183 | } | ||
1184 | dbg("%s", buffer); | ||
1185 | } | ||
1186 | return i; | ||
1187 | } | ||
1188 | #endif | ||
diff --git a/drivers/usb/atm/usb_atm.h b/drivers/usb/atm/usb_atm.h deleted file mode 100644 index cf8c5328353..00000000000 --- a/drivers/usb/atm/usb_atm.h +++ /dev/null | |||
@@ -1,176 +0,0 @@ | |||
1 | /****************************************************************************** | ||
2 | * usb_atm.h - Generic USB xDSL driver core | ||
3 | * | ||
4 | * Copyright (C) 2001, Alcatel | ||
5 | * Copyright (C) 2003, Duncan Sands, SolNegro, Josep Comas | ||
6 | * Copyright (C) 2004, David Woodhouse | ||
7 | * | ||
8 | * This program is free software; you can redistribute it and/or modify it | ||
9 | * under the terms of the GNU General Public License as published by the Free | ||
10 | * Software Foundation; either version 2 of the License, or (at your option) | ||
11 | * any later version. | ||
12 | * | ||
13 | * This program is distributed in the hope that it will be useful, but WITHOUT | ||
14 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | ||
15 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for | ||
16 | * more details. | ||
17 | * | ||
18 | * You should have received a copy of the GNU General Public License along with | ||
19 | * this program; if not, write to the Free Software Foundation, Inc., 59 | ||
20 | * Temple Place - Suite 330, Boston, MA 02111-1307, USA. | ||
21 | * | ||
22 | ******************************************************************************/ | ||
23 | |||
24 | #include <linux/config.h> | ||
25 | #include <linux/list.h> | ||
26 | #include <linux/kref.h> | ||
27 | #include <linux/atm.h> | ||
28 | #include <linux/atmdev.h> | ||
29 | #include <asm/semaphore.h> | ||
30 | |||
31 | /* | ||
32 | #define DEBUG | ||
33 | #define VERBOSE_DEBUG | ||
34 | */ | ||
35 | |||
36 | #if !defined (DEBUG) && defined (CONFIG_USB_DEBUG) | ||
37 | # define DEBUG | ||
38 | #endif | ||
39 | |||
40 | #include <linux/usb.h> | ||
41 | |||
42 | #ifdef DEBUG | ||
43 | #define UDSL_ASSERT(x) BUG_ON(!(x)) | ||
44 | #else | ||
45 | #define UDSL_ASSERT(x) do { if (!(x)) warn("failed assertion '" #x "' at line %d", __LINE__); } while(0) | ||
46 | #endif | ||
47 | |||
48 | #define UDSL_MAX_RCV_URBS 4 | ||
49 | #define UDSL_MAX_SND_URBS 4 | ||
50 | #define UDSL_MAX_RCV_BUFS 8 | ||
51 | #define UDSL_MAX_SND_BUFS 8 | ||
52 | #define UDSL_MAX_RCV_BUF_SIZE 1024 /* ATM cells */ | ||
53 | #define UDSL_MAX_SND_BUF_SIZE 1024 /* ATM cells */ | ||
54 | #define UDSL_DEFAULT_RCV_URBS 2 | ||
55 | #define UDSL_DEFAULT_SND_URBS 2 | ||
56 | #define UDSL_DEFAULT_RCV_BUFS 4 | ||
57 | #define UDSL_DEFAULT_SND_BUFS 4 | ||
58 | #define UDSL_DEFAULT_RCV_BUF_SIZE 64 /* ATM cells */ | ||
59 | #define UDSL_DEFAULT_SND_BUF_SIZE 64 /* ATM cells */ | ||
60 | |||
61 | #define ATM_CELL_HEADER (ATM_CELL_SIZE - ATM_CELL_PAYLOAD) | ||
62 | #define UDSL_NUM_CELLS(x) (((x) + ATM_AAL5_TRAILER + ATM_CELL_PAYLOAD - 1) / ATM_CELL_PAYLOAD) | ||
63 | |||
64 | /* receive */ | ||
65 | |||
66 | struct udsl_receive_buffer { | ||
67 | struct list_head list; | ||
68 | unsigned char *base; | ||
69 | unsigned int filled_cells; | ||
70 | }; | ||
71 | |||
72 | struct udsl_receiver { | ||
73 | struct list_head list; | ||
74 | struct udsl_receive_buffer *buffer; | ||
75 | struct urb *urb; | ||
76 | struct udsl_instance_data *instance; | ||
77 | }; | ||
78 | |||
79 | struct udsl_vcc_data { | ||
80 | /* vpi/vci lookup */ | ||
81 | struct list_head list; | ||
82 | short vpi; | ||
83 | int vci; | ||
84 | struct atm_vcc *vcc; | ||
85 | |||
86 | /* raw cell reassembly */ | ||
87 | struct sk_buff *sarb; | ||
88 | }; | ||
89 | |||
90 | /* send */ | ||
91 | |||
92 | struct udsl_send_buffer { | ||
93 | struct list_head list; | ||
94 | unsigned char *base; | ||
95 | unsigned char *free_start; | ||
96 | unsigned int free_cells; | ||
97 | }; | ||
98 | |||
99 | struct udsl_sender { | ||
100 | struct list_head list; | ||
101 | struct udsl_send_buffer *buffer; | ||
102 | struct urb *urb; | ||
103 | struct udsl_instance_data *instance; | ||
104 | }; | ||
105 | |||
106 | struct udsl_control { | ||
107 | struct atm_skb_data atm_data; | ||
108 | unsigned int num_cells; | ||
109 | unsigned int num_entire; | ||
110 | unsigned int pdu_padding; | ||
111 | unsigned char aal5_trailer[ATM_AAL5_TRAILER]; | ||
112 | }; | ||
113 | |||
114 | #define UDSL_SKB(x) ((struct udsl_control *)(x)->cb) | ||
115 | |||
116 | /* main driver data */ | ||
117 | |||
118 | enum udsl_status { | ||
119 | UDSL_NO_FIRMWARE, | ||
120 | UDSL_LOADING_FIRMWARE, | ||
121 | UDSL_LOADED_FIRMWARE | ||
122 | }; | ||
123 | |||
124 | struct udsl_instance_data { | ||
125 | struct kref refcount; | ||
126 | struct semaphore serialize; | ||
127 | |||
128 | /* USB device part */ | ||
129 | struct usb_device *usb_dev; | ||
130 | char description[64]; | ||
131 | int data_endpoint; | ||
132 | int snd_padding; | ||
133 | int rcv_padding; | ||
134 | const char *driver_name; | ||
135 | |||
136 | /* ATM device part */ | ||
137 | struct atm_dev *atm_dev; | ||
138 | struct list_head vcc_list; | ||
139 | |||
140 | /* firmware */ | ||
141 | int (*firmware_wait) (struct udsl_instance_data *); | ||
142 | enum udsl_status status; | ||
143 | wait_queue_head_t firmware_waiters; | ||
144 | |||
145 | /* receive */ | ||
146 | struct udsl_receiver receivers[UDSL_MAX_RCV_URBS]; | ||
147 | struct udsl_receive_buffer receive_buffers[UDSL_MAX_RCV_BUFS]; | ||
148 | |||
149 | spinlock_t receive_lock; | ||
150 | struct list_head spare_receivers; | ||
151 | struct list_head filled_receive_buffers; | ||
152 | |||
153 | struct tasklet_struct receive_tasklet; | ||
154 | struct list_head spare_receive_buffers; | ||
155 | |||
156 | /* send */ | ||
157 | struct udsl_sender senders[UDSL_MAX_SND_URBS]; | ||
158 | struct udsl_send_buffer send_buffers[UDSL_MAX_SND_BUFS]; | ||
159 | |||
160 | struct sk_buff_head sndqueue; | ||
161 | |||
162 | spinlock_t send_lock; | ||
163 | struct list_head spare_senders; | ||
164 | struct list_head spare_send_buffers; | ||
165 | |||
166 | struct tasklet_struct send_tasklet; | ||
167 | struct sk_buff *current_skb; /* being emptied */ | ||
168 | struct udsl_send_buffer *current_buffer; /* being filled */ | ||
169 | struct list_head filled_send_buffers; | ||
170 | }; | ||
171 | |||
172 | extern int udsl_instance_setup(struct usb_device *dev, | ||
173 | struct udsl_instance_data *instance); | ||
174 | extern void udsl_instance_disconnect(struct udsl_instance_data *instance); | ||
175 | extern void udsl_get_instance(struct udsl_instance_data *instance); | ||
176 | extern void udsl_put_instance(struct udsl_instance_data *instance); | ||
diff --git a/drivers/usb/atm/usbatm.c b/drivers/usb/atm/usbatm.c new file mode 100644 index 00000000000..e134e279448 --- /dev/null +++ b/drivers/usb/atm/usbatm.c | |||
@@ -0,0 +1,1231 @@ | |||
1 | /****************************************************************************** | ||
2 | * usbatm.c - Generic USB xDSL driver core | ||
3 | * | ||
4 | * Copyright (C) 2001, Alcatel | ||
5 | * Copyright (C) 2003, Duncan Sands, SolNegro, Josep Comas | ||
6 | * Copyright (C) 2004, David Woodhouse, Roman Kagan | ||
7 | * | ||
8 | * This program is free software; you can redistribute it and/or modify it | ||
9 | * under the terms of the GNU General Public License as published by the Free | ||
10 | * Software Foundation; either version 2 of the License, or (at your option) | ||
11 | * any later version. | ||
12 | * | ||
13 | * This program is distributed in the hope that it will be useful, but WITHOUT | ||
14 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | ||
15 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for | ||
16 | * more details. | ||
17 | * | ||
18 | * You should have received a copy of the GNU General Public License along with | ||
19 | * this program; if not, write to the Free Software Foundation, Inc., 59 | ||
20 | * Temple Place - Suite 330, Boston, MA 02111-1307, USA. | ||
21 | * | ||
22 | ******************************************************************************/ | ||
23 | |||
24 | /* | ||
25 | * Written by Johan Verrept, Duncan Sands (duncan.sands@free.fr) and David Woodhouse | ||
26 | * | ||
27 | * 1.7+: - See the check-in logs | ||
28 | * | ||
29 | * 1.6: - No longer opens a connection if the firmware is not loaded | ||
30 | * - Added support for the speedtouch 330 | ||
31 | * - Removed the limit on the number of devices | ||
32 | * - Module now autoloads on device plugin | ||
33 | * - Merged relevant parts of sarlib | ||
34 | * - Replaced the kernel thread with a tasklet | ||
35 | * - New packet transmission code | ||
36 | * - Changed proc file contents | ||
37 | * - Fixed all known SMP races | ||
38 | * - Many fixes and cleanups | ||
39 | * - Various fixes by Oliver Neukum (oliver@neukum.name) | ||
40 | * | ||
41 | * 1.5A: - Version for inclusion in 2.5 series kernel | ||
42 | * - Modifications by Richard Purdie (rpurdie@rpsys.net) | ||
43 | * - made compatible with kernel 2.5.6 onwards by changing | ||
44 | * usbatm_usb_send_data_context->urb to a pointer and adding code | ||
45 | * to alloc and free it | ||
46 | * - remove_wait_queue() added to usbatm_atm_processqueue_thread() | ||
47 | * | ||
48 | * 1.5: - fixed memory leak when atmsar_decode_aal5 returned NULL. | ||
49 | * (reported by stephen.robinson@zen.co.uk) | ||
50 | * | ||
51 | * 1.4: - changed the spin_lock() under interrupt to spin_lock_irqsave() | ||
52 | * - unlink all active send urbs of a vcc that is being closed. | ||
53 | * | ||
54 | * 1.3.1: - added the version number | ||
55 | * | ||
56 | * 1.3: - Added multiple send urb support | ||
57 | * - fixed memory leak and vcc->tx_inuse starvation bug | ||
58 | * when not enough memory left in vcc. | ||
59 | * | ||
60 | * 1.2: - Fixed race condition in usbatm_usb_send_data() | ||
61 | * 1.1: - Turned off packet debugging | ||
62 | * | ||
63 | */ | ||
64 | |||
65 | #include "usbatm.h" | ||
66 | |||
67 | #include <asm/uaccess.h> | ||
68 | #include <linux/crc32.h> | ||
69 | #include <linux/errno.h> | ||
70 | #include <linux/init.h> | ||
71 | #include <linux/interrupt.h> | ||
72 | #include <linux/kernel.h> | ||
73 | #include <linux/module.h> | ||
74 | #include <linux/moduleparam.h> | ||
75 | #include <linux/proc_fs.h> | ||
76 | #include <linux/sched.h> | ||
77 | #include <linux/signal.h> | ||
78 | #include <linux/slab.h> | ||
79 | #include <linux/smp_lock.h> | ||
80 | #include <linux/stat.h> | ||
81 | #include <linux/timer.h> | ||
82 | #include <linux/wait.h> | ||
83 | |||
84 | #ifdef VERBOSE_DEBUG | ||
85 | static int usbatm_print_packet(const unsigned char *data, int len); | ||
86 | #define PACKETDEBUG(arg...) usbatm_print_packet (arg) | ||
87 | #define vdbg(arg...) dbg (arg) | ||
88 | #else | ||
89 | #define PACKETDEBUG(arg...) | ||
90 | #define vdbg(arg...) | ||
91 | #endif | ||
92 | |||
93 | #define DRIVER_AUTHOR "Johan Verrept, Duncan Sands <duncan.sands@free.fr>" | ||
94 | #define DRIVER_VERSION "1.9" | ||
95 | #define DRIVER_DESC "Generic USB ATM/DSL I/O, version " DRIVER_VERSION | ||
96 | |||
97 | static const char usbatm_driver_name[] = "usbatm"; | ||
98 | |||
99 | #define UDSL_MAX_RCV_URBS 16 | ||
100 | #define UDSL_MAX_SND_URBS 16 | ||
101 | #define UDSL_MAX_RCV_BUF_SIZE 1024 /* ATM cells */ | ||
102 | #define UDSL_MAX_SND_BUF_SIZE 1024 /* ATM cells */ | ||
103 | #define UDSL_DEFAULT_RCV_URBS 4 | ||
104 | #define UDSL_DEFAULT_SND_URBS 4 | ||
105 | #define UDSL_DEFAULT_RCV_BUF_SIZE 64 /* ATM cells */ | ||
106 | #define UDSL_DEFAULT_SND_BUF_SIZE 64 /* ATM cells */ | ||
107 | |||
108 | #define ATM_CELL_HEADER (ATM_CELL_SIZE - ATM_CELL_PAYLOAD) | ||
109 | |||
110 | #define THROTTLE_MSECS 100 /* delay to recover processing after urb submission fails */ | ||
111 | |||
112 | static unsigned int num_rcv_urbs = UDSL_DEFAULT_RCV_URBS; | ||
113 | static unsigned int num_snd_urbs = UDSL_DEFAULT_SND_URBS; | ||
114 | static unsigned int rcv_buf_size = UDSL_DEFAULT_RCV_BUF_SIZE; | ||
115 | static unsigned int snd_buf_size = UDSL_DEFAULT_SND_BUF_SIZE; | ||
116 | |||
117 | module_param(num_rcv_urbs, uint, S_IRUGO); | ||
118 | MODULE_PARM_DESC(num_rcv_urbs, | ||
119 | "Number of urbs used for reception (range: 0-" | ||
120 | __MODULE_STRING(UDSL_MAX_RCV_URBS) ", default: " | ||
121 | __MODULE_STRING(UDSL_DEFAULT_RCV_URBS) ")"); | ||
122 | |||
123 | module_param(num_snd_urbs, uint, S_IRUGO); | ||
124 | MODULE_PARM_DESC(num_snd_urbs, | ||
125 | "Number of urbs used for transmission (range: 0-" | ||
126 | __MODULE_STRING(UDSL_MAX_SND_URBS) ", default: " | ||
127 | __MODULE_STRING(UDSL_DEFAULT_SND_URBS) ")"); | ||
128 | |||
129 | module_param(rcv_buf_size, uint, S_IRUGO); | ||
130 | MODULE_PARM_DESC(rcv_buf_size, | ||
131 | "Size of the buffers used for reception in ATM cells (range: 1-" | ||
132 | __MODULE_STRING(UDSL_MAX_RCV_BUF_SIZE) ", default: " | ||
133 | __MODULE_STRING(UDSL_DEFAULT_RCV_BUF_SIZE) ")"); | ||
134 | |||
135 | module_param(snd_buf_size, uint, S_IRUGO); | ||
136 | MODULE_PARM_DESC(snd_buf_size, | ||
137 | "Size of the buffers used for transmission in ATM cells (range: 1-" | ||
138 | __MODULE_STRING(UDSL_MAX_SND_BUF_SIZE) ", default: " | ||
139 | __MODULE_STRING(UDSL_DEFAULT_SND_BUF_SIZE) ")"); | ||
140 | |||
141 | |||
142 | /* receive */ | ||
143 | |||
144 | struct usbatm_vcc_data { | ||
145 | /* vpi/vci lookup */ | ||
146 | struct list_head list; | ||
147 | short vpi; | ||
148 | int vci; | ||
149 | struct atm_vcc *vcc; | ||
150 | |||
151 | /* raw cell reassembly */ | ||
152 | struct sk_buff *sarb; | ||
153 | }; | ||
154 | |||
155 | |||
156 | /* send */ | ||
157 | |||
158 | struct usbatm_control { | ||
159 | struct atm_skb_data atm; | ||
160 | u32 len; | ||
161 | u32 crc; | ||
162 | }; | ||
163 | |||
164 | #define UDSL_SKB(x) ((struct usbatm_control *)(x)->cb) | ||
165 | |||
166 | |||
167 | /* ATM */ | ||
168 | |||
169 | static void usbatm_atm_dev_close(struct atm_dev *dev); | ||
170 | static int usbatm_atm_open(struct atm_vcc *vcc); | ||
171 | static void usbatm_atm_close(struct atm_vcc *vcc); | ||
172 | static int usbatm_atm_ioctl(struct atm_dev *dev, unsigned int cmd, void __user * arg); | ||
173 | static int usbatm_atm_send(struct atm_vcc *vcc, struct sk_buff *skb); | ||
174 | static int usbatm_atm_proc_read(struct atm_dev *atm_dev, loff_t * pos, char *page); | ||
175 | |||
176 | static struct atmdev_ops usbatm_atm_devops = { | ||
177 | .dev_close = usbatm_atm_dev_close, | ||
178 | .open = usbatm_atm_open, | ||
179 | .close = usbatm_atm_close, | ||
180 | .ioctl = usbatm_atm_ioctl, | ||
181 | .send = usbatm_atm_send, | ||
182 | .proc_read = usbatm_atm_proc_read, | ||
183 | .owner = THIS_MODULE, | ||
184 | }; | ||
185 | |||
186 | |||
187 | /*********** | ||
188 | ** misc ** | ||
189 | ***********/ | ||
190 | |||
191 | static inline unsigned int usbatm_pdu_length(unsigned int length) | ||
192 | { | ||
193 | length += ATM_CELL_PAYLOAD - 1 + ATM_AAL5_TRAILER; | ||
194 | return length - length % ATM_CELL_PAYLOAD; | ||
195 | } | ||
196 | |||
197 | static inline void usbatm_pop(struct atm_vcc *vcc, struct sk_buff *skb) | ||
198 | { | ||
199 | if (vcc->pop) | ||
200 | vcc->pop(vcc, skb); | ||
201 | else | ||
202 | dev_kfree_skb(skb); | ||
203 | } | ||
204 | |||
205 | |||
206 | /*********** | ||
207 | ** urbs ** | ||
208 | ************/ | ||
209 | |||
210 | static inline struct urb *usbatm_pop_urb(struct usbatm_channel *channel) | ||
211 | { | ||
212 | struct urb *urb; | ||
213 | |||
214 | spin_lock_irq(&channel->lock); | ||
215 | if (list_empty(&channel->list)) { | ||
216 | spin_unlock_irq(&channel->lock); | ||
217 | return NULL; | ||
218 | } | ||
219 | |||
220 | urb = list_entry(channel->list.next, struct urb, urb_list); | ||
221 | list_del(&urb->urb_list); | ||
222 | spin_unlock_irq(&channel->lock); | ||
223 | |||
224 | return urb; | ||
225 | } | ||
226 | |||
227 | static inline int usbatm_submit_urb(struct urb *urb) | ||
228 | { | ||
229 | struct usbatm_channel *channel = urb->context; | ||
230 | int ret; | ||
231 | |||
232 | vdbg("%s: submitting urb 0x%p, size %u", | ||
233 | __func__, urb, urb->transfer_buffer_length); | ||
234 | |||
235 | ret = usb_submit_urb(urb, GFP_ATOMIC); | ||
236 | if (ret) { | ||
237 | atm_dbg(channel->usbatm, "%s: urb 0x%p submission failed (%d)!\n", | ||
238 | __func__, urb, ret); | ||
239 | |||
240 | /* consider all errors transient and return the buffer back to the queue */ | ||
241 | urb->status = -EAGAIN; | ||
242 | spin_lock_irq(&channel->lock); | ||
243 | |||
244 | /* must add to the front when sending; doesn't matter when receiving */ | ||
245 | list_add(&urb->urb_list, &channel->list); | ||
246 | |||
247 | spin_unlock_irq(&channel->lock); | ||
248 | |||
249 | /* make sure the channel doesn't stall */ | ||
250 | mod_timer(&channel->delay, jiffies + msecs_to_jiffies(THROTTLE_MSECS)); | ||
251 | } | ||
252 | |||
253 | return ret; | ||
254 | } | ||
255 | |||
256 | static void usbatm_complete(struct urb *urb, struct pt_regs *regs) | ||
257 | { | ||
258 | struct usbatm_channel *channel = urb->context; | ||
259 | unsigned long flags; | ||
260 | |||
261 | vdbg("%s: urb 0x%p, status %d, actual_length %d", | ||
262 | __func__, urb, urb->status, urb->actual_length); | ||
263 | |||
264 | /* usually in_interrupt(), but not always */ | ||
265 | spin_lock_irqsave(&channel->lock, flags); | ||
266 | |||
267 | /* must add to the back when receiving; doesn't matter when sending */ | ||
268 | list_add_tail(&urb->urb_list, &channel->list); | ||
269 | |||
270 | spin_unlock_irqrestore(&channel->lock, flags); | ||
271 | |||
272 | if (unlikely(urb->status)) | ||
273 | /* throttle processing in case of an error */ | ||
274 | mod_timer(&channel->delay, jiffies + msecs_to_jiffies(THROTTLE_MSECS)); | ||
275 | else | ||
276 | tasklet_schedule(&channel->tasklet); | ||
277 | } | ||
278 | |||
279 | |||
280 | /************* | ||
281 | ** decode ** | ||
282 | *************/ | ||
283 | |||
284 | static inline struct usbatm_vcc_data *usbatm_find_vcc(struct usbatm_data *instance, | ||
285 | short vpi, int vci) | ||
286 | { | ||
287 | struct usbatm_vcc_data *vcc; | ||
288 | |||
289 | list_for_each_entry(vcc, &instance->vcc_list, list) | ||
290 | if ((vcc->vci == vci) && (vcc->vpi == vpi)) | ||
291 | return vcc; | ||
292 | return NULL; | ||
293 | } | ||
294 | |||
295 | static void usbatm_extract_cells(struct usbatm_data *instance, | ||
296 | unsigned char *source, unsigned int avail_data) | ||
297 | { | ||
298 | struct usbatm_vcc_data *cached_vcc = NULL; | ||
299 | struct atm_vcc *vcc; | ||
300 | struct sk_buff *sarb; | ||
301 | struct usbatm_vcc_data *vcc_data; | ||
302 | unsigned int stride = instance->rx_channel.stride; | ||
303 | int vci, cached_vci = 0; | ||
304 | short vpi, cached_vpi = 0; | ||
305 | u8 pti; | ||
306 | |||
307 | for (; avail_data >= stride; avail_data -= stride, source += stride) { | ||
308 | vpi = ((source[0] & 0x0f) << 4) | (source[1] >> 4); | ||
309 | vci = ((source[1] & 0x0f) << 12) | (source[2] << 4) | (source[3] >> 4); | ||
310 | pti = ((source[3] & 0xe) >> 1); | ||
311 | |||
312 | vdbg("%s: vpi %hd, vci %d, pti %d", __func__, vpi, vci, pti); | ||
313 | |||
314 | if (cached_vcc && (vci == cached_vci) && (vpi == cached_vpi)) | ||
315 | vcc_data = cached_vcc; | ||
316 | else if ((vcc_data = usbatm_find_vcc(instance, vpi, vci))) { | ||
317 | cached_vcc = vcc_data; | ||
318 | cached_vpi = vpi; | ||
319 | cached_vci = vci; | ||
320 | } else { | ||
321 | atm_dbg(instance, "%s: unknown vpi/vci (%hd/%d)!\n", __func__, vpi, vci); | ||
322 | continue; | ||
323 | } | ||
324 | |||
325 | vcc = vcc_data->vcc; | ||
326 | |||
327 | /* OAM F5 end-to-end */ | ||
328 | if (pti == ATM_PTI_E2EF5) { | ||
329 | atm_warn(instance, "%s: OAM not supported (vpi %d, vci %d)!\n", __func__, vpi, vci); | ||
330 | atomic_inc(&vcc->stats->rx_err); | ||
331 | continue; | ||
332 | } | ||
333 | |||
334 | sarb = vcc_data->sarb; | ||
335 | |||
336 | if (sarb->tail + ATM_CELL_PAYLOAD > sarb->end) { | ||
337 | atm_dbg(instance, "%s: buffer overrun (sarb->len %u, vcc: 0x%p)!\n", | ||
338 | __func__, sarb->len, vcc); | ||
339 | /* discard cells already received */ | ||
340 | skb_trim(sarb, 0); | ||
341 | UDSL_ASSERT(sarb->tail + ATM_CELL_PAYLOAD <= sarb->end); | ||
342 | } | ||
343 | |||
344 | memcpy(sarb->tail, source + ATM_CELL_HEADER, ATM_CELL_PAYLOAD); | ||
345 | __skb_put(sarb, ATM_CELL_PAYLOAD); | ||
346 | |||
347 | if (pti & 1) { | ||
348 | struct sk_buff *skb; | ||
349 | unsigned int length; | ||
350 | unsigned int pdu_length; | ||
351 | |||
352 | length = (source[ATM_CELL_SIZE - 6] << 8) + source[ATM_CELL_SIZE - 5]; | ||
353 | |||
354 | /* guard against overflow */ | ||
355 | if (length > ATM_MAX_AAL5_PDU) { | ||
356 | atm_dbg(instance, "%s: bogus length %u (vcc: 0x%p)!\n", | ||
357 | __func__, length, vcc); | ||
358 | atomic_inc(&vcc->stats->rx_err); | ||
359 | goto out; | ||
360 | } | ||
361 | |||
362 | pdu_length = usbatm_pdu_length(length); | ||
363 | |||
364 | if (sarb->len < pdu_length) { | ||
365 | atm_dbg(instance, "%s: bogus pdu_length %u (sarb->len: %u, vcc: 0x%p)!\n", | ||
366 | __func__, pdu_length, sarb->len, vcc); | ||
367 | atomic_inc(&vcc->stats->rx_err); | ||
368 | goto out; | ||
369 | } | ||
370 | |||
371 | if (crc32_be(~0, sarb->tail - pdu_length, pdu_length) != 0xc704dd7b) { | ||
372 | atm_dbg(instance, "%s: packet failed crc check (vcc: 0x%p)!\n", | ||
373 | __func__, vcc); | ||
374 | atomic_inc(&vcc->stats->rx_err); | ||
375 | goto out; | ||
376 | } | ||
377 | |||
378 | vdbg("%s: got packet (length: %u, pdu_length: %u, vcc: 0x%p)", __func__, length, pdu_length, vcc); | ||
379 | |||
380 | if (!(skb = dev_alloc_skb(length))) { | ||
381 | atm_dbg(instance, "%s: no memory for skb (length: %u)!\n", __func__, length); | ||
382 | atomic_inc(&vcc->stats->rx_drop); | ||
383 | goto out; | ||
384 | } | ||
385 | |||
386 | vdbg("%s: allocated new sk_buff (skb: 0x%p, skb->truesize: %u)", __func__, skb, skb->truesize); | ||
387 | |||
388 | if (!atm_charge(vcc, skb->truesize)) { | ||
389 | atm_dbg(instance, "%s: failed atm_charge (skb->truesize: %u)!\n", __func__, skb->truesize); | ||
390 | dev_kfree_skb(skb); | ||
391 | goto out; /* atm_charge increments rx_drop */ | ||
392 | } | ||
393 | |||
394 | memcpy(skb->data, sarb->tail - pdu_length, length); | ||
395 | __skb_put(skb, length); | ||
396 | |||
397 | vdbg("%s: sending skb 0x%p, skb->len %u, skb->truesize %u", | ||
398 | __func__, skb, skb->len, skb->truesize); | ||
399 | |||
400 | PACKETDEBUG(skb->data, skb->len); | ||
401 | |||
402 | vcc->push(vcc, skb); | ||
403 | |||
404 | atomic_inc(&vcc->stats->rx); | ||
405 | out: | ||
406 | skb_trim(sarb, 0); | ||
407 | } | ||
408 | } | ||
409 | } | ||
410 | |||
411 | |||
412 | /************* | ||
413 | ** encode ** | ||
414 | *************/ | ||
415 | |||
416 | static unsigned int usbatm_write_cells(struct usbatm_data *instance, | ||
417 | struct sk_buff *skb, | ||
418 | u8 *target, unsigned int avail_space) | ||
419 | { | ||
420 | struct usbatm_control *ctrl = UDSL_SKB(skb); | ||
421 | struct atm_vcc *vcc = ctrl->atm.vcc; | ||
422 | unsigned int num_written; | ||
423 | unsigned int stride = instance->tx_channel.stride; | ||
424 | |||
425 | vdbg("%s: skb->len=%d, avail_space=%u", __func__, skb->len, avail_space); | ||
426 | UDSL_ASSERT(!(avail_space % stride)); | ||
427 | |||
428 | for (num_written = 0; num_written < avail_space && ctrl->len; | ||
429 | num_written += stride, target += stride) { | ||
430 | unsigned int data_len = min_t(unsigned int, skb->len, ATM_CELL_PAYLOAD); | ||
431 | unsigned int left = ATM_CELL_PAYLOAD - data_len; | ||
432 | u8 *ptr = target; | ||
433 | |||
434 | ptr[0] = vcc->vpi >> 4; | ||
435 | ptr[1] = (vcc->vpi << 4) | (vcc->vci >> 12); | ||
436 | ptr[2] = vcc->vci >> 4; | ||
437 | ptr[3] = vcc->vci << 4; | ||
438 | ptr[4] = 0xec; | ||
439 | ptr += ATM_CELL_HEADER; | ||
440 | |||
441 | memcpy(ptr, skb->data, data_len); | ||
442 | ptr += data_len; | ||
443 | __skb_pull(skb, data_len); | ||
444 | |||
445 | if(!left) | ||
446 | continue; | ||
447 | |||
448 | memset(ptr, 0, left); | ||
449 | |||
450 | if (left >= ATM_AAL5_TRAILER) { /* trailer will go in this cell */ | ||
451 | u8 *trailer = target + ATM_CELL_SIZE - ATM_AAL5_TRAILER; | ||
452 | /* trailer[0] = 0; UU = 0 */ | ||
453 | /* trailer[1] = 0; CPI = 0 */ | ||
454 | trailer[2] = ctrl->len >> 8; | ||
455 | trailer[3] = ctrl->len; | ||
456 | |||
457 | ctrl->crc = ~ crc32_be(ctrl->crc, ptr, left - 4); | ||
458 | |||
459 | trailer[4] = ctrl->crc >> 24; | ||
460 | trailer[5] = ctrl->crc >> 16; | ||
461 | trailer[6] = ctrl->crc >> 8; | ||
462 | trailer[7] = ctrl->crc; | ||
463 | |||
464 | target[3] |= 0x2; /* adjust PTI */ | ||
465 | |||
466 | ctrl->len = 0; /* tag this skb finished */ | ||
467 | } | ||
468 | else | ||
469 | ctrl->crc = crc32_be(ctrl->crc, ptr, left); | ||
470 | } | ||
471 | |||
472 | return num_written; | ||
473 | } | ||
474 | |||
475 | |||
476 | /************** | ||
477 | ** receive ** | ||
478 | **************/ | ||
479 | |||
480 | static void usbatm_rx_process(unsigned long data) | ||
481 | { | ||
482 | struct usbatm_data *instance = (struct usbatm_data *)data; | ||
483 | struct urb *urb; | ||
484 | |||
485 | while ((urb = usbatm_pop_urb(&instance->rx_channel))) { | ||
486 | vdbg("%s: processing urb 0x%p", __func__, urb); | ||
487 | |||
488 | if (usb_pipeisoc(urb->pipe)) { | ||
489 | int i; | ||
490 | for (i = 0; i < urb->number_of_packets; i++) | ||
491 | if (!urb->iso_frame_desc[i].status) | ||
492 | usbatm_extract_cells(instance, | ||
493 | (u8 *)urb->transfer_buffer + urb->iso_frame_desc[i].offset, | ||
494 | urb->iso_frame_desc[i].actual_length); | ||
495 | } | ||
496 | else | ||
497 | if (!urb->status) | ||
498 | usbatm_extract_cells(instance, urb->transfer_buffer, urb->actual_length); | ||
499 | |||
500 | if (usbatm_submit_urb(urb)) | ||
501 | return; | ||
502 | } | ||
503 | } | ||
504 | |||
505 | |||
506 | /*********** | ||
507 | ** send ** | ||
508 | ***********/ | ||
509 | |||
510 | static void usbatm_tx_process(unsigned long data) | ||
511 | { | ||
512 | struct usbatm_data *instance = (struct usbatm_data *)data; | ||
513 | struct sk_buff *skb = instance->current_skb; | ||
514 | struct urb *urb = NULL; | ||
515 | const unsigned int buf_size = instance->tx_channel.buf_size; | ||
516 | unsigned int num_written = 0; | ||
517 | u8 *buffer = NULL; | ||
518 | |||
519 | if (!skb) | ||
520 | skb = skb_dequeue(&instance->sndqueue); | ||
521 | |||
522 | while (skb) { | ||
523 | if (!urb) { | ||
524 | urb = usbatm_pop_urb(&instance->tx_channel); | ||
525 | if (!urb) | ||
526 | break; /* no more senders */ | ||
527 | buffer = urb->transfer_buffer; | ||
528 | num_written = (urb->status == -EAGAIN) ? | ||
529 | urb->transfer_buffer_length : 0; | ||
530 | } | ||
531 | |||
532 | num_written += usbatm_write_cells(instance, skb, | ||
533 | buffer + num_written, | ||
534 | buf_size - num_written); | ||
535 | |||
536 | vdbg("%s: wrote %u bytes from skb 0x%p to urb 0x%p", | ||
537 | __func__, num_written, skb, urb); | ||
538 | |||
539 | if (!UDSL_SKB(skb)->len) { | ||
540 | struct atm_vcc *vcc = UDSL_SKB(skb)->atm.vcc; | ||
541 | |||
542 | usbatm_pop(vcc, skb); | ||
543 | atomic_inc(&vcc->stats->tx); | ||
544 | |||
545 | skb = skb_dequeue(&instance->sndqueue); | ||
546 | } | ||
547 | |||
548 | if (num_written == buf_size || (!skb && num_written)) { | ||
549 | urb->transfer_buffer_length = num_written; | ||
550 | |||
551 | if (usbatm_submit_urb(urb)) | ||
552 | break; | ||
553 | urb = NULL; | ||
554 | } | ||
555 | } | ||
556 | |||
557 | instance->current_skb = skb; | ||
558 | } | ||
559 | |||
560 | static void usbatm_cancel_send(struct usbatm_data *instance, | ||
561 | struct atm_vcc *vcc) | ||
562 | { | ||
563 | struct sk_buff *skb, *n; | ||
564 | |||
565 | atm_dbg(instance, "%s entered\n", __func__); | ||
566 | spin_lock_irq(&instance->sndqueue.lock); | ||
567 | for (skb = instance->sndqueue.next, n = skb->next; | ||
568 | skb != (struct sk_buff *)&instance->sndqueue; | ||
569 | skb = n, n = skb->next) | ||
570 | if (UDSL_SKB(skb)->atm.vcc == vcc) { | ||
571 | atm_dbg(instance, "%s: popping skb 0x%p\n", __func__, skb); | ||
572 | __skb_unlink(skb, &instance->sndqueue); | ||
573 | usbatm_pop(vcc, skb); | ||
574 | } | ||
575 | spin_unlock_irq(&instance->sndqueue.lock); | ||
576 | |||
577 | tasklet_disable(&instance->tx_channel.tasklet); | ||
578 | if ((skb = instance->current_skb) && (UDSL_SKB(skb)->atm.vcc == vcc)) { | ||
579 | atm_dbg(instance, "%s: popping current skb (0x%p)\n", __func__, skb); | ||
580 | instance->current_skb = NULL; | ||
581 | usbatm_pop(vcc, skb); | ||
582 | } | ||
583 | tasklet_enable(&instance->tx_channel.tasklet); | ||
584 | atm_dbg(instance, "%s done\n", __func__); | ||
585 | } | ||
586 | |||
587 | static int usbatm_atm_send(struct atm_vcc *vcc, struct sk_buff *skb) | ||
588 | { | ||
589 | struct usbatm_data *instance = vcc->dev->dev_data; | ||
590 | struct usbatm_control *ctrl = UDSL_SKB(skb); | ||
591 | int err; | ||
592 | |||
593 | vdbg("%s called (skb 0x%p, len %u)", __func__, skb, skb->len); | ||
594 | |||
595 | if (!instance) { | ||
596 | dbg("%s: NULL data!", __func__); | ||
597 | err = -ENODEV; | ||
598 | goto fail; | ||
599 | } | ||
600 | |||
601 | if (vcc->qos.aal != ATM_AAL5) { | ||
602 | atm_dbg(instance, "%s: unsupported ATM type %d!\n", __func__, vcc->qos.aal); | ||
603 | err = -EINVAL; | ||
604 | goto fail; | ||
605 | } | ||
606 | |||
607 | if (skb->len > ATM_MAX_AAL5_PDU) { | ||
608 | atm_dbg(instance, "%s: packet too long (%d vs %d)!\n", | ||
609 | __func__, skb->len, ATM_MAX_AAL5_PDU); | ||
610 | err = -EINVAL; | ||
611 | goto fail; | ||
612 | } | ||
613 | |||
614 | PACKETDEBUG(skb->data, skb->len); | ||
615 | |||
616 | /* initialize the control block */ | ||
617 | ctrl->atm.vcc = vcc; | ||
618 | ctrl->len = skb->len; | ||
619 | ctrl->crc = crc32_be(~0, skb->data, skb->len); | ||
620 | |||
621 | skb_queue_tail(&instance->sndqueue, skb); | ||
622 | tasklet_schedule(&instance->tx_channel.tasklet); | ||
623 | |||
624 | return 0; | ||
625 | |||
626 | fail: | ||
627 | usbatm_pop(vcc, skb); | ||
628 | return err; | ||
629 | } | ||
630 | |||
631 | |||
632 | /******************** | ||
633 | ** bean counting ** | ||
634 | ********************/ | ||
635 | |||
636 | static void usbatm_destroy_instance(struct kref *kref) | ||
637 | { | ||
638 | struct usbatm_data *instance = container_of(kref, struct usbatm_data, refcount); | ||
639 | |||
640 | dbg("%s", __func__); | ||
641 | |||
642 | tasklet_kill(&instance->rx_channel.tasklet); | ||
643 | tasklet_kill(&instance->tx_channel.tasklet); | ||
644 | usb_put_dev(instance->usb_dev); | ||
645 | kfree(instance); | ||
646 | } | ||
647 | |||
648 | void usbatm_get_instance(struct usbatm_data *instance) | ||
649 | { | ||
650 | dbg("%s", __func__); | ||
651 | |||
652 | kref_get(&instance->refcount); | ||
653 | } | ||
654 | |||
655 | void usbatm_put_instance(struct usbatm_data *instance) | ||
656 | { | ||
657 | dbg("%s", __func__); | ||
658 | |||
659 | kref_put(&instance->refcount, usbatm_destroy_instance); | ||
660 | } | ||
661 | |||
662 | |||
663 | /********** | ||
664 | ** ATM ** | ||
665 | **********/ | ||
666 | |||
667 | static void usbatm_atm_dev_close(struct atm_dev *dev) | ||
668 | { | ||
669 | struct usbatm_data *instance = dev->dev_data; | ||
670 | |||
671 | dbg("%s", __func__); | ||
672 | |||
673 | if (!instance) | ||
674 | return; | ||
675 | |||
676 | dev->dev_data = NULL; | ||
677 | usbatm_put_instance(instance); /* taken in usbatm_atm_init */ | ||
678 | } | ||
679 | |||
680 | static int usbatm_atm_proc_read(struct atm_dev *atm_dev, loff_t * pos, char *page) | ||
681 | { | ||
682 | struct usbatm_data *instance = atm_dev->dev_data; | ||
683 | int left = *pos; | ||
684 | |||
685 | if (!instance) { | ||
686 | dbg("%s: NULL instance!", __func__); | ||
687 | return -ENODEV; | ||
688 | } | ||
689 | |||
690 | if (!left--) | ||
691 | return sprintf(page, "%s\n", instance->description); | ||
692 | |||
693 | if (!left--) | ||
694 | return sprintf(page, "MAC: %02x:%02x:%02x:%02x:%02x:%02x\n", | ||
695 | atm_dev->esi[0], atm_dev->esi[1], | ||
696 | atm_dev->esi[2], atm_dev->esi[3], | ||
697 | atm_dev->esi[4], atm_dev->esi[5]); | ||
698 | |||
699 | if (!left--) | ||
700 | return sprintf(page, | ||
701 | "AAL5: tx %d ( %d err ), rx %d ( %d err, %d drop )\n", | ||
702 | atomic_read(&atm_dev->stats.aal5.tx), | ||
703 | atomic_read(&atm_dev->stats.aal5.tx_err), | ||
704 | atomic_read(&atm_dev->stats.aal5.rx), | ||
705 | atomic_read(&atm_dev->stats.aal5.rx_err), | ||
706 | atomic_read(&atm_dev->stats.aal5.rx_drop)); | ||
707 | |||
708 | if (!left--) | ||
709 | switch (atm_dev->signal) { | ||
710 | case ATM_PHY_SIG_FOUND: | ||
711 | return sprintf(page, "Line up\n"); | ||
712 | case ATM_PHY_SIG_LOST: | ||
713 | return sprintf(page, "Line down\n"); | ||
714 | default: | ||
715 | return sprintf(page, "Line state unknown\n"); | ||
716 | } | ||
717 | |||
718 | return 0; | ||
719 | } | ||
720 | |||
721 | static int usbatm_atm_open(struct atm_vcc *vcc) | ||
722 | { | ||
723 | struct usbatm_data *instance = vcc->dev->dev_data; | ||
724 | struct usbatm_vcc_data *new = NULL; | ||
725 | int ret; | ||
726 | int vci = vcc->vci; | ||
727 | short vpi = vcc->vpi; | ||
728 | |||
729 | if (!instance) { | ||
730 | dbg("%s: NULL data!", __func__); | ||
731 | return -ENODEV; | ||
732 | } | ||
733 | |||
734 | atm_dbg(instance, "%s: vpi %hd, vci %d\n", __func__, vpi, vci); | ||
735 | |||
736 | /* only support AAL5 */ | ||
737 | if ((vcc->qos.aal != ATM_AAL5) || (vcc->qos.rxtp.max_sdu < 0) | ||
738 | || (vcc->qos.rxtp.max_sdu > ATM_MAX_AAL5_PDU)) { | ||
739 | atm_dbg(instance, "%s: unsupported ATM type %d!\n", __func__, vcc->qos.aal); | ||
740 | return -EINVAL; | ||
741 | } | ||
742 | |||
743 | down(&instance->serialize); /* vs self, usbatm_atm_close */ | ||
744 | |||
745 | if (usbatm_find_vcc(instance, vpi, vci)) { | ||
746 | atm_dbg(instance, "%s: %hd/%d already in use!\n", __func__, vpi, vci); | ||
747 | ret = -EADDRINUSE; | ||
748 | goto fail; | ||
749 | } | ||
750 | |||
751 | if (!(new = kmalloc(sizeof(struct usbatm_vcc_data), GFP_KERNEL))) { | ||
752 | atm_dbg(instance, "%s: no memory for vcc_data!\n", __func__); | ||
753 | ret = -ENOMEM; | ||
754 | goto fail; | ||
755 | } | ||
756 | |||
757 | memset(new, 0, sizeof(struct usbatm_vcc_data)); | ||
758 | new->vcc = vcc; | ||
759 | new->vpi = vpi; | ||
760 | new->vci = vci; | ||
761 | |||
762 | new->sarb = alloc_skb(usbatm_pdu_length(vcc->qos.rxtp.max_sdu), GFP_KERNEL); | ||
763 | if (!new->sarb) { | ||
764 | atm_dbg(instance, "%s: no memory for SAR buffer!\n", __func__); | ||
765 | ret = -ENOMEM; | ||
766 | goto fail; | ||
767 | } | ||
768 | |||
769 | vcc->dev_data = new; | ||
770 | |||
771 | tasklet_disable(&instance->rx_channel.tasklet); | ||
772 | list_add(&new->list, &instance->vcc_list); | ||
773 | tasklet_enable(&instance->rx_channel.tasklet); | ||
774 | |||
775 | set_bit(ATM_VF_ADDR, &vcc->flags); | ||
776 | set_bit(ATM_VF_PARTIAL, &vcc->flags); | ||
777 | set_bit(ATM_VF_READY, &vcc->flags); | ||
778 | |||
779 | up(&instance->serialize); | ||
780 | |||
781 | atm_dbg(instance, "%s: allocated vcc data 0x%p\n", __func__, new); | ||
782 | |||
783 | return 0; | ||
784 | |||
785 | fail: | ||
786 | kfree(new); | ||
787 | up(&instance->serialize); | ||
788 | return ret; | ||
789 | } | ||
790 | |||
791 | static void usbatm_atm_close(struct atm_vcc *vcc) | ||
792 | { | ||
793 | struct usbatm_data *instance = vcc->dev->dev_data; | ||
794 | struct usbatm_vcc_data *vcc_data = vcc->dev_data; | ||
795 | |||
796 | if (!instance || !vcc_data) { | ||
797 | dbg("%s: NULL data!", __func__); | ||
798 | return; | ||
799 | } | ||
800 | |||
801 | atm_dbg(instance, "%s entered\n", __func__); | ||
802 | |||
803 | atm_dbg(instance, "%s: deallocating vcc 0x%p with vpi %d vci %d\n", | ||
804 | __func__, vcc_data, vcc_data->vpi, vcc_data->vci); | ||
805 | |||
806 | usbatm_cancel_send(instance, vcc); | ||
807 | |||
808 | down(&instance->serialize); /* vs self, usbatm_atm_open */ | ||
809 | |||
810 | tasklet_disable(&instance->rx_channel.tasklet); | ||
811 | list_del(&vcc_data->list); | ||
812 | tasklet_enable(&instance->rx_channel.tasklet); | ||
813 | |||
814 | kfree_skb(vcc_data->sarb); | ||
815 | vcc_data->sarb = NULL; | ||
816 | |||
817 | kfree(vcc_data); | ||
818 | vcc->dev_data = NULL; | ||
819 | |||
820 | vcc->vpi = ATM_VPI_UNSPEC; | ||
821 | vcc->vci = ATM_VCI_UNSPEC; | ||
822 | clear_bit(ATM_VF_READY, &vcc->flags); | ||
823 | clear_bit(ATM_VF_PARTIAL, &vcc->flags); | ||
824 | clear_bit(ATM_VF_ADDR, &vcc->flags); | ||
825 | |||
826 | up(&instance->serialize); | ||
827 | |||
828 | atm_dbg(instance, "%s successful\n", __func__); | ||
829 | } | ||
830 | |||
831 | static int usbatm_atm_ioctl(struct atm_dev *dev, unsigned int cmd, | ||
832 | void __user * arg) | ||
833 | { | ||
834 | switch (cmd) { | ||
835 | case ATM_QUERYLOOP: | ||
836 | return put_user(ATM_LM_NONE, (int __user *)arg) ? -EFAULT : 0; | ||
837 | default: | ||
838 | return -ENOIOCTLCMD; | ||
839 | } | ||
840 | } | ||
841 | |||
842 | static int usbatm_atm_init(struct usbatm_data *instance) | ||
843 | { | ||
844 | struct atm_dev *atm_dev; | ||
845 | int ret, i; | ||
846 | |||
847 | /* ATM init */ | ||
848 | atm_dev = atm_dev_register(instance->driver_name, &usbatm_atm_devops, -1, NULL); | ||
849 | if (!atm_dev) { | ||
850 | usb_dbg(instance, "%s: failed to register ATM device!\n", __func__); | ||
851 | return -1; | ||
852 | } | ||
853 | |||
854 | instance->atm_dev = atm_dev; | ||
855 | |||
856 | atm_dev->ci_range.vpi_bits = ATM_CI_MAX; | ||
857 | atm_dev->ci_range.vci_bits = ATM_CI_MAX; | ||
858 | atm_dev->signal = ATM_PHY_SIG_UNKNOWN; | ||
859 | |||
860 | /* temp init ATM device, set to 128kbit */ | ||
861 | atm_dev->link_rate = 128 * 1000 / 424; | ||
862 | |||
863 | if (instance->driver->atm_start && ((ret = instance->driver->atm_start(instance, atm_dev)) < 0)) { | ||
864 | atm_dbg(instance, "%s: atm_start failed: %d!\n", __func__, ret); | ||
865 | goto fail; | ||
866 | } | ||
867 | |||
868 | /* ready for ATM callbacks */ | ||
869 | usbatm_get_instance(instance); /* dropped in usbatm_atm_dev_close */ | ||
870 | mb(); | ||
871 | atm_dev->dev_data = instance; | ||
872 | |||
873 | /* submit all rx URBs */ | ||
874 | for (i = 0; i < num_rcv_urbs; i++) | ||
875 | usbatm_submit_urb(instance->urbs[i]); | ||
876 | |||
877 | return 0; | ||
878 | |||
879 | fail: | ||
880 | instance->atm_dev = NULL; | ||
881 | shutdown_atm_dev(atm_dev); /* usbatm_atm_dev_close will eventually be called */ | ||
882 | return ret; | ||
883 | } | ||
884 | |||
885 | |||
886 | /********** | ||
887 | ** USB ** | ||
888 | **********/ | ||
889 | |||
890 | static int usbatm_do_heavy_init(void *arg) | ||
891 | { | ||
892 | struct usbatm_data *instance = arg; | ||
893 | int ret; | ||
894 | |||
895 | daemonize(instance->driver->driver_name); | ||
896 | allow_signal(SIGTERM); | ||
897 | |||
898 | complete(&instance->thread_started); | ||
899 | |||
900 | ret = instance->driver->heavy_init(instance, instance->usb_intf); | ||
901 | |||
902 | if (!ret) | ||
903 | ret = usbatm_atm_init(instance); | ||
904 | |||
905 | down(&instance->serialize); | ||
906 | instance->thread_pid = -1; | ||
907 | up(&instance->serialize); | ||
908 | |||
909 | complete_and_exit(&instance->thread_exited, ret); | ||
910 | } | ||
911 | |||
912 | static int usbatm_heavy_init(struct usbatm_data *instance) | ||
913 | { | ||
914 | int ret = kernel_thread(usbatm_do_heavy_init, instance, CLONE_KERNEL); | ||
915 | |||
916 | if (ret < 0) { | ||
917 | usb_dbg(instance, "%s: failed to create kernel_thread (%d)!\n", __func__, ret); | ||
918 | return ret; | ||
919 | } | ||
920 | |||
921 | down(&instance->serialize); | ||
922 | instance->thread_pid = ret; | ||
923 | up(&instance->serialize); | ||
924 | |||
925 | wait_for_completion(&instance->thread_started); | ||
926 | |||
927 | return 0; | ||
928 | } | ||
929 | |||
930 | static void usbatm_tasklet_schedule(unsigned long data) | ||
931 | { | ||
932 | tasklet_schedule((struct tasklet_struct *) data); | ||
933 | } | ||
934 | |||
935 | static inline void usbatm_init_channel(struct usbatm_channel *channel) | ||
936 | { | ||
937 | spin_lock_init(&channel->lock); | ||
938 | INIT_LIST_HEAD(&channel->list); | ||
939 | channel->delay.function = usbatm_tasklet_schedule; | ||
940 | channel->delay.data = (unsigned long) &channel->tasklet; | ||
941 | init_timer(&channel->delay); | ||
942 | } | ||
943 | |||
944 | int usbatm_usb_probe(struct usb_interface *intf, const struct usb_device_id *id, | ||
945 | struct usbatm_driver *driver) | ||
946 | { | ||
947 | struct device *dev = &intf->dev; | ||
948 | struct usb_device *usb_dev = interface_to_usbdev(intf); | ||
949 | struct usbatm_data *instance; | ||
950 | char *buf; | ||
951 | int error = -ENOMEM; | ||
952 | int i, length; | ||
953 | int need_heavy; | ||
954 | |||
955 | dev_dbg(dev, "%s: trying driver %s with vendor=0x%x, product=0x%x, ifnum %d\n", | ||
956 | __func__, driver->driver_name, | ||
957 | le16_to_cpu(usb_dev->descriptor.idVendor), | ||
958 | le16_to_cpu(usb_dev->descriptor.idProduct), | ||
959 | intf->altsetting->desc.bInterfaceNumber); | ||
960 | |||
961 | /* instance init */ | ||
962 | instance = kmalloc(sizeof(*instance) + sizeof(struct urb *) * (num_rcv_urbs + num_snd_urbs), | ||
963 | GFP_KERNEL); | ||
964 | if (!instance) { | ||
965 | dev_dbg(dev, "%s: no memory for instance data!\n", __func__); | ||
966 | return -ENOMEM; | ||
967 | } | ||
968 | |||
969 | memset(instance, 0, sizeof(*instance)); | ||
970 | |||
971 | /* public fields */ | ||
972 | |||
973 | instance->driver = driver; | ||
974 | snprintf(instance->driver_name, sizeof(instance->driver_name), driver->driver_name); | ||
975 | |||
976 | instance->usb_dev = usb_dev; | ||
977 | instance->usb_intf = intf; | ||
978 | |||
979 | buf = instance->description; | ||
980 | length = sizeof(instance->description); | ||
981 | |||
982 | if ((i = usb_string(usb_dev, usb_dev->descriptor.iProduct, buf, length)) < 0) | ||
983 | goto bind; | ||
984 | |||
985 | buf += i; | ||
986 | length -= i; | ||
987 | |||
988 | i = scnprintf(buf, length, " ("); | ||
989 | buf += i; | ||
990 | length -= i; | ||
991 | |||
992 | if (length <= 0 || (i = usb_make_path(usb_dev, buf, length)) < 0) | ||
993 | goto bind; | ||
994 | |||
995 | buf += i; | ||
996 | length -= i; | ||
997 | |||
998 | snprintf(buf, length, ")"); | ||
999 | |||
1000 | bind: | ||
1001 | need_heavy = 1; | ||
1002 | if (driver->bind && (error = driver->bind(instance, intf, id, &need_heavy)) < 0) { | ||
1003 | dev_dbg(dev, "%s: bind failed: %d!\n", __func__, error); | ||
1004 | goto fail_free; | ||
1005 | } | ||
1006 | |||
1007 | /* private fields */ | ||
1008 | |||
1009 | kref_init(&instance->refcount); /* dropped in usbatm_usb_disconnect */ | ||
1010 | init_MUTEX(&instance->serialize); | ||
1011 | |||
1012 | instance->thread_pid = -1; | ||
1013 | init_completion(&instance->thread_started); | ||
1014 | init_completion(&instance->thread_exited); | ||
1015 | |||
1016 | INIT_LIST_HEAD(&instance->vcc_list); | ||
1017 | |||
1018 | usbatm_init_channel(&instance->rx_channel); | ||
1019 | usbatm_init_channel(&instance->tx_channel); | ||
1020 | tasklet_init(&instance->rx_channel.tasklet, usbatm_rx_process, (unsigned long)instance); | ||
1021 | tasklet_init(&instance->tx_channel.tasklet, usbatm_tx_process, (unsigned long)instance); | ||
1022 | instance->rx_channel.endpoint = usb_rcvbulkpipe(usb_dev, driver->in); | ||
1023 | instance->tx_channel.endpoint = usb_sndbulkpipe(usb_dev, driver->out); | ||
1024 | instance->rx_channel.stride = ATM_CELL_SIZE + driver->rx_padding; | ||
1025 | instance->tx_channel.stride = ATM_CELL_SIZE + driver->tx_padding; | ||
1026 | instance->rx_channel.buf_size = rcv_buf_size * instance->rx_channel.stride; | ||
1027 | instance->tx_channel.buf_size = snd_buf_size * instance->tx_channel.stride; | ||
1028 | instance->rx_channel.usbatm = instance->tx_channel.usbatm = instance; | ||
1029 | |||
1030 | skb_queue_head_init(&instance->sndqueue); | ||
1031 | |||
1032 | for (i = 0; i < num_rcv_urbs + num_snd_urbs; i++) { | ||
1033 | struct urb *urb; | ||
1034 | u8 *buffer; | ||
1035 | unsigned int iso_packets = 0, iso_size = 0; | ||
1036 | struct usbatm_channel *channel = i < num_rcv_urbs ? | ||
1037 | &instance->rx_channel : &instance->tx_channel; | ||
1038 | |||
1039 | if (usb_pipeisoc(channel->endpoint)) { | ||
1040 | /* don't expect iso out endpoints */ | ||
1041 | iso_size = usb_maxpacket(instance->usb_dev, channel->endpoint, 0); | ||
1042 | iso_size -= iso_size % channel->stride; /* alignment */ | ||
1043 | BUG_ON(!iso_size); | ||
1044 | iso_packets = (channel->buf_size - 1) / iso_size + 1; | ||
1045 | } | ||
1046 | |||
1047 | urb = usb_alloc_urb(iso_packets, GFP_KERNEL); | ||
1048 | if (!urb) { | ||
1049 | dev_dbg(dev, "%s: no memory for urb %d!\n", __func__, i); | ||
1050 | goto fail_unbind; | ||
1051 | } | ||
1052 | |||
1053 | buffer = kmalloc(channel->buf_size, GFP_KERNEL); | ||
1054 | if (!buffer) { | ||
1055 | dev_dbg(dev, "%s: no memory for buffer %d!\n", __func__, i); | ||
1056 | goto fail_unbind; | ||
1057 | } | ||
1058 | memset(buffer, 0, channel->buf_size); | ||
1059 | |||
1060 | usb_fill_bulk_urb(urb, instance->usb_dev, channel->endpoint, | ||
1061 | buffer, channel->buf_size, usbatm_complete, channel); | ||
1062 | if (iso_packets) { | ||
1063 | int j; | ||
1064 | urb->interval = 1; | ||
1065 | urb->transfer_flags = URB_ISO_ASAP; | ||
1066 | urb->number_of_packets = iso_packets; | ||
1067 | for (j = 0; j < iso_packets; j++) { | ||
1068 | urb->iso_frame_desc[j].offset = iso_size * j; | ||
1069 | urb->iso_frame_desc[j].length = min_t(int, iso_size, | ||
1070 | channel->buf_size - urb->iso_frame_desc[j].offset); | ||
1071 | } | ||
1072 | } | ||
1073 | |||
1074 | /* put all tx URBs on the list of spares */ | ||
1075 | if (i >= num_rcv_urbs) | ||
1076 | list_add_tail(&urb->urb_list, &channel->list); | ||
1077 | |||
1078 | vdbg("%s: alloced buffer 0x%p buf size %u urb 0x%p", | ||
1079 | __func__, urb->transfer_buffer, urb->transfer_buffer_length, urb); | ||
1080 | instance->urbs[i] = urb; | ||
1081 | } | ||
1082 | |||
1083 | if (need_heavy && driver->heavy_init) { | ||
1084 | error = usbatm_heavy_init(instance); | ||
1085 | } else { | ||
1086 | complete(&instance->thread_exited); /* pretend that heavy_init was run */ | ||
1087 | error = usbatm_atm_init(instance); | ||
1088 | } | ||
1089 | |||
1090 | if (error < 0) | ||
1091 | goto fail_unbind; | ||
1092 | |||
1093 | usb_get_dev(usb_dev); | ||
1094 | usb_set_intfdata(intf, instance); | ||
1095 | |||
1096 | return 0; | ||
1097 | |||
1098 | fail_unbind: | ||
1099 | if (instance->driver->unbind) | ||
1100 | instance->driver->unbind(instance, intf); | ||
1101 | fail_free: | ||
1102 | for (i = 0; i < num_rcv_urbs + num_snd_urbs; i++) { | ||
1103 | if (instance->urbs[i]) | ||
1104 | kfree(instance->urbs[i]->transfer_buffer); | ||
1105 | usb_free_urb(instance->urbs[i]); | ||
1106 | } | ||
1107 | |||
1108 | kfree (instance); | ||
1109 | |||
1110 | return error; | ||
1111 | } | ||
1112 | EXPORT_SYMBOL_GPL(usbatm_usb_probe); | ||
1113 | |||
1114 | void usbatm_usb_disconnect(struct usb_interface *intf) | ||
1115 | { | ||
1116 | struct device *dev = &intf->dev; | ||
1117 | struct usbatm_data *instance = usb_get_intfdata(intf); | ||
1118 | int i; | ||
1119 | |||
1120 | dev_dbg(dev, "%s entered\n", __func__); | ||
1121 | |||
1122 | if (!instance) { | ||
1123 | dev_dbg(dev, "%s: NULL instance!\n", __func__); | ||
1124 | return; | ||
1125 | } | ||
1126 | |||
1127 | usb_set_intfdata(intf, NULL); | ||
1128 | |||
1129 | down(&instance->serialize); | ||
1130 | if (instance->thread_pid >= 0) | ||
1131 | kill_proc(instance->thread_pid, SIGTERM, 1); | ||
1132 | up(&instance->serialize); | ||
1133 | |||
1134 | wait_for_completion(&instance->thread_exited); | ||
1135 | |||
1136 | tasklet_disable(&instance->rx_channel.tasklet); | ||
1137 | tasklet_disable(&instance->tx_channel.tasklet); | ||
1138 | |||
1139 | for (i = 0; i < num_rcv_urbs + num_snd_urbs; i++) | ||
1140 | usb_kill_urb(instance->urbs[i]); | ||
1141 | |||
1142 | del_timer_sync(&instance->rx_channel.delay); | ||
1143 | del_timer_sync(&instance->tx_channel.delay); | ||
1144 | |||
1145 | if (instance->atm_dev && instance->driver->atm_stop) | ||
1146 | instance->driver->atm_stop(instance, instance->atm_dev); | ||
1147 | |||
1148 | if (instance->driver->unbind) | ||
1149 | instance->driver->unbind(instance, intf); | ||
1150 | |||
1151 | instance->driver_data = NULL; | ||
1152 | |||
1153 | /* turn usbatm_[rt]x_process into noop */ | ||
1154 | /* no need to take the spinlock */ | ||
1155 | INIT_LIST_HEAD(&instance->rx_channel.list); | ||
1156 | INIT_LIST_HEAD(&instance->tx_channel.list); | ||
1157 | |||
1158 | tasklet_enable(&instance->rx_channel.tasklet); | ||
1159 | tasklet_enable(&instance->tx_channel.tasklet); | ||
1160 | |||
1161 | for (i = 0; i < num_rcv_urbs + num_snd_urbs; i++) { | ||
1162 | kfree(instance->urbs[i]->transfer_buffer); | ||
1163 | usb_free_urb(instance->urbs[i]); | ||
1164 | } | ||
1165 | |||
1166 | /* ATM finalize */ | ||
1167 | if (instance->atm_dev) | ||
1168 | shutdown_atm_dev(instance->atm_dev); | ||
1169 | |||
1170 | usbatm_put_instance(instance); /* taken in usbatm_usb_probe */ | ||
1171 | } | ||
1172 | EXPORT_SYMBOL_GPL(usbatm_usb_disconnect); | ||
1173 | |||
1174 | |||
1175 | /*********** | ||
1176 | ** init ** | ||
1177 | ***********/ | ||
1178 | |||
1179 | static int __init usbatm_usb_init(void) | ||
1180 | { | ||
1181 | dbg("%s: driver version %s", __func__, DRIVER_VERSION); | ||
1182 | |||
1183 | if (sizeof(struct usbatm_control) > sizeof(((struct sk_buff *) 0)->cb)) { | ||
1184 | printk(KERN_ERR "%s unusable with this kernel!\n", usbatm_driver_name); | ||
1185 | return -EIO; | ||
1186 | } | ||
1187 | |||
1188 | if ((num_rcv_urbs > UDSL_MAX_RCV_URBS) | ||
1189 | || (num_snd_urbs > UDSL_MAX_SND_URBS) | ||
1190 | || (rcv_buf_size < 1) | ||
1191 | || (rcv_buf_size > UDSL_MAX_RCV_BUF_SIZE) | ||
1192 | || (snd_buf_size < 1) | ||
1193 | || (snd_buf_size > UDSL_MAX_SND_BUF_SIZE)) | ||
1194 | return -EINVAL; | ||
1195 | |||
1196 | return 0; | ||
1197 | } | ||
1198 | module_init(usbatm_usb_init); | ||
1199 | |||
1200 | static void __exit usbatm_usb_exit(void) | ||
1201 | { | ||
1202 | dbg("%s", __func__); | ||
1203 | } | ||
1204 | module_exit(usbatm_usb_exit); | ||
1205 | |||
1206 | MODULE_AUTHOR(DRIVER_AUTHOR); | ||
1207 | MODULE_DESCRIPTION(DRIVER_DESC); | ||
1208 | MODULE_LICENSE("GPL"); | ||
1209 | MODULE_VERSION(DRIVER_VERSION); | ||
1210 | |||
1211 | /************ | ||
1212 | ** debug ** | ||
1213 | ************/ | ||
1214 | |||
1215 | #ifdef VERBOSE_DEBUG | ||
1216 | static int usbatm_print_packet(const unsigned char *data, int len) | ||
1217 | { | ||
1218 | unsigned char buffer[256]; | ||
1219 | int i = 0, j = 0; | ||
1220 | |||
1221 | for (i = 0; i < len;) { | ||
1222 | buffer[0] = '\0'; | ||
1223 | sprintf(buffer, "%.3d :", i); | ||
1224 | for (j = 0; (j < 16) && (i < len); j++, i++) { | ||
1225 | sprintf(buffer, "%s %2.2x", buffer, data[i]); | ||
1226 | } | ||
1227 | dbg("%s", buffer); | ||
1228 | } | ||
1229 | return i; | ||
1230 | } | ||
1231 | #endif | ||
diff --git a/drivers/usb/atm/usbatm.h b/drivers/usb/atm/usbatm.h new file mode 100644 index 00000000000..2239804453c --- /dev/null +++ b/drivers/usb/atm/usbatm.h | |||
@@ -0,0 +1,183 @@ | |||
1 | /****************************************************************************** | ||
2 | * usbatm.h - Generic USB xDSL driver core | ||
3 | * | ||
4 | * Copyright (C) 2001, Alcatel | ||
5 | * Copyright (C) 2003, Duncan Sands, SolNegro, Josep Comas | ||
6 | * Copyright (C) 2004, David Woodhouse | ||
7 | * | ||
8 | * This program is free software; you can redistribute it and/or modify it | ||
9 | * under the terms of the GNU General Public License as published by the Free | ||
10 | * Software Foundation; either version 2 of the License, or (at your option) | ||
11 | * any later version. | ||
12 | * | ||
13 | * This program is distributed in the hope that it will be useful, but WITHOUT | ||
14 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | ||
15 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for | ||
16 | * more details. | ||
17 | * | ||
18 | * You should have received a copy of the GNU General Public License along with | ||
19 | * this program; if not, write to the Free Software Foundation, Inc., 59 | ||
20 | * Temple Place - Suite 330, Boston, MA 02111-1307, USA. | ||
21 | * | ||
22 | ******************************************************************************/ | ||
23 | |||
24 | #ifndef _USBATM_H_ | ||
25 | #define _USBATM_H_ | ||
26 | |||
27 | #include <linux/config.h> | ||
28 | |||
29 | /* | ||
30 | #define DEBUG | ||
31 | #define VERBOSE_DEBUG | ||
32 | */ | ||
33 | |||
34 | #if !defined (DEBUG) && defined (CONFIG_USB_DEBUG) | ||
35 | # define DEBUG | ||
36 | #endif | ||
37 | |||
38 | #include <asm/semaphore.h> | ||
39 | #include <linux/atm.h> | ||
40 | #include <linux/atmdev.h> | ||
41 | #include <linux/completion.h> | ||
42 | #include <linux/device.h> | ||
43 | #include <linux/kref.h> | ||
44 | #include <linux/list.h> | ||
45 | #include <linux/stringify.h> | ||
46 | #include <linux/usb.h> | ||
47 | |||
48 | #ifdef DEBUG | ||
49 | #define UDSL_ASSERT(x) BUG_ON(!(x)) | ||
50 | #else | ||
51 | #define UDSL_ASSERT(x) do { if (!(x)) warn("failed assertion '%s' at line %d", __stringify(x), __LINE__); } while(0) | ||
52 | #endif | ||
53 | |||
54 | #define usb_err(instance, format, arg...) \ | ||
55 | dev_err(&(instance)->usb_intf->dev , format , ## arg) | ||
56 | #define usb_info(instance, format, arg...) \ | ||
57 | dev_info(&(instance)->usb_intf->dev , format , ## arg) | ||
58 | #define usb_warn(instance, format, arg...) \ | ||
59 | dev_warn(&(instance)->usb_intf->dev , format , ## arg) | ||
60 | #define usb_dbg(instance, format, arg...) \ | ||
61 | dev_dbg(&(instance)->usb_intf->dev , format , ## arg) | ||
62 | |||
63 | /* FIXME: move to dev_* once ATM is driver model aware */ | ||
64 | #define atm_printk(level, instance, format, arg...) \ | ||
65 | printk(level "ATM dev %d: " format , (instance)->atm_dev->number, ## arg) | ||
66 | |||
67 | #define atm_err(instance, format, arg...) \ | ||
68 | atm_printk(KERN_ERR, instance , format , ## arg) | ||
69 | #define atm_info(instance, format, arg...) \ | ||
70 | atm_printk(KERN_INFO, instance , format , ## arg) | ||
71 | #define atm_warn(instance, format, arg...) \ | ||
72 | atm_printk(KERN_WARNING, instance , format , ## arg) | ||
73 | #ifdef DEBUG | ||
74 | #define atm_dbg(instance, format, arg...) \ | ||
75 | atm_printk(KERN_DEBUG, instance , format , ## arg) | ||
76 | #else | ||
77 | #define atm_dbg(instance, format, arg...) \ | ||
78 | do {} while (0) | ||
79 | #endif | ||
80 | |||
81 | |||
82 | /* mini driver */ | ||
83 | |||
84 | struct usbatm_data; | ||
85 | |||
86 | /* | ||
87 | * Assuming all methods exist and succeed, they are called in this order: | ||
88 | * | ||
89 | * bind, heavy_init, atm_start, ..., atm_stop, unbind | ||
90 | */ | ||
91 | |||
92 | struct usbatm_driver { | ||
93 | struct module *owner; | ||
94 | |||
95 | const char *driver_name; | ||
96 | |||
97 | /* | ||
98 | * init device ... can sleep, or cause probe() failure. Drivers with a heavy_init | ||
99 | * method can avoid having it called by setting need_heavy_init to zero. | ||
100 | */ | ||
101 | int (*bind) (struct usbatm_data *, struct usb_interface *, | ||
102 | const struct usb_device_id *id, int *need_heavy_init); | ||
103 | |||
104 | /* additional device initialization that is too slow to be done in probe() */ | ||
105 | int (*heavy_init) (struct usbatm_data *, struct usb_interface *); | ||
106 | |||
107 | /* cleanup device ... can sleep, but can't fail */ | ||
108 | void (*unbind) (struct usbatm_data *, struct usb_interface *); | ||
109 | |||
110 | /* init ATM device ... can sleep, or cause ATM initialization failure */ | ||
111 | int (*atm_start) (struct usbatm_data *, struct atm_dev *); | ||
112 | |||
113 | /* cleanup ATM device ... can sleep, but can't fail */ | ||
114 | void (*atm_stop) (struct usbatm_data *, struct atm_dev *); | ||
115 | |||
116 | int in; /* rx endpoint */ | ||
117 | int out; /* tx endpoint */ | ||
118 | |||
119 | unsigned rx_padding; | ||
120 | unsigned tx_padding; | ||
121 | }; | ||
122 | |||
123 | extern int usbatm_usb_probe(struct usb_interface *intf, const struct usb_device_id *id, | ||
124 | struct usbatm_driver *driver); | ||
125 | extern void usbatm_usb_disconnect(struct usb_interface *intf); | ||
126 | |||
127 | |||
128 | struct usbatm_channel { | ||
129 | int endpoint; /* usb pipe */ | ||
130 | unsigned int stride; /* ATM cell size + padding */ | ||
131 | unsigned int buf_size; /* urb buffer size */ | ||
132 | spinlock_t lock; | ||
133 | struct list_head list; | ||
134 | struct tasklet_struct tasklet; | ||
135 | struct timer_list delay; | ||
136 | struct usbatm_data *usbatm; | ||
137 | }; | ||
138 | |||
139 | /* main driver data */ | ||
140 | |||
141 | struct usbatm_data { | ||
142 | /****************** | ||
143 | * public fields * | ||
144 | ******************/ | ||
145 | |||
146 | /* mini driver */ | ||
147 | struct usbatm_driver *driver; | ||
148 | void *driver_data; | ||
149 | char driver_name[16]; | ||
150 | |||
151 | /* USB device */ | ||
152 | struct usb_device *usb_dev; | ||
153 | struct usb_interface *usb_intf; | ||
154 | char description[64]; | ||
155 | |||
156 | /* ATM device */ | ||
157 | struct atm_dev *atm_dev; | ||
158 | |||
159 | /******************************** | ||
160 | * private fields - do not use * | ||
161 | ********************************/ | ||
162 | |||
163 | struct kref refcount; | ||
164 | struct semaphore serialize; | ||
165 | |||
166 | /* heavy init */ | ||
167 | int thread_pid; | ||
168 | struct completion thread_started; | ||
169 | struct completion thread_exited; | ||
170 | |||
171 | /* ATM device */ | ||
172 | struct list_head vcc_list; | ||
173 | |||
174 | struct usbatm_channel rx_channel; | ||
175 | struct usbatm_channel tx_channel; | ||
176 | |||
177 | struct sk_buff_head sndqueue; | ||
178 | struct sk_buff *current_skb; /* being emptied */ | ||
179 | |||
180 | struct urb *urbs[0]; | ||
181 | }; | ||
182 | |||
183 | #endif /* _USBATM_H_ */ | ||