blob: 60843b3f3b6f25b4c03ec2b4a8d1d4b63dca8199 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * hfc_usb.c
3 *
Martin Bachemd6c59c12007-08-21 14:26:21 +02004 * $Id: hfc_usb.c,v 2.3.2.20 2007/08/20 14:07:54 mbachem Exp $
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 *
6 * modular HiSax ISDN driver for Colognechip HFC-S USB chip
7 *
Martin Bachemd6c59c12007-08-21 14:26:21 +02008 * Authors : Peter Sprenger (sprenger@moving-bytes.de)
9 * Martin Bachem (m.bachem@gmx.de, info@colognechip.com)
Linus Torvalds1da177e2005-04-16 15:20:36 -070010 *
11 * based on the first hfc_usb driver of
12 * Werner Cornelius (werner@isdn-development.de)
13 *
14 * This program is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation; either version 2, or (at your option)
17 * any later version.
18 *
19 * This program is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
23 *
24 * You should have received a copy of the GNU General Public License
25 * along with this program; if not, write to the Free Software
26 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
27 *
28 * See Version Histroy at the bottom of this file
29 *
30*/
31
32#include <linux/types.h>
33#include <linux/stddef.h>
34#include <linux/timer.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#include <linux/init.h>
36#include <linux/module.h>
37#include <linux/kernel_stat.h>
38#include <linux/usb.h>
39#include <linux/kernel.h>
Martin Bachemd6c59c12007-08-21 14:26:21 +020040#include <linux/smp_lock.h>
41#include <linux/sched.h>
42#include <linux/moduleparam.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070043#include "hisax.h"
44#include "hisax_if.h"
45#include "hfc_usb.h"
46
Linus Torvalds1da177e2005-04-16 15:20:36 -070047static const char *hfcusb_revision =
Martin Bachemd6c59c12007-08-21 14:26:21 +020048 "$Revision: 2.3.2.20 $ $Date: 2007/08/20 14:07:54 $ ";
Linus Torvalds1da177e2005-04-16 15:20:36 -070049
50/* Hisax debug support
Martin Bachemd6c59c12007-08-21 14:26:21 +020051* debug flags defined in hfc_usb.h as HFCUSB_DBG_[*]
Linus Torvalds1da177e2005-04-16 15:20:36 -070052*/
Linus Torvalds1da177e2005-04-16 15:20:36 -070053#define __debug_variable hfc_debug
54#include "hisax_debug.h"
55static u_int debug;
56module_param(debug, uint, 0);
Adrian Bunk672c3fd2005-06-25 14:59:18 -070057static int hfc_debug;
Martin Bachemd6c59c12007-08-21 14:26:21 +020058
Linus Torvalds1da177e2005-04-16 15:20:36 -070059
Martin Bachem597a1072005-11-07 01:00:20 -080060/* private vendor specific data */
61typedef struct {
62 __u8 led_scheme; // led display scheme
63 signed short led_bits[8]; // array of 8 possible LED bitmask settings
64 char *vend_name; // device name
65} hfcsusb_vdata;
Linus Torvalds1da177e2005-04-16 15:20:36 -070066
Martin Bachemd6c59c12007-08-21 14:26:21 +020067/* VID/PID device list */
Martin Bachem597a1072005-11-07 01:00:20 -080068static struct usb_device_id hfcusb_idtab[] = {
69 {
Andrew Mortond6a1a642005-11-15 00:09:13 -080070 USB_DEVICE(0x0959, 0x2bd0),
Martin Bachem597a1072005-11-07 01:00:20 -080071 .driver_info = (unsigned long) &((hfcsusb_vdata)
72 {LED_OFF, {4, 0, 2, 1},
73 "ISDN USB TA (Cologne Chip HFC-S USB based)"}),
74 },
75 {
Andrew Mortond6a1a642005-11-15 00:09:13 -080076 USB_DEVICE(0x0675, 0x1688),
Martin Bachem597a1072005-11-07 01:00:20 -080077 .driver_info = (unsigned long) &((hfcsusb_vdata)
78 {LED_SCHEME1, {1, 2, 0, 0},
79 "DrayTek miniVigor 128 USB ISDN TA"}),
80 },
81 {
Andrew Mortond6a1a642005-11-15 00:09:13 -080082 USB_DEVICE(0x07b0, 0x0007),
Martin Bachem597a1072005-11-07 01:00:20 -080083 .driver_info = (unsigned long) &((hfcsusb_vdata)
84 {LED_SCHEME1, {0x80, -64, -32, -16},
85 "Billion tiny USB ISDN TA 128"}),
86 },
87 {
Andrew Mortond6a1a642005-11-15 00:09:13 -080088 USB_DEVICE(0x0742, 0x2008),
Martin Bachem597a1072005-11-07 01:00:20 -080089 .driver_info = (unsigned long) &((hfcsusb_vdata)
90 {LED_SCHEME1, {4, 0, 2, 1},
91 "Stollmann USB TA"}),
Martin Bachemd6c59c12007-08-21 14:26:21 +020092 },
Martin Bachem597a1072005-11-07 01:00:20 -080093 {
Andrew Mortond6a1a642005-11-15 00:09:13 -080094 USB_DEVICE(0x0742, 0x2009),
Martin Bachem597a1072005-11-07 01:00:20 -080095 .driver_info = (unsigned long) &((hfcsusb_vdata)
96 {LED_SCHEME1, {4, 0, 2, 1},
97 "Aceex USB ISDN TA"}),
Martin Bachemd6c59c12007-08-21 14:26:21 +020098 },
Martin Bachem597a1072005-11-07 01:00:20 -080099 {
Andrew Mortond6a1a642005-11-15 00:09:13 -0800100 USB_DEVICE(0x0742, 0x200A),
Martin Bachem597a1072005-11-07 01:00:20 -0800101 .driver_info = (unsigned long) &((hfcsusb_vdata)
102 {LED_SCHEME1, {4, 0, 2, 1},
103 "OEM USB ISDN TA"}),
Martin Bachemd6c59c12007-08-21 14:26:21 +0200104 },
Martin Bachem597a1072005-11-07 01:00:20 -0800105 {
Andrew Mortond6a1a642005-11-15 00:09:13 -0800106 USB_DEVICE(0x08e3, 0x0301),
Martin Bachem597a1072005-11-07 01:00:20 -0800107 .driver_info = (unsigned long) &((hfcsusb_vdata)
108 {LED_SCHEME1, {2, 0, 1, 4},
109 "Olitec USB RNIS"}),
Martin Bachemd6c59c12007-08-21 14:26:21 +0200110 },
Martin Bachem597a1072005-11-07 01:00:20 -0800111 {
Andrew Mortond6a1a642005-11-15 00:09:13 -0800112 USB_DEVICE(0x07fa, 0x0846),
Martin Bachem597a1072005-11-07 01:00:20 -0800113 .driver_info = (unsigned long) &((hfcsusb_vdata)
114 {LED_SCHEME1, {0x80, -64, -32, -16},
115 "Bewan Modem RNIS USB"}),
Martin Bachemd6c59c12007-08-21 14:26:21 +0200116 },
Martin Bachem597a1072005-11-07 01:00:20 -0800117 {
Andrew Mortond6a1a642005-11-15 00:09:13 -0800118 USB_DEVICE(0x07fa, 0x0847),
Martin Bachem597a1072005-11-07 01:00:20 -0800119 .driver_info = (unsigned long) &((hfcsusb_vdata)
120 {LED_SCHEME1, {0x80, -64, -32, -16},
121 "Djinn Numeris USB"}),
Martin Bachemd6c59c12007-08-21 14:26:21 +0200122 },
Martin Bachem597a1072005-11-07 01:00:20 -0800123 {
Andrew Mortond6a1a642005-11-15 00:09:13 -0800124 USB_DEVICE(0x07b0, 0x0006),
Martin Bachem597a1072005-11-07 01:00:20 -0800125 .driver_info = (unsigned long) &((hfcsusb_vdata)
126 {LED_SCHEME1, {0x80, -64, -32, -16},
127 "Twister ISDN TA"}),
Martin Bachemd6c59c12007-08-21 14:26:21 +0200128 },
Andrew Mortond6a1a642005-11-15 00:09:13 -0800129 { }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130};
131
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132/* structure defining input+output fifos (interrupt/bulk mode) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133struct usb_fifo; /* forward definition */
134typedef struct iso_urb_struct {
135 struct urb *purb;
136 __u8 buffer[ISO_BUFFER_SIZE]; /* buffer incoming/outgoing data */
137 struct usb_fifo *owner_fifo; /* pointer to owner fifo */
138} iso_urb_struct;
139
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140struct hfcusb_data; /* forward definition */
Martin Bachemd6c59c12007-08-21 14:26:21 +0200141
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142typedef struct usb_fifo {
143 int fifonum; /* fifo index attached to this structure */
144 int active; /* fifo is currently active */
145 struct hfcusb_data *hfc; /* pointer to main structure */
146 int pipe; /* address of endpoint */
147 __u8 usb_packet_maxlen; /* maximum length for usb transfer */
148 unsigned int max_size; /* maximum size of receive/send packet */
149 __u8 intervall; /* interrupt interval */
150 struct sk_buff *skbuff; /* actual used buffer */
151 struct urb *urb; /* transfer structure for usb routines */
152 __u8 buffer[128]; /* buffer incoming/outgoing data */
153 int bit_line; /* how much bits are in the fifo? */
154
155 volatile __u8 usb_transfer_mode; /* switched between ISO and INT */
156 iso_urb_struct iso[2]; /* need two urbs to have one always for pending */
157 struct hisax_if *hif; /* hisax interface */
158 int delete_flg; /* only delete skbuff once */
159 int last_urblen; /* remember length of last packet */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160} usb_fifo;
161
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162/* structure holding all data for one device */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163typedef struct hfcusb_data {
164 /* HiSax Interface for loadable Layer1 drivers */
Martin Bachemd6c59c12007-08-21 14:26:21 +0200165 struct hisax_d_if d_if; /* see hisax_if.h */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 struct hisax_b_if b_if[2]; /* see hisax_if.h */
167 int protocol;
168
169 struct usb_device *dev; /* our device */
170 int if_used; /* used interface number */
171 int alt_used; /* used alternate config */
172 int ctrl_paksize; /* control pipe packet size */
Martin Bachemd6c59c12007-08-21 14:26:21 +0200173 int ctrl_in_pipe, /* handles for control pipe */
174 ctrl_out_pipe;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 int cfg_used; /* configuration index used */
176 int vend_idx; /* vendor found */
177 int b_mode[2]; /* B-channel mode */
178 int l1_activated; /* layer 1 activated */
Martin Bachemd6c59c12007-08-21 14:26:21 +0200179 int disc_flag; /* TRUE if device was disonnected to avoid some USB actions */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 int packet_size, iso_packet_size;
181
182 /* control pipe background handling */
183 ctrl_buft ctrl_buff[HFC_CTRL_BUFSIZE]; /* buffer holding queued data */
184 volatile int ctrl_in_idx, ctrl_out_idx, ctrl_cnt; /* input/output pointer + count */
185 struct urb *ctrl_urb; /* transfer structure for control channel */
186
187 struct usb_ctrlrequest ctrl_write; /* buffer for control write request */
188 struct usb_ctrlrequest ctrl_read; /* same for read request */
189
190 __u8 old_led_state, led_state, led_new_data, led_b_active;
191
192 volatile __u8 threshold_mask; /* threshold actually reported */
193 volatile __u8 bch_enables; /* or mask for sctrl_r and sctrl register values */
194
195 usb_fifo fifos[HFCUSB_NUM_FIFOS]; /* structure holding all fifo data */
196
197 volatile __u8 l1_state; /* actual l1 state */
198 struct timer_list t3_timer; /* timer 3 for activation/deactivation */
199 struct timer_list t4_timer; /* timer 4 for activation/deactivation */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200} hfcusb_data;
201
202
203static void collect_rx_frame(usb_fifo * fifo, __u8 * data, int len,
204 int finish);
205
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206static inline const char *
207symbolic(struct hfcusb_symbolic_list list[], const int num)
208{
209 int i;
210 for (i = 0; list[i].name != NULL; i++)
211 if (list[i].num == num)
212 return (list[i].name);
Karsten Keilb05121b2006-03-06 15:42:41 -0800213 return "<unknown ERROR>";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214}
215
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216static void
217ctrl_start_transfer(hfcusb_data * hfc)
218{
219 if (hfc->ctrl_cnt) {
220 hfc->ctrl_urb->pipe = hfc->ctrl_out_pipe;
221 hfc->ctrl_urb->setup_packet = (u_char *) & hfc->ctrl_write;
222 hfc->ctrl_urb->transfer_buffer = NULL;
223 hfc->ctrl_urb->transfer_buffer_length = 0;
224 hfc->ctrl_write.wIndex =
Karsten Keilb05121b2006-03-06 15:42:41 -0800225 cpu_to_le16(hfc->ctrl_buff[hfc->ctrl_out_idx].hfc_reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 hfc->ctrl_write.wValue =
Karsten Keilb05121b2006-03-06 15:42:41 -0800227 cpu_to_le16(hfc->ctrl_buff[hfc->ctrl_out_idx].reg_val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228
229 usb_submit_urb(hfc->ctrl_urb, GFP_ATOMIC); /* start transfer */
230 }
231} /* ctrl_start_transfer */
232
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233static int
234queue_control_request(hfcusb_data * hfc, __u8 reg, __u8 val, int action)
235{
236 ctrl_buft *buf;
237
238 if (hfc->ctrl_cnt >= HFC_CTRL_BUFSIZE)
239 return (1); /* no space left */
240 buf = &hfc->ctrl_buff[hfc->ctrl_in_idx]; /* pointer to new index */
241 buf->hfc_reg = reg;
242 buf->reg_val = val;
243 buf->action = action;
244 if (++hfc->ctrl_in_idx >= HFC_CTRL_BUFSIZE)
245 hfc->ctrl_in_idx = 0; /* pointer wrap */
246 if (++hfc->ctrl_cnt == 1)
247 ctrl_start_transfer(hfc);
248 return (0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249}
250
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251static void
David Howells7d12e782006-10-05 14:55:46 +0100252ctrl_complete(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253{
254 hfcusb_data *hfc = (hfcusb_data *) urb->context;
255 ctrl_buft *buf;
256
257 urb->dev = hfc->dev;
258 if (hfc->ctrl_cnt) {
259 buf = &hfc->ctrl_buff[hfc->ctrl_out_idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 hfc->ctrl_cnt--; /* decrement actual count */
261 if (++hfc->ctrl_out_idx >= HFC_CTRL_BUFSIZE)
262 hfc->ctrl_out_idx = 0; /* pointer wrap */
263
264 ctrl_start_transfer(hfc); /* start next transfer */
265 }
266} /* ctrl_complete */
267
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268/* write led data to auxport & invert if necessary */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269static void
270write_led(hfcusb_data * hfc, __u8 led_state)
271{
272 if (led_state != hfc->old_led_state) {
273 hfc->old_led_state = led_state;
274 queue_control_request(hfc, HFCUSB_P_DATA, led_state, 1);
275 }
276}
277
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278static void
279set_led_bit(hfcusb_data * hfc, signed short led_bits, int unset)
280{
281 if (unset) {
282 if (led_bits < 0)
283 hfc->led_state |= abs(led_bits);
284 else
285 hfc->led_state &= ~led_bits;
286 } else {
287 if (led_bits < 0)
288 hfc->led_state &= ~abs(led_bits);
289 else
290 hfc->led_state |= led_bits;
291 }
292}
293
Martin Bachemd6c59c12007-08-21 14:26:21 +0200294/* handle LED requests */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295static void
296handle_led(hfcusb_data * hfc, int event)
297{
Martin Bachem597a1072005-11-07 01:00:20 -0800298 hfcsusb_vdata *driver_info =
299 (hfcsusb_vdata *) hfcusb_idtab[hfc->vend_idx].driver_info;
300
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 /* if no scheme -> no LED action */
Martin Bachem597a1072005-11-07 01:00:20 -0800302 if (driver_info->led_scheme == LED_OFF)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 return;
304
305 switch (event) {
306 case LED_POWER_ON:
Martin Bachemd6c59c12007-08-21 14:26:21 +0200307 set_led_bit(hfc, driver_info->led_bits[0], 0);
308 set_led_bit(hfc, driver_info->led_bits[1], 1);
309 set_led_bit(hfc, driver_info->led_bits[2], 1);
310 set_led_bit(hfc, driver_info->led_bits[3], 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 break;
Martin Bachemd6c59c12007-08-21 14:26:21 +0200312 case LED_POWER_OFF:
313 set_led_bit(hfc, driver_info->led_bits[0], 1);
314 set_led_bit(hfc, driver_info->led_bits[1], 1);
315 set_led_bit(hfc, driver_info->led_bits[2], 1);
316 set_led_bit(hfc, driver_info->led_bits[3], 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 break;
318 case LED_S0_ON:
Martin Bachemd6c59c12007-08-21 14:26:21 +0200319 set_led_bit(hfc, driver_info->led_bits[1], 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 break;
321 case LED_S0_OFF:
Martin Bachemd6c59c12007-08-21 14:26:21 +0200322 set_led_bit(hfc, driver_info->led_bits[1], 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 break;
324 case LED_B1_ON:
Martin Bachemd6c59c12007-08-21 14:26:21 +0200325 set_led_bit(hfc, driver_info->led_bits[2], 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 break;
327 case LED_B1_OFF:
Martin Bachemd6c59c12007-08-21 14:26:21 +0200328 set_led_bit(hfc, driver_info->led_bits[2], 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 break;
330 case LED_B2_ON:
Martin Bachemd6c59c12007-08-21 14:26:21 +0200331 set_led_bit(hfc, driver_info->led_bits[3], 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 break;
333 case LED_B2_OFF:
Martin Bachemd6c59c12007-08-21 14:26:21 +0200334 set_led_bit(hfc, driver_info->led_bits[3], 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 break;
336 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 write_led(hfc, hfc->led_state);
338}
339
Martin Bachemd6c59c12007-08-21 14:26:21 +0200340/* ISDN l1 timer T3 expires */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341static void
342l1_timer_expire_t3(hfcusb_data * hfc)
343{
344 hfc->d_if.ifc.l1l2(&hfc->d_if.ifc, PH_DEACTIVATE | INDICATION,
345 NULL);
Martin Bachemd6c59c12007-08-21 14:26:21 +0200346
347 DBG(HFCUSB_DBG_STATES,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 "HFC-S USB: PH_DEACTIVATE | INDICATION sent (T3 expire)");
Martin Bachemd6c59c12007-08-21 14:26:21 +0200349
350 hfc->l1_activated = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 handle_led(hfc, LED_S0_OFF);
352 /* deactivate : */
353 queue_control_request(hfc, HFCUSB_STATES, 0x10, 1);
354 queue_control_request(hfc, HFCUSB_STATES, 3, 1);
355}
356
Martin Bachemd6c59c12007-08-21 14:26:21 +0200357/* ISDN l1 timer T4 expires */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358static void
359l1_timer_expire_t4(hfcusb_data * hfc)
360{
361 hfc->d_if.ifc.l1l2(&hfc->d_if.ifc, PH_DEACTIVATE | INDICATION,
362 NULL);
Martin Bachemd6c59c12007-08-21 14:26:21 +0200363
364 DBG(HFCUSB_DBG_STATES,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 "HFC-S USB: PH_DEACTIVATE | INDICATION sent (T4 expire)");
Martin Bachemd6c59c12007-08-21 14:26:21 +0200366
367 hfc->l1_activated = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 handle_led(hfc, LED_S0_OFF);
369}
370
Martin Bachemd6c59c12007-08-21 14:26:21 +0200371/* S0 state changed */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372static void
Martin Bachemd6c59c12007-08-21 14:26:21 +0200373s0_state_handler(hfcusb_data * hfc, __u8 state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374{
375 __u8 old_state;
376
377 old_state = hfc->l1_state;
378 if (state == old_state || state < 1 || state > 8)
379 return;
380
Martin Bachemd6c59c12007-08-21 14:26:21 +0200381 DBG(HFCUSB_DBG_STATES, "HFC-S USB: S0 statechange(%d -> %d)",
382 old_state, state);
383
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 if (state < 4 || state == 7 || state == 8) {
385 if (timer_pending(&hfc->t3_timer))
386 del_timer(&hfc->t3_timer);
Martin Bachemd6c59c12007-08-21 14:26:21 +0200387 DBG(HFCUSB_DBG_STATES, "HFC-S USB: T3 deactivated");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 }
389 if (state >= 7) {
390 if (timer_pending(&hfc->t4_timer))
391 del_timer(&hfc->t4_timer);
Martin Bachemd6c59c12007-08-21 14:26:21 +0200392 DBG(HFCUSB_DBG_STATES, "HFC-S USB: T4 deactivated");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 }
394
395 if (state == 7 && !hfc->l1_activated) {
396 hfc->d_if.ifc.l1l2(&hfc->d_if.ifc,
397 PH_ACTIVATE | INDICATION, NULL);
Martin Bachemd6c59c12007-08-21 14:26:21 +0200398 DBG(HFCUSB_DBG_STATES, "HFC-S USB: PH_ACTIVATE | INDICATION sent");
399 hfc->l1_activated = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 handle_led(hfc, LED_S0_ON);
401 } else if (state <= 3 /* && activated */ ) {
402 if (old_state == 7 || old_state == 8) {
Martin Bachemd6c59c12007-08-21 14:26:21 +0200403 DBG(HFCUSB_DBG_STATES, "HFC-S USB: T4 activated");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 if (!timer_pending(&hfc->t4_timer)) {
405 hfc->t4_timer.expires =
406 jiffies + (HFC_TIMER_T4 * HZ) / 1000;
407 add_timer(&hfc->t4_timer);
408 }
409 } else {
410 hfc->d_if.ifc.l1l2(&hfc->d_if.ifc,
411 PH_DEACTIVATE | INDICATION,
412 NULL);
Martin Bachemd6c59c12007-08-21 14:26:21 +0200413 DBG(HFCUSB_DBG_STATES,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 "HFC-S USB: PH_DEACTIVATE | INDICATION sent");
Martin Bachemd6c59c12007-08-21 14:26:21 +0200415 hfc->l1_activated = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 handle_led(hfc, LED_S0_OFF);
417 }
418 }
419 hfc->l1_state = state;
420}
421
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422static void
423fill_isoc_urb(struct urb *urb, struct usb_device *dev, unsigned int pipe,
424 void *buf, int num_packets, int packet_size, int interval,
425 usb_complete_t complete, void *context)
426{
427 int k;
428
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 urb->dev = dev;
430 urb->pipe = pipe;
431 urb->complete = complete;
432 urb->number_of_packets = num_packets;
433 urb->transfer_buffer_length = packet_size * num_packets;
434 urb->context = context;
435 urb->transfer_buffer = buf;
436 urb->transfer_flags = URB_ISO_ASAP;
437 urb->actual_length = 0;
438 urb->interval = interval;
439 for (k = 0; k < num_packets; k++) {
440 urb->iso_frame_desc[k].offset = packet_size * k;
441 urb->iso_frame_desc[k].length = packet_size;
442 urb->iso_frame_desc[k].actual_length = 0;
443 }
444}
445
446/* allocs urbs and start isoc transfer with two pending urbs to avoid
Martin Bachemd6c59c12007-08-21 14:26:21 +0200447 * gaps in the transfer chain
448 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449static int
450start_isoc_chain(usb_fifo * fifo, int num_packets_per_urb,
451 usb_complete_t complete, int packet_size)
452{
453 int i, k, errcode;
454
Martin Bachemd6c59c12007-08-21 14:26:21 +0200455 DBG(HFCUSB_DBG_INIT, "HFC-S USB: starting ISO-URBs for fifo:%d\n",
456 fifo->fifonum);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457
458 /* allocate Memory for Iso out Urbs */
459 for (i = 0; i < 2; i++) {
460 if (!(fifo->iso[i].purb)) {
461 fifo->iso[i].purb =
462 usb_alloc_urb(num_packets_per_urb, GFP_KERNEL);
463 if (!(fifo->iso[i].purb)) {
464 printk(KERN_INFO
465 "alloc urb for fifo %i failed!!!",
466 fifo->fifonum);
467 }
468 fifo->iso[i].owner_fifo = (struct usb_fifo *) fifo;
469
470 /* Init the first iso */
471 if (ISO_BUFFER_SIZE >=
472 (fifo->usb_packet_maxlen *
473 num_packets_per_urb)) {
474 fill_isoc_urb(fifo->iso[i].purb,
475 fifo->hfc->dev, fifo->pipe,
476 fifo->iso[i].buffer,
477 num_packets_per_urb,
478 fifo->usb_packet_maxlen,
479 fifo->intervall, complete,
480 &fifo->iso[i]);
481 memset(fifo->iso[i].buffer, 0,
482 sizeof(fifo->iso[i].buffer));
483 /* defining packet delimeters in fifo->buffer */
484 for (k = 0; k < num_packets_per_urb; k++) {
485 fifo->iso[i].purb->
486 iso_frame_desc[k].offset =
487 k * packet_size;
488 fifo->iso[i].purb->
489 iso_frame_desc[k].length =
490 packet_size;
491 }
492 } else {
493 printk(KERN_INFO
494 "HFC-S USB: ISO Buffer size to small!\n");
495 }
496 }
497 fifo->bit_line = BITLINE_INF;
498
499 errcode = usb_submit_urb(fifo->iso[i].purb, GFP_KERNEL);
500 fifo->active = (errcode >= 0) ? 1 : 0;
Martin Bachemd6c59c12007-08-21 14:26:21 +0200501 if (errcode < 0)
502 printk(KERN_INFO "HFC-S USB: usb_submit_urb URB nr:%d, error(%i): '%s'\n",
503 i, errcode, symbolic(urb_errlist, errcode));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 }
505 return (fifo->active);
506}
507
508/* stops running iso chain and frees their pending urbs */
509static void
510stop_isoc_chain(usb_fifo * fifo)
511{
512 int i;
513
514 for (i = 0; i < 2; i++) {
515 if (fifo->iso[i].purb) {
Martin Bachemd6c59c12007-08-21 14:26:21 +0200516 DBG(HFCUSB_DBG_INIT,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 "HFC-S USB: Stopping iso chain for fifo %i.%i",
518 fifo->fifonum, i);
Alan Sternae2d9902007-05-23 13:58:12 -0700519 usb_kill_urb(fifo->iso[i].purb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 usb_free_urb(fifo->iso[i].purb);
521 fifo->iso[i].purb = NULL;
522 }
523 }
Martin Bachemd6c59c12007-08-21 14:26:21 +0200524
Alan Sternae2d9902007-05-23 13:58:12 -0700525 usb_kill_urb(fifo->urb);
526 usb_free_urb(fifo->urb);
527 fifo->urb = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 fifo->active = 0;
529}
530
531/* defines how much ISO packets are handled in one URB */
532static int iso_packets[8] =
533 { ISOC_PACKETS_B, ISOC_PACKETS_B, ISOC_PACKETS_B, ISOC_PACKETS_B,
534 ISOC_PACKETS_D, ISOC_PACKETS_D, ISOC_PACKETS_D, ISOC_PACKETS_D
535};
536
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537static void
David Howells7d12e782006-10-05 14:55:46 +0100538tx_iso_complete(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539{
540 iso_urb_struct *context_iso_urb = (iso_urb_struct *) urb->context;
541 usb_fifo *fifo = context_iso_urb->owner_fifo;
542 hfcusb_data *hfc = fifo->hfc;
543 int k, tx_offset, num_isoc_packets, sink, len, current_len,
544 errcode;
545 int frame_complete, transp_mode, fifon, status;
546 __u8 threshbit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547
548 fifon = fifo->fifonum;
549 status = urb->status;
550
551 tx_offset = 0;
552
Martin Bachemd6c59c12007-08-21 14:26:21 +0200553 /* ISO transfer only partially completed,
554 look at individual frame status for details */
555 if (status == -EXDEV) {
556 DBG(HFCUSB_DBG_VERBOSE_USB, "HFC-S USB: tx_iso_complete with -EXDEV"
557 ", urb->status %d, fifonum %d\n",
558 status, fifon);
559
560 for (k = 0; k < iso_packets[fifon]; ++k) {
561 errcode = urb->iso_frame_desc[k].status;
562 if (errcode)
563 DBG(HFCUSB_DBG_VERBOSE_USB, "HFC-S USB: tx_iso_complete "
564 "packet %i, status: %i\n",
565 k, errcode);
566 }
567
568 // clear status, so go on with ISO transfers
569 status = 0;
570 }
571
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 if (fifo->active && !status) {
573 transp_mode = 0;
574 if (fifon < 4 && hfc->b_mode[fifon / 2] == L1_MODE_TRANS)
Martin Bachemd6c59c12007-08-21 14:26:21 +0200575 transp_mode = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576
577 /* is FifoFull-threshold set for our channel? */
Martin Bachemd6c59c12007-08-21 14:26:21 +0200578 threshbit = (hfc->threshold_mask & (1 << fifon));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 num_isoc_packets = iso_packets[fifon];
580
581 /* predict dataflow to avoid fifo overflow */
582 if (fifon >= HFCUSB_D_TX) {
583 sink = (threshbit) ? SINK_DMIN : SINK_DMAX;
584 } else {
585 sink = (threshbit) ? SINK_MIN : SINK_MAX;
586 }
587 fill_isoc_urb(urb, fifo->hfc->dev, fifo->pipe,
588 context_iso_urb->buffer, num_isoc_packets,
589 fifo->usb_packet_maxlen, fifo->intervall,
590 tx_iso_complete, urb->context);
591 memset(context_iso_urb->buffer, 0,
592 sizeof(context_iso_urb->buffer));
Martin Bachemd6c59c12007-08-21 14:26:21 +0200593 frame_complete = 0;
594
595 /* Generate next ISO Packets */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 for (k = 0; k < num_isoc_packets; ++k) {
597 if (fifo->skbuff) {
598 len = fifo->skbuff->len;
599 /* we lower data margin every msec */
600 fifo->bit_line -= sink;
601 current_len = (0 - fifo->bit_line) / 8;
602 /* maximum 15 byte for every ISO packet makes our life easier */
603 if (current_len > 14)
604 current_len = 14;
605 current_len =
606 (len <=
607 current_len) ? len : current_len;
608 /* how much bit do we put on the line? */
609 fifo->bit_line += current_len * 8;
610
611 context_iso_urb->buffer[tx_offset] = 0;
612 if (current_len == len) {
613 if (!transp_mode) {
614 /* here frame completion */
615 context_iso_urb->
616 buffer[tx_offset] = 1;
617 /* add 2 byte flags and 16bit CRC at end of ISDN frame */
618 fifo->bit_line += 32;
619 }
Martin Bachemd6c59c12007-08-21 14:26:21 +0200620 frame_complete = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 }
622
623 memcpy(context_iso_urb->buffer +
624 tx_offset + 1, fifo->skbuff->data,
625 current_len);
626 skb_pull(fifo->skbuff, current_len);
627
628 /* define packet delimeters within the URB buffer */
629 urb->iso_frame_desc[k].offset = tx_offset;
630 urb->iso_frame_desc[k].length =
631 current_len + 1;
632
633 tx_offset += (current_len + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 } else {
635 urb->iso_frame_desc[k].offset =
636 tx_offset++;
637
638 urb->iso_frame_desc[k].length = 1;
639 fifo->bit_line -= sink; /* we lower data margin every msec */
640
641 if (fifo->bit_line < BITLINE_INF) {
642 fifo->bit_line = BITLINE_INF;
643 }
644 }
645
646 if (frame_complete) {
Martin Bachemd6c59c12007-08-21 14:26:21 +0200647 fifo->delete_flg = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 fifo->hif->l1l2(fifo->hif,
649 PH_DATA | CONFIRM,
Alan Cox17a45062006-10-03 01:13:55 -0700650 (void *) (unsigned long) fifo->skbuff->
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 truesize);
652 if (fifo->skbuff && fifo->delete_flg) {
653 dev_kfree_skb_any(fifo->skbuff);
654 fifo->skbuff = NULL;
Martin Bachemd6c59c12007-08-21 14:26:21 +0200655 fifo->delete_flg = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 }
Martin Bachemd6c59c12007-08-21 14:26:21 +0200657 frame_complete = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 }
659 }
660 errcode = usb_submit_urb(urb, GFP_ATOMIC);
661 if (errcode < 0) {
662 printk(KERN_INFO
Martin Bachemd6c59c12007-08-21 14:26:21 +0200663 "HFC-S USB: error submitting ISO URB: %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 errcode);
665 }
666 } else {
667 if (status && !hfc->disc_flag) {
668 printk(KERN_INFO
Martin Bachemd6c59c12007-08-21 14:26:21 +0200669 "HFC-S USB: tx_iso_complete: error(%i): '%s', fifonum=%d\n",
670 status, symbolic(urb_errlist, status), fifon);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 }
672 }
Martin Bachemd6c59c12007-08-21 14:26:21 +0200673}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675static void
David Howells7d12e782006-10-05 14:55:46 +0100676rx_iso_complete(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677{
678 iso_urb_struct *context_iso_urb = (iso_urb_struct *) urb->context;
679 usb_fifo *fifo = context_iso_urb->owner_fifo;
680 hfcusb_data *hfc = fifo->hfc;
681 int k, len, errcode, offset, num_isoc_packets, fifon, maxlen,
682 status;
683 unsigned int iso_status;
684 __u8 *buf;
685 static __u8 eof[8];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686
687 fifon = fifo->fifonum;
688 status = urb->status;
689
690 if (urb->status == -EOVERFLOW) {
Martin Bachemd6c59c12007-08-21 14:26:21 +0200691 DBG(HFCUSB_DBG_VERBOSE_USB,
692 "HFC-USB: ignoring USB DATAOVERRUN fifo(%i)", fifon);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 status = 0;
694 }
Martin Bachemd6c59c12007-08-21 14:26:21 +0200695
696 /* ISO transfer only partially completed,
697 look at individual frame status for details */
698 if (status == -EXDEV) {
699 DBG(HFCUSB_DBG_VERBOSE_USB, "HFC-S USB: rx_iso_complete with -EXDEV "
700 "urb->status %d, fifonum %d\n",
701 status, fifon);
702 status = 0;
703 }
704
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 if (fifo->active && !status) {
706 num_isoc_packets = iso_packets[fifon];
707 maxlen = fifo->usb_packet_maxlen;
708 for (k = 0; k < num_isoc_packets; ++k) {
709 len = urb->iso_frame_desc[k].actual_length;
710 offset = urb->iso_frame_desc[k].offset;
711 buf = context_iso_urb->buffer + offset;
712 iso_status = urb->iso_frame_desc[k].status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713
Martin Bachemd6c59c12007-08-21 14:26:21 +0200714 if (iso_status && !hfc->disc_flag)
715 DBG(HFCUSB_DBG_VERBOSE_USB,
716 "HFC-S USB: rx_iso_complete "
717 "ISO packet %i, status: %i\n",
718 k, iso_status);
719
720 if (fifon == HFCUSB_D_RX) {
721 DBG(HFCUSB_DBG_VERBOSE_USB,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 "HFC-S USB: ISO-D-RX lst_urblen:%2d "
Martin Bachemd6c59c12007-08-21 14:26:21 +0200723 "act_urblen:%2d max-urblen:%2d EOF:0x%0x",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724 fifo->last_urblen, len, maxlen,
725 eof[5]);
Martin Bachemd6c59c12007-08-21 14:26:21 +0200726
727 DBG_PACKET(HFCUSB_DBG_VERBOSE_USB, buf, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 }
Martin Bachemd6c59c12007-08-21 14:26:21 +0200729
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 if (fifo->last_urblen != maxlen) {
731 /* the threshold mask is in the 2nd status byte */
732 hfc->threshold_mask = buf[1];
733 /* care for L1 state only for D-Channel
734 to avoid overlapped iso completions */
Martin Bachemd6c59c12007-08-21 14:26:21 +0200735 if (fifon == HFCUSB_D_RX) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 /* the S0 state is in the upper half
737 of the 1st status byte */
Martin Bachemd6c59c12007-08-21 14:26:21 +0200738 s0_state_handler(hfc, buf[0] >> 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 }
740 eof[fifon] = buf[0] & 1;
741 if (len > 2)
742 collect_rx_frame(fifo, buf + 2,
743 len - 2,
Martin Bachemd6c59c12007-08-21 14:26:21 +0200744 (len < maxlen) ?
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 eof[fifon] : 0);
746 } else {
747 collect_rx_frame(fifo, buf, len,
748 (len <
749 maxlen) ? eof[fifon] :
750 0);
751 }
752 fifo->last_urblen = len;
753 }
754
755 fill_isoc_urb(urb, fifo->hfc->dev, fifo->pipe,
756 context_iso_urb->buffer, num_isoc_packets,
757 fifo->usb_packet_maxlen, fifo->intervall,
758 rx_iso_complete, urb->context);
759 errcode = usb_submit_urb(urb, GFP_ATOMIC);
760 if (errcode < 0) {
Martin Bachemd6c59c12007-08-21 14:26:21 +0200761 printk(KERN_ERR
762 "HFC-S USB: error submitting ISO URB: %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 errcode);
764 }
765 } else {
766 if (status && !hfc->disc_flag) {
Martin Bachemd6c59c12007-08-21 14:26:21 +0200767 printk(KERN_ERR
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 "HFC-S USB: rx_iso_complete : "
769 "urb->status %d, fifonum %d\n",
770 status, fifon);
771 }
772 }
Martin Bachemd6c59c12007-08-21 14:26:21 +0200773}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774
Martin Bachemd6c59c12007-08-21 14:26:21 +0200775/* collect rx data from INT- and ISO-URBs */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776static void
777collect_rx_frame(usb_fifo * fifo, __u8 * data, int len, int finish)
778{
779 hfcusb_data *hfc = fifo->hfc;
780 int transp_mode, fifon;
Martin Bachemd6c59c12007-08-21 14:26:21 +0200781
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 fifon = fifo->fifonum;
783 transp_mode = 0;
784 if (fifon < 4 && hfc->b_mode[fifon / 2] == L1_MODE_TRANS)
Martin Bachemd6c59c12007-08-21 14:26:21 +0200785 transp_mode = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786
787 if (!fifo->skbuff) {
788 fifo->skbuff = dev_alloc_skb(fifo->max_size + 3);
789 if (!fifo->skbuff) {
Martin Bachemd6c59c12007-08-21 14:26:21 +0200790 printk(KERN_ERR
791 "HFC-S USB: cannot allocate buffer for fifo(%d)\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 fifon);
793 return;
794 }
795 }
796 if (len) {
797 if (fifo->skbuff->len + len < fifo->max_size) {
798 memcpy(skb_put(fifo->skbuff, len), data, len);
799 } else {
Martin Bachemd6c59c12007-08-21 14:26:21 +0200800 DBG(HFCUSB_DBG_FIFO_ERR,
801 "HCF-USB: got frame exceeded fifo->max_size(%d) fifo(%d)",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 fifo->max_size, fifon);
Martin Bachemd6c59c12007-08-21 14:26:21 +0200803 DBG_SKB(HFCUSB_DBG_VERBOSE_USB, fifo->skbuff);
804 skb_trim(fifo->skbuff, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 }
806 }
807 if (transp_mode && fifo->skbuff->len >= 128) {
808 fifo->hif->l1l2(fifo->hif, PH_DATA | INDICATION,
809 fifo->skbuff);
810 fifo->skbuff = NULL;
811 return;
812 }
813 /* we have a complete hdlc packet */
814 if (finish) {
815 if ((!fifo->skbuff->data[fifo->skbuff->len - 1])
816 && (fifo->skbuff->len > 3)) {
Martin Bachemd6c59c12007-08-21 14:26:21 +0200817
818 if (fifon == HFCUSB_D_RX) {
819 DBG(HFCUSB_DBG_DCHANNEL,
820 "HFC-S USB: D-RX len(%d)", fifo->skbuff->len);
821 DBG_SKB(HFCUSB_DBG_DCHANNEL, fifo->skbuff);
822 }
823
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 /* remove CRC & status */
825 skb_trim(fifo->skbuff, fifo->skbuff->len - 3);
826 if (fifon == HFCUSB_PCM_RX) {
827 fifo->hif->l1l2(fifo->hif,
828 PH_DATA_E | INDICATION,
829 fifo->skbuff);
830 } else
831 fifo->hif->l1l2(fifo->hif,
832 PH_DATA | INDICATION,
833 fifo->skbuff);
834 fifo->skbuff = NULL; /* buffer was freed from upper layer */
835 } else {
Martin Bachemd6c59c12007-08-21 14:26:21 +0200836 DBG(HFCUSB_DBG_FIFO_ERR,
837 "HFC-S USB: ERROR frame len(%d) fifo(%d)",
838 fifo->skbuff->len, fifon);
839 DBG_SKB(HFCUSB_DBG_VERBOSE_USB, fifo->skbuff);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840 skb_trim(fifo->skbuff, 0);
841 }
842 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843}
844
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845static void
Martin Bachemd6c59c12007-08-21 14:26:21 +0200846rx_int_complete(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847{
848 int len;
849 int status;
850 __u8 *buf, maxlen, fifon;
851 usb_fifo *fifo = (usb_fifo *) urb->context;
852 hfcusb_data *hfc = fifo->hfc;
853 static __u8 eof[8];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854
855 urb->dev = hfc->dev; /* security init */
856
857 fifon = fifo->fifonum;
858 if ((!fifo->active) || (urb->status)) {
Martin Bachemd6c59c12007-08-21 14:26:21 +0200859 DBG(HFCUSB_DBG_INIT, "HFC-S USB: RX-Fifo %i is going down (%i)",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 fifon, urb->status);
Martin Bachemd6c59c12007-08-21 14:26:21 +0200861
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 fifo->urb->interval = 0; /* cancel automatic rescheduling */
863 if (fifo->skbuff) {
864 dev_kfree_skb_any(fifo->skbuff);
865 fifo->skbuff = NULL;
866 }
867 return;
868 }
869 len = urb->actual_length;
870 buf = fifo->buffer;
871 maxlen = fifo->usb_packet_maxlen;
872
Martin Bachemd6c59c12007-08-21 14:26:21 +0200873 if (fifon == HFCUSB_D_RX) {
874 DBG(HFCUSB_DBG_VERBOSE_USB,
875 "HFC-S USB: INT-D-RX lst_urblen:%2d "
876 "act_urblen:%2d max-urblen:%2d EOF:0x%0x",
877 fifo->last_urblen, len, maxlen,
878 eof[5]);
879 DBG_PACKET(HFCUSB_DBG_VERBOSE_USB, buf, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881
882 if (fifo->last_urblen != fifo->usb_packet_maxlen) {
883 /* the threshold mask is in the 2nd status byte */
884 hfc->threshold_mask = buf[1];
885 /* the S0 state is in the upper half of the 1st status byte */
Martin Bachemd6c59c12007-08-21 14:26:21 +0200886 s0_state_handler(hfc, buf[0] >> 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 eof[fifon] = buf[0] & 1;
888 /* if we have more than the 2 status bytes -> collect data */
889 if (len > 2)
890 collect_rx_frame(fifo, buf + 2,
891 urb->actual_length - 2,
892 (len < maxlen) ? eof[fifon] : 0);
893 } else {
894 collect_rx_frame(fifo, buf, urb->actual_length,
895 (len < maxlen) ? eof[fifon] : 0);
896 }
897 fifo->last_urblen = urb->actual_length;
898 status = usb_submit_urb(urb, GFP_ATOMIC);
899 if (status) {
900 printk(KERN_INFO
Martin Bachemd6c59c12007-08-21 14:26:21 +0200901 "HFC-S USB: %s error resubmitting URB fifo(%d)\n",
902 __FUNCTION__, fifon);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903 }
Martin Bachemd6c59c12007-08-21 14:26:21 +0200904}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905
Martin Bachemd6c59c12007-08-21 14:26:21 +0200906/* start initial INT-URB for certain fifo */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907static void
908start_int_fifo(usb_fifo * fifo)
909{
910 int errcode;
911
Martin Bachemd6c59c12007-08-21 14:26:21 +0200912 DBG(HFCUSB_DBG_INIT, "HFC-S USB: starting RX INT-URB for fifo:%d\n",
913 fifo->fifonum);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914
915 if (!fifo->urb) {
916 fifo->urb = usb_alloc_urb(0, GFP_KERNEL);
917 if (!fifo->urb)
918 return;
919 }
920 usb_fill_int_urb(fifo->urb, fifo->hfc->dev, fifo->pipe,
921 fifo->buffer, fifo->usb_packet_maxlen,
Martin Bachemd6c59c12007-08-21 14:26:21 +0200922 rx_int_complete, fifo, fifo->intervall);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923 fifo->active = 1; /* must be marked active */
924 errcode = usb_submit_urb(fifo->urb, GFP_KERNEL);
925 if (errcode) {
Martin Bachemd6c59c12007-08-21 14:26:21 +0200926 printk(KERN_ERR
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927 "HFC-S USB: submit URB error(start_int_info): status:%i\n",
928 errcode);
929 fifo->active = 0;
930 fifo->skbuff = NULL;
931 }
Martin Bachemd6c59c12007-08-21 14:26:21 +0200932}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934static void
Martin Bachemd6c59c12007-08-21 14:26:21 +0200935setup_bchannel(hfcusb_data * hfc, int channel, int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936{
937 __u8 val, idx_table[2] = { 0, 2 };
938
939 if (hfc->disc_flag) {
940 return;
941 }
Martin Bachemd6c59c12007-08-21 14:26:21 +0200942 DBG(HFCUSB_DBG_STATES, "HFC-S USB: setting channel %d to mode %d",
943 channel, mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944 hfc->b_mode[channel] = mode;
945
946 /* setup CON_HDLC */
947 val = 0;
948 if (mode != L1_MODE_NULL)
949 val = 8; /* enable fifo? */
950 if (mode == L1_MODE_TRANS)
951 val |= 2; /* set transparent bit */
952
953 /* set FIFO to transmit register */
954 queue_control_request(hfc, HFCUSB_FIFO, idx_table[channel], 1);
955 queue_control_request(hfc, HFCUSB_CON_HDLC, val, 1);
956 /* reset fifo */
957 queue_control_request(hfc, HFCUSB_INC_RES_F, 2, 1);
958 /* set FIFO to receive register */
959 queue_control_request(hfc, HFCUSB_FIFO, idx_table[channel] + 1, 1);
960 queue_control_request(hfc, HFCUSB_CON_HDLC, val, 1);
961 /* reset fifo */
962 queue_control_request(hfc, HFCUSB_INC_RES_F, 2, 1);
963
964 val = 0x40;
965 if (hfc->b_mode[0])
966 val |= 1;
967 if (hfc->b_mode[1])
968 val |= 2;
969 queue_control_request(hfc, HFCUSB_SCTRL, val, 1);
970
971 val = 0;
972 if (hfc->b_mode[0])
973 val |= 1;
974 if (hfc->b_mode[1])
975 val |= 2;
976 queue_control_request(hfc, HFCUSB_SCTRL_R, val, 1);
977
978 if (mode == L1_MODE_NULL) {
979 if (channel)
980 handle_led(hfc, LED_B2_OFF);
981 else
982 handle_led(hfc, LED_B1_OFF);
983 } else {
984 if (channel)
985 handle_led(hfc, LED_B2_ON);
986 else
987 handle_led(hfc, LED_B1_ON);
988 }
989}
990
Adrian Bunk672c3fd2005-06-25 14:59:18 -0700991static void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992hfc_usb_l2l1(struct hisax_if *my_hisax_if, int pr, void *arg)
993{
994 usb_fifo *fifo = my_hisax_if->priv;
995 hfcusb_data *hfc = fifo->hfc;
996
997 switch (pr) {
998 case PH_ACTIVATE | REQUEST:
999 if (fifo->fifonum == HFCUSB_D_TX) {
Martin Bachemd6c59c12007-08-21 14:26:21 +02001000 DBG(HFCUSB_DBG_STATES,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001 "HFC_USB: hfc_usb_d_l2l1 D-chan: PH_ACTIVATE | REQUEST");
Martin Bachemd6c59c12007-08-21 14:26:21 +02001002
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003 if (hfc->l1_state != 3
1004 && hfc->l1_state != 7) {
1005 hfc->d_if.ifc.l1l2(&hfc->d_if.ifc,
1006 PH_DEACTIVATE |
1007 INDICATION,
1008 NULL);
Martin Bachemd6c59c12007-08-21 14:26:21 +02001009 DBG(HFCUSB_DBG_STATES,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010 "HFC-S USB: PH_DEACTIVATE | INDICATION sent (not state 3 or 7)");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011 } else {
1012 if (hfc->l1_state == 7) { /* l1 already active */
1013 hfc->d_if.ifc.l1l2(&hfc->
1014 d_if.
1015 ifc,
1016 PH_ACTIVATE
1017 |
1018 INDICATION,
1019 NULL);
Martin Bachemd6c59c12007-08-21 14:26:21 +02001020 DBG(HFCUSB_DBG_STATES,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 "HFC-S USB: PH_ACTIVATE | INDICATION sent again ;)");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022 } else {
1023 /* force sending sending INFO1 */
1024 queue_control_request(hfc,
1025 HFCUSB_STATES,
1026 0x14,
1027 1);
1028 mdelay(1);
1029 /* start l1 activation */
1030 queue_control_request(hfc,
1031 HFCUSB_STATES,
1032 0x04,
1033 1);
1034 if (!timer_pending
1035 (&hfc->t3_timer)) {
1036 hfc->t3_timer.
1037 expires =
1038 jiffies +
1039 (HFC_TIMER_T3 *
1040 HZ) / 1000;
1041 add_timer(&hfc->
1042 t3_timer);
1043 }
1044 }
1045 }
1046 } else {
Martin Bachemd6c59c12007-08-21 14:26:21 +02001047 DBG(HFCUSB_DBG_STATES,
1048 "HFC_USB: hfc_usb_d_l2l1 B-chan: PH_ACTIVATE | REQUEST");
1049 setup_bchannel(hfc,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050 (fifo->fifonum ==
1051 HFCUSB_B1_TX) ? 0 : 1,
Alan Cox17a45062006-10-03 01:13:55 -07001052 (long) arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053 fifo->hif->l1l2(fifo->hif,
1054 PH_ACTIVATE | INDICATION,
1055 NULL);
1056 }
1057 break;
1058 case PH_DEACTIVATE | REQUEST:
1059 if (fifo->fifonum == HFCUSB_D_TX) {
Martin Bachemd6c59c12007-08-21 14:26:21 +02001060 DBG(HFCUSB_DBG_STATES,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061 "HFC_USB: hfc_usb_d_l2l1 D-chan: PH_DEACTIVATE | REQUEST");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 } else {
Martin Bachemd6c59c12007-08-21 14:26:21 +02001063 DBG(HFCUSB_DBG_STATES,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064 "HFC_USB: hfc_usb_d_l2l1 Bx-chan: PH_DEACTIVATE | REQUEST");
Martin Bachemd6c59c12007-08-21 14:26:21 +02001065 setup_bchannel(hfc,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066 (fifo->fifonum ==
1067 HFCUSB_B1_TX) ? 0 : 1,
1068 (int) L1_MODE_NULL);
1069 fifo->hif->l1l2(fifo->hif,
1070 PH_DEACTIVATE | INDICATION,
1071 NULL);
1072 }
1073 break;
1074 case PH_DATA | REQUEST:
1075 if (fifo->skbuff && fifo->delete_flg) {
1076 dev_kfree_skb_any(fifo->skbuff);
1077 fifo->skbuff = NULL;
Martin Bachemd6c59c12007-08-21 14:26:21 +02001078 fifo->delete_flg = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 }
1080 fifo->skbuff = arg; /* we have a new buffer */
1081 break;
1082 default:
Martin Bachemd6c59c12007-08-21 14:26:21 +02001083 DBG(HFCUSB_DBG_STATES,
1084 "HFC_USB: hfc_usb_d_l2l1: unkown state : %#x", pr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085 break;
1086 }
1087}
1088
Martin Bachemd6c59c12007-08-21 14:26:21 +02001089/* initial init HFC-S USB chip registers, HiSax interface, USB URBs */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090static int
Martin Bachemd6c59c12007-08-21 14:26:21 +02001091hfc_usb_init(hfcusb_data * hfc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092{
1093 usb_fifo *fifo;
1094 int i, err;
1095 u_char b;
1096 struct hisax_b_if *p_b_if[2];
1097
1098 /* check the chip id */
1099 if (read_usb(hfc, HFCUSB_CHIP_ID, &b) != 1) {
1100 printk(KERN_INFO "HFC-USB: cannot read chip id\n");
1101 return (1);
1102 }
1103 if (b != HFCUSB_CHIPID) {
1104 printk(KERN_INFO "HFC-S USB: Invalid chip id 0x%02x\n", b);
1105 return (1);
1106 }
1107
1108 /* first set the needed config, interface and alternate */
1109 err = usb_set_interface(hfc->dev, hfc->if_used, hfc->alt_used);
1110
1111 /* do Chip reset */
1112 write_usb(hfc, HFCUSB_CIRM, 8);
1113 /* aux = output, reset off */
1114 write_usb(hfc, HFCUSB_CIRM, 0x10);
1115
Martin Bachemd6c59c12007-08-21 14:26:21 +02001116 /* set USB_SIZE to match wMaxPacketSize for INT or BULK transfers */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117 write_usb(hfc, HFCUSB_USB_SIZE,
1118 (hfc->packet_size / 8) | ((hfc->packet_size / 8) << 4));
1119
Martin Bachemd6c59c12007-08-21 14:26:21 +02001120 /* set USB_SIZE_I to match wMaxPacketSize for ISO transfers */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121 write_usb(hfc, HFCUSB_USB_SIZE_I, hfc->iso_packet_size);
1122
1123 /* enable PCM/GCI master mode */
1124 write_usb(hfc, HFCUSB_MST_MODE1, 0); /* set default values */
1125 write_usb(hfc, HFCUSB_MST_MODE0, 1); /* enable master mode */
1126
1127 /* init the fifos */
1128 write_usb(hfc, HFCUSB_F_THRES,
1129 (HFCUSB_TX_THRESHOLD /
1130 8) | ((HFCUSB_RX_THRESHOLD / 8) << 4));
1131
1132 fifo = hfc->fifos;
1133 for (i = 0; i < HFCUSB_NUM_FIFOS; i++) {
1134 write_usb(hfc, HFCUSB_FIFO, i); /* select the desired fifo */
1135 fifo[i].skbuff = NULL; /* init buffer pointer */
1136 fifo[i].max_size =
1137 (i <= HFCUSB_B2_RX) ? MAX_BCH_SIZE : MAX_DFRAME_LEN;
1138 fifo[i].last_urblen = 0;
1139 /* set 2 bit for D- & E-channel */
1140 write_usb(hfc, HFCUSB_HDLC_PAR,
1141 ((i <= HFCUSB_B2_RX) ? 0 : 2));
1142 /* rx hdlc, enable IFF for D-channel */
1143 write_usb(hfc, HFCUSB_CON_HDLC,
1144 ((i == HFCUSB_D_TX) ? 0x09 : 0x08));
1145 write_usb(hfc, HFCUSB_INC_RES_F, 2); /* reset the fifo */
1146 }
1147
1148 write_usb(hfc, HFCUSB_CLKDEL, 0x0f); /* clock delay value */
1149 write_usb(hfc, HFCUSB_STATES, 3 | 0x10); /* set deactivated mode */
1150 write_usb(hfc, HFCUSB_STATES, 3); /* enable state machine */
1151
1152 write_usb(hfc, HFCUSB_SCTRL_R, 0); /* disable both B receivers */
1153 write_usb(hfc, HFCUSB_SCTRL, 0x40); /* disable B transmitters + capacitive mode */
1154
1155 /* set both B-channel to not connected */
1156 hfc->b_mode[0] = L1_MODE_NULL;
1157 hfc->b_mode[1] = L1_MODE_NULL;
1158
Martin Bachemd6c59c12007-08-21 14:26:21 +02001159 hfc->l1_activated = 0;
1160 hfc->disc_flag = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161 hfc->led_state = 0;
1162 hfc->led_new_data = 0;
1163 hfc->old_led_state = 0;
1164
1165 /* init the t3 timer */
1166 init_timer(&hfc->t3_timer);
1167 hfc->t3_timer.data = (long) hfc;
1168 hfc->t3_timer.function = (void *) l1_timer_expire_t3;
1169
1170 /* init the t4 timer */
1171 init_timer(&hfc->t4_timer);
1172 hfc->t4_timer.data = (long) hfc;
1173 hfc->t4_timer.function = (void *) l1_timer_expire_t4;
1174
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175 /* init the background machinery for control requests */
1176 hfc->ctrl_read.bRequestType = 0xc0;
1177 hfc->ctrl_read.bRequest = 1;
Karsten Keilb05121b2006-03-06 15:42:41 -08001178 hfc->ctrl_read.wLength = cpu_to_le16(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179 hfc->ctrl_write.bRequestType = 0x40;
1180 hfc->ctrl_write.bRequest = 0;
1181 hfc->ctrl_write.wLength = 0;
1182 usb_fill_control_urb(hfc->ctrl_urb,
1183 hfc->dev,
1184 hfc->ctrl_out_pipe,
1185 (u_char *) & hfc->ctrl_write,
1186 NULL, 0, ctrl_complete, hfc);
1187 /* Init All Fifos */
1188 for (i = 0; i < HFCUSB_NUM_FIFOS; i++) {
1189 hfc->fifos[i].iso[0].purb = NULL;
1190 hfc->fifos[i].iso[1].purb = NULL;
1191 hfc->fifos[i].active = 0;
1192 }
1193 /* register Modul to upper Hisax Layers */
1194 hfc->d_if.owner = THIS_MODULE;
1195 hfc->d_if.ifc.priv = &hfc->fifos[HFCUSB_D_TX];
1196 hfc->d_if.ifc.l2l1 = hfc_usb_l2l1;
1197 for (i = 0; i < 2; i++) {
1198 hfc->b_if[i].ifc.priv = &hfc->fifos[HFCUSB_B1_TX + i * 2];
1199 hfc->b_if[i].ifc.l2l1 = hfc_usb_l2l1;
1200 p_b_if[i] = &hfc->b_if[i];
1201 }
1202 /* default Prot: EURO ISDN, should be a module_param */
1203 hfc->protocol = 2;
Alan Sternae2d9902007-05-23 13:58:12 -07001204 i = hisax_register(&hfc->d_if, p_b_if, "hfc_usb", hfc->protocol);
1205 if (i) {
1206 printk(KERN_INFO "HFC-S USB: hisax_register -> %d\n", i);
1207 return i;
1208 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209
1210#ifdef CONFIG_HISAX_DEBUG
1211 hfc_debug = debug;
1212#endif
1213
1214 for (i = 0; i < 4; i++)
1215 hfc->fifos[i].hif = &p_b_if[i / 2]->ifc;
1216 for (i = 4; i < 8; i++)
1217 hfc->fifos[i].hif = &hfc->d_if.ifc;
1218
1219 /* 3 (+1) INT IN + 3 ISO OUT */
1220 if (hfc->cfg_used == CNF_3INT3ISO || hfc->cfg_used == CNF_4INT3ISO) {
1221 start_int_fifo(hfc->fifos + HFCUSB_D_RX);
1222 if (hfc->fifos[HFCUSB_PCM_RX].pipe)
1223 start_int_fifo(hfc->fifos + HFCUSB_PCM_RX);
1224 start_int_fifo(hfc->fifos + HFCUSB_B1_RX);
1225 start_int_fifo(hfc->fifos + HFCUSB_B2_RX);
1226 }
1227 /* 3 (+1) ISO IN + 3 ISO OUT */
1228 if (hfc->cfg_used == CNF_3ISO3ISO || hfc->cfg_used == CNF_4ISO3ISO) {
1229 start_isoc_chain(hfc->fifos + HFCUSB_D_RX, ISOC_PACKETS_D,
1230 rx_iso_complete, 16);
1231 if (hfc->fifos[HFCUSB_PCM_RX].pipe)
1232 start_isoc_chain(hfc->fifos + HFCUSB_PCM_RX,
1233 ISOC_PACKETS_D, rx_iso_complete,
1234 16);
1235 start_isoc_chain(hfc->fifos + HFCUSB_B1_RX, ISOC_PACKETS_B,
1236 rx_iso_complete, 16);
1237 start_isoc_chain(hfc->fifos + HFCUSB_B2_RX, ISOC_PACKETS_B,
1238 rx_iso_complete, 16);
1239 }
1240
1241 start_isoc_chain(hfc->fifos + HFCUSB_D_TX, ISOC_PACKETS_D,
1242 tx_iso_complete, 1);
1243 start_isoc_chain(hfc->fifos + HFCUSB_B1_TX, ISOC_PACKETS_B,
1244 tx_iso_complete, 1);
1245 start_isoc_chain(hfc->fifos + HFCUSB_B2_TX, ISOC_PACKETS_B,
1246 tx_iso_complete, 1);
1247
1248 handle_led(hfc, LED_POWER_ON);
1249
1250 return (0);
Martin Bachemd6c59c12007-08-21 14:26:21 +02001251}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252
Martin Bachemd6c59c12007-08-21 14:26:21 +02001253/* initial callback for each plugged USB device */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254static int
1255hfc_usb_probe(struct usb_interface *intf, const struct usb_device_id *id)
1256{
1257 struct usb_device *dev = interface_to_usbdev(intf);
1258 hfcusb_data *context;
1259 struct usb_host_interface *iface = intf->cur_altsetting;
1260 struct usb_host_interface *iface_used = NULL;
1261 struct usb_host_endpoint *ep;
1262 int ifnum = iface->desc.bInterfaceNumber;
1263 int i, idx, alt_idx, probe_alt_setting, vend_idx, cfg_used, *vcf,
1264 attr, cfg_found, cidx, ep_addr;
1265 int cmptbl[16], small_match, iso_packet_size, packet_size,
1266 alt_used = 0;
Martin Bachem597a1072005-11-07 01:00:20 -08001267 hfcsusb_vdata *driver_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268
1269 vend_idx = 0xffff;
Martin Bachem597a1072005-11-07 01:00:20 -08001270 for (i = 0; hfcusb_idtab[i].idVendor; i++) {
Karsten Keilb05121b2006-03-06 15:42:41 -08001271 if ((le16_to_cpu(dev->descriptor.idVendor) == hfcusb_idtab[i].idVendor)
1272 && (le16_to_cpu(dev->descriptor.idProduct) == hfcusb_idtab[i].idProduct)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273 vend_idx = i;
Martin Bachem597a1072005-11-07 01:00:20 -08001274 continue;
1275 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276 }
Martin Bachem597a1072005-11-07 01:00:20 -08001277
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278 printk(KERN_INFO
1279 "HFC-S USB: probing interface(%d) actalt(%d) minor(%d)\n",
1280 ifnum, iface->desc.bAlternateSetting, intf->minor);
1281
1282 if (vend_idx != 0xffff) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283 /* if vendor and product ID is OK, start probing alternate settings */
1284 alt_idx = 0;
1285 small_match = 0xffff;
1286
1287 /* default settings */
1288 iso_packet_size = 16;
1289 packet_size = 64;
1290
1291 while (alt_idx < intf->num_altsetting) {
1292 iface = intf->altsetting + alt_idx;
1293 probe_alt_setting = iface->desc.bAlternateSetting;
1294 cfg_used = 0;
1295
1296 /* check for config EOL element */
1297 while (validconf[cfg_used][0]) {
Martin Bachemd6c59c12007-08-21 14:26:21 +02001298 cfg_found = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299 vcf = validconf[cfg_used];
1300 /* first endpoint descriptor */
1301 ep = iface->endpoint;
Martin Bachemd6c59c12007-08-21 14:26:21 +02001302
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303 memcpy(cmptbl, vcf, 16 * sizeof(int));
1304
1305 /* check for all endpoints in this alternate setting */
1306 for (i = 0; i < iface->desc.bNumEndpoints;
1307 i++) {
1308 ep_addr =
1309 ep->desc.bEndpointAddress;
1310 /* get endpoint base */
1311 idx = ((ep_addr & 0x7f) - 1) * 2;
1312 if (ep_addr & 0x80)
1313 idx++;
1314 attr = ep->desc.bmAttributes;
1315 if (cmptbl[idx] == EP_NUL) {
Martin Bachemd6c59c12007-08-21 14:26:21 +02001316 cfg_found = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317 }
1318 if (attr == USB_ENDPOINT_XFER_INT
1319 && cmptbl[idx] == EP_INT)
1320 cmptbl[idx] = EP_NUL;
1321 if (attr == USB_ENDPOINT_XFER_BULK
1322 && cmptbl[idx] == EP_BLK)
1323 cmptbl[idx] = EP_NUL;
1324 if (attr == USB_ENDPOINT_XFER_ISOC
1325 && cmptbl[idx] == EP_ISO)
1326 cmptbl[idx] = EP_NUL;
1327
1328 /* check if all INT endpoints match minimum interval */
Martin Bachemd6c59c12007-08-21 14:26:21 +02001329 if ((attr == USB_ENDPOINT_XFER_INT)
1330 && (ep->desc.bInterval < vcf[17])) {
1331 cfg_found = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332 }
1333 ep++;
1334 }
1335 for (i = 0; i < 16; i++) {
1336 /* all entries must be EP_NOP or EP_NUL for a valid config */
1337 if (cmptbl[i] != EP_NOP
1338 && cmptbl[i] != EP_NUL)
Martin Bachemd6c59c12007-08-21 14:26:21 +02001339 cfg_found = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340 }
1341 if (cfg_found) {
1342 if (cfg_used < small_match) {
1343 small_match = cfg_used;
1344 alt_used =
1345 probe_alt_setting;
1346 iface_used = iface;
1347 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348 }
1349 cfg_used++;
1350 }
1351 alt_idx++;
Martin Bachemd6c59c12007-08-21 14:26:21 +02001352 } /* (alt_idx < intf->num_altsetting) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353
1354 /* found a valid USB Ta Endpint config */
1355 if (small_match != 0xffff) {
1356 iface = iface_used;
Martin Bachemd6c59c12007-08-21 14:26:21 +02001357 if (!(context = kzalloc(sizeof(hfcusb_data), GFP_KERNEL)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358 return (-ENOMEM); /* got no mem */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359
1360 ep = iface->endpoint;
1361 vcf = validconf[small_match];
1362
1363 for (i = 0; i < iface->desc.bNumEndpoints; i++) {
1364 ep_addr = ep->desc.bEndpointAddress;
1365 /* get endpoint base */
1366 idx = ((ep_addr & 0x7f) - 1) * 2;
1367 if (ep_addr & 0x80)
1368 idx++;
1369 cidx = idx & 7;
1370 attr = ep->desc.bmAttributes;
1371
1372 /* init Endpoints */
1373 if (vcf[idx] != EP_NOP
1374 && vcf[idx] != EP_NUL) {
1375 switch (attr) {
1376 case USB_ENDPOINT_XFER_INT:
1377 context->
1378 fifos[cidx].
1379 pipe =
1380 usb_rcvintpipe
1381 (dev,
1382 ep->desc.
1383 bEndpointAddress);
1384 context->
1385 fifos[cidx].
1386 usb_transfer_mode
1387 = USB_INT;
1388 packet_size =
Karsten Keilb05121b2006-03-06 15:42:41 -08001389 le16_to_cpu(ep->desc.wMaxPacketSize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390 break;
1391 case USB_ENDPOINT_XFER_BULK:
1392 if (ep_addr & 0x80)
1393 context->
1394 fifos
1395 [cidx].
1396 pipe =
1397 usb_rcvbulkpipe
1398 (dev,
1399 ep->
1400 desc.
1401 bEndpointAddress);
1402 else
1403 context->
1404 fifos
1405 [cidx].
1406 pipe =
1407 usb_sndbulkpipe
1408 (dev,
1409 ep->
1410 desc.
1411 bEndpointAddress);
1412 context->
1413 fifos[cidx].
1414 usb_transfer_mode
1415 = USB_BULK;
1416 packet_size =
Karsten Keilb05121b2006-03-06 15:42:41 -08001417 le16_to_cpu(ep->desc.wMaxPacketSize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418 break;
1419 case USB_ENDPOINT_XFER_ISOC:
1420 if (ep_addr & 0x80)
1421 context->
1422 fifos
1423 [cidx].
1424 pipe =
1425 usb_rcvisocpipe
1426 (dev,
1427 ep->
1428 desc.
1429 bEndpointAddress);
1430 else
1431 context->
1432 fifos
1433 [cidx].
1434 pipe =
1435 usb_sndisocpipe
1436 (dev,
1437 ep->
1438 desc.
1439 bEndpointAddress);
1440 context->
1441 fifos[cidx].
1442 usb_transfer_mode
1443 = USB_ISOC;
1444 iso_packet_size =
Karsten Keilb05121b2006-03-06 15:42:41 -08001445 le16_to_cpu(ep->desc.wMaxPacketSize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446 break;
1447 default:
1448 context->
1449 fifos[cidx].
1450 pipe = 0;
1451 } /* switch attribute */
1452
1453 if (context->fifos[cidx].pipe) {
1454 context->fifos[cidx].
1455 fifonum = cidx;
1456 context->fifos[cidx].hfc =
1457 context;
Karsten Keilb05121b2006-03-06 15:42:41 -08001458 context->fifos[cidx].usb_packet_maxlen =
1459 le16_to_cpu(ep->desc.wMaxPacketSize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460 context->fifos[cidx].
1461 intervall =
1462 ep->desc.bInterval;
1463 context->fifos[cidx].
1464 skbuff = NULL;
1465 }
1466 }
1467 ep++;
1468 }
1469 context->dev = dev; /* save device */
1470 context->if_used = ifnum; /* save used interface */
1471 context->alt_used = alt_used; /* and alternate config */
1472 context->ctrl_paksize = dev->descriptor.bMaxPacketSize0; /* control size */
1473 context->cfg_used = vcf[16]; /* store used config */
1474 context->vend_idx = vend_idx; /* store found vendor */
1475 context->packet_size = packet_size;
1476 context->iso_packet_size = iso_packet_size;
1477
1478 /* create the control pipes needed for register access */
1479 context->ctrl_in_pipe =
1480 usb_rcvctrlpipe(context->dev, 0);
1481 context->ctrl_out_pipe =
1482 usb_sndctrlpipe(context->dev, 0);
1483 context->ctrl_urb = usb_alloc_urb(0, GFP_KERNEL);
1484
Martin Bachem597a1072005-11-07 01:00:20 -08001485 driver_info =
1486 (hfcsusb_vdata *) hfcusb_idtab[vend_idx].
1487 driver_info;
1488 printk(KERN_INFO "HFC-S USB: detected \"%s\"\n",
1489 driver_info->vend_name);
Martin Bachemd6c59c12007-08-21 14:26:21 +02001490
1491 DBG(HFCUSB_DBG_INIT,
1492 "HFC-S USB: Endpoint-Config: %s (if=%d alt=%d), E-Channel(%d)",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493 conf_str[small_match], context->if_used,
Martin Bachemd6c59c12007-08-21 14:26:21 +02001494 context->alt_used,
1495 validconf[small_match][18]);
1496
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497 /* init the chip and register the driver */
Martin Bachemd6c59c12007-08-21 14:26:21 +02001498 if (hfc_usb_init(context)) {
Alan Sternae2d9902007-05-23 13:58:12 -07001499 usb_kill_urb(context->ctrl_urb);
1500 usb_free_urb(context->ctrl_urb);
1501 context->ctrl_urb = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502 kfree(context);
1503 return (-EIO);
1504 }
1505 usb_set_intfdata(intf, context);
1506 return (0);
1507 }
1508 } else {
1509 printk(KERN_INFO
1510 "HFC-S USB: no valid vendor found in USB descriptor\n");
1511 }
1512 return (-EIO);
1513}
1514
Martin Bachemd6c59c12007-08-21 14:26:21 +02001515/* callback for unplugged USB device */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516static void
1517hfc_usb_disconnect(struct usb_interface
1518 *intf)
1519{
1520 hfcusb_data *context = usb_get_intfdata(intf);
1521 int i;
Martin Bachemd6c59c12007-08-21 14:26:21 +02001522
1523 handle_led(context, LED_POWER_OFF);
1524 schedule_timeout((10 * HZ) / 1000);
1525
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526 printk(KERN_INFO "HFC-S USB: device disconnect\n");
Martin Bachemd6c59c12007-08-21 14:26:21 +02001527 context->disc_flag = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528 usb_set_intfdata(intf, NULL);
1529 if (!context)
1530 return;
1531 if (timer_pending(&context->t3_timer))
1532 del_timer(&context->t3_timer);
1533 if (timer_pending(&context->t4_timer))
1534 del_timer(&context->t4_timer);
Martin Bachemd6c59c12007-08-21 14:26:21 +02001535
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536 /* tell all fifos to terminate */
1537 for (i = 0; i < HFCUSB_NUM_FIFOS; i++) {
1538 if (context->fifos[i].usb_transfer_mode == USB_ISOC) {
1539 if (context->fifos[i].active > 0) {
1540 stop_isoc_chain(&context->fifos[i]);
Martin Bachemd6c59c12007-08-21 14:26:21 +02001541 DBG(HFCUSB_DBG_INIT,
1542 "HFC-S USB: %s stopping ISOC chain Fifo(%i)",
1543 __FUNCTION__, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544 }
1545 } else {
1546 if (context->fifos[i].active > 0) {
1547 context->fifos[i].active = 0;
Martin Bachemd6c59c12007-08-21 14:26:21 +02001548 DBG(HFCUSB_DBG_INIT,
1549 "HFC-S USB: %s unlinking URB for Fifo(%i)",
1550 __FUNCTION__, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551 }
Alan Sternae2d9902007-05-23 13:58:12 -07001552 usb_kill_urb(context->fifos[i].urb);
1553 usb_free_urb(context->fifos[i].urb);
1554 context->fifos[i].urb = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555 }
1556 context->fifos[i].active = 0;
1557 }
Alan Sternae2d9902007-05-23 13:58:12 -07001558 usb_kill_urb(context->ctrl_urb);
1559 usb_free_urb(context->ctrl_urb);
1560 context->ctrl_urb = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561 hisax_unregister(&context->d_if);
1562 kfree(context); /* free our structure again */
Martin Bachemd6c59c12007-08-21 14:26:21 +02001563}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565static struct usb_driver hfc_drv = {
Martin Bachem597a1072005-11-07 01:00:20 -08001566 .name = "hfc_usb",
1567 .id_table = hfcusb_idtab,
1568 .probe = hfc_usb_probe,
1569 .disconnect = hfc_usb_disconnect,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570};
Martin Bachemd6c59c12007-08-21 14:26:21 +02001571
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572static void __exit
Martin Bachemd6c59c12007-08-21 14:26:21 +02001573hfc_usb_mod_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574{
Martin Bachemd6c59c12007-08-21 14:26:21 +02001575 usb_deregister(&hfc_drv); /* release our driver */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576 printk(KERN_INFO "HFC-S USB: module removed\n");
1577}
1578
1579static int __init
Martin Bachemd6c59c12007-08-21 14:26:21 +02001580hfc_usb_mod_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582 char revstr[30], datestr[30], dummy[30];
Martin Bachemd6c59c12007-08-21 14:26:21 +02001583#ifndef CONFIG_HISAX_DEBUG
1584 hfc_debug = debug;
1585#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586 sscanf(hfcusb_revision,
1587 "%s %s $ %s %s %s $ ", dummy, revstr,
1588 dummy, datestr, dummy);
1589 printk(KERN_INFO
1590 "HFC-S USB: driver module revision %s date %s loaded, (debug=%i)\n",
1591 revstr, datestr, debug);
1592 if (usb_register(&hfc_drv)) {
1593 printk(KERN_INFO
1594 "HFC-S USB: Unable to register HFC-S USB module at usb stack\n");
1595 return (-1); /* unable to register */
1596 }
1597 return (0);
1598}
1599
Martin Bachemd6c59c12007-08-21 14:26:21 +02001600module_init(hfc_usb_mod_init);
1601module_exit(hfc_usb_mod_exit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602MODULE_AUTHOR(DRIVER_AUTHOR);
1603MODULE_DESCRIPTION(DRIVER_DESC);
1604MODULE_LICENSE("GPL");
Martin Bachem597a1072005-11-07 01:00:20 -08001605MODULE_DEVICE_TABLE(usb, hfcusb_idtab);