blob: aaaeaafd86f4caa0b1e92aeacc4848346081fd49 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * hfc_usb.c
3 *
Martin Bachem7441dd12007-10-14 18:10:30 +02004 * $Id: hfc_usb.c,v 2.3.2.24 2007/10/14 08:40:29 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/sched.h>
41#include <linux/moduleparam.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070042#include "hisax.h"
43#include "hisax_if.h"
44#include "hfc_usb.h"
45
Linus Torvalds1da177e2005-04-16 15:20:36 -070046static const char *hfcusb_revision =
Martin Bachem7441dd12007-10-14 18:10:30 +020047 "$Revision: 2.3.2.24 $ $Date: 2007/10/14 08:40:29 $ ";
Linus Torvalds1da177e2005-04-16 15:20:36 -070048
49/* Hisax debug support
Martin Bachemd6c59c12007-08-21 14:26:21 +020050* debug flags defined in hfc_usb.h as HFCUSB_DBG_[*]
Linus Torvalds1da177e2005-04-16 15:20:36 -070051*/
Linus Torvalds1da177e2005-04-16 15:20:36 -070052#define __debug_variable hfc_debug
53#include "hisax_debug.h"
54static u_int debug;
55module_param(debug, uint, 0);
Adrian Bunk672c3fd2005-06-25 14:59:18 -070056static int hfc_debug;
Martin Bachemd6c59c12007-08-21 14:26:21 +020057
Linus Torvalds1da177e2005-04-16 15:20:36 -070058
Martin Bachem597a1072005-11-07 01:00:20 -080059/* private vendor specific data */
60typedef struct {
61 __u8 led_scheme; // led display scheme
62 signed short led_bits[8]; // array of 8 possible LED bitmask settings
63 char *vend_name; // device name
64} hfcsusb_vdata;
Linus Torvalds1da177e2005-04-16 15:20:36 -070065
Martin Bachemd6c59c12007-08-21 14:26:21 +020066/* VID/PID device list */
Martin Bachem597a1072005-11-07 01:00:20 -080067static struct usb_device_id hfcusb_idtab[] = {
68 {
Andrew Mortond6a1a642005-11-15 00:09:13 -080069 USB_DEVICE(0x0959, 0x2bd0),
Martin Bachem597a1072005-11-07 01:00:20 -080070 .driver_info = (unsigned long) &((hfcsusb_vdata)
71 {LED_OFF, {4, 0, 2, 1},
72 "ISDN USB TA (Cologne Chip HFC-S USB based)"}),
73 },
74 {
Andrew Mortond6a1a642005-11-15 00:09:13 -080075 USB_DEVICE(0x0675, 0x1688),
Martin Bachem597a1072005-11-07 01:00:20 -080076 .driver_info = (unsigned long) &((hfcsusb_vdata)
77 {LED_SCHEME1, {1, 2, 0, 0},
78 "DrayTek miniVigor 128 USB ISDN TA"}),
79 },
80 {
Andrew Mortond6a1a642005-11-15 00:09:13 -080081 USB_DEVICE(0x07b0, 0x0007),
Martin Bachem597a1072005-11-07 01:00:20 -080082 .driver_info = (unsigned long) &((hfcsusb_vdata)
83 {LED_SCHEME1, {0x80, -64, -32, -16},
84 "Billion tiny USB ISDN TA 128"}),
85 },
86 {
Andrew Mortond6a1a642005-11-15 00:09:13 -080087 USB_DEVICE(0x0742, 0x2008),
Martin Bachem597a1072005-11-07 01:00:20 -080088 .driver_info = (unsigned long) &((hfcsusb_vdata)
89 {LED_SCHEME1, {4, 0, 2, 1},
90 "Stollmann USB TA"}),
Martin Bachemd6c59c12007-08-21 14:26:21 +020091 },
Martin Bachem597a1072005-11-07 01:00:20 -080092 {
Andrew Mortond6a1a642005-11-15 00:09:13 -080093 USB_DEVICE(0x0742, 0x2009),
Martin Bachem597a1072005-11-07 01:00:20 -080094 .driver_info = (unsigned long) &((hfcsusb_vdata)
95 {LED_SCHEME1, {4, 0, 2, 1},
96 "Aceex USB ISDN TA"}),
Martin Bachemd6c59c12007-08-21 14:26:21 +020097 },
Martin Bachem597a1072005-11-07 01:00:20 -080098 {
Andrew Mortond6a1a642005-11-15 00:09:13 -080099 USB_DEVICE(0x0742, 0x200A),
Martin Bachem597a1072005-11-07 01:00:20 -0800100 .driver_info = (unsigned long) &((hfcsusb_vdata)
101 {LED_SCHEME1, {4, 0, 2, 1},
102 "OEM USB ISDN TA"}),
Martin Bachemd6c59c12007-08-21 14:26:21 +0200103 },
Martin Bachem597a1072005-11-07 01:00:20 -0800104 {
Andrew Mortond6a1a642005-11-15 00:09:13 -0800105 USB_DEVICE(0x08e3, 0x0301),
Martin Bachem597a1072005-11-07 01:00:20 -0800106 .driver_info = (unsigned long) &((hfcsusb_vdata)
107 {LED_SCHEME1, {2, 0, 1, 4},
108 "Olitec USB RNIS"}),
Martin Bachemd6c59c12007-08-21 14:26:21 +0200109 },
Martin Bachem597a1072005-11-07 01:00:20 -0800110 {
Andrew Mortond6a1a642005-11-15 00:09:13 -0800111 USB_DEVICE(0x07fa, 0x0846),
Martin Bachem597a1072005-11-07 01:00:20 -0800112 .driver_info = (unsigned long) &((hfcsusb_vdata)
113 {LED_SCHEME1, {0x80, -64, -32, -16},
114 "Bewan Modem RNIS USB"}),
Martin Bachemd6c59c12007-08-21 14:26:21 +0200115 },
Martin Bachem597a1072005-11-07 01:00:20 -0800116 {
Andrew Mortond6a1a642005-11-15 00:09:13 -0800117 USB_DEVICE(0x07fa, 0x0847),
Martin Bachem597a1072005-11-07 01:00:20 -0800118 .driver_info = (unsigned long) &((hfcsusb_vdata)
119 {LED_SCHEME1, {0x80, -64, -32, -16},
120 "Djinn Numeris USB"}),
Martin Bachemd6c59c12007-08-21 14:26:21 +0200121 },
Martin Bachem597a1072005-11-07 01:00:20 -0800122 {
Andrew Mortond6a1a642005-11-15 00:09:13 -0800123 USB_DEVICE(0x07b0, 0x0006),
Martin Bachem597a1072005-11-07 01:00:20 -0800124 .driver_info = (unsigned long) &((hfcsusb_vdata)
125 {LED_SCHEME1, {0x80, -64, -32, -16},
126 "Twister ISDN TA"}),
Martin Bachemd6c59c12007-08-21 14:26:21 +0200127 },
Martin Bachem7441dd12007-10-14 18:10:30 +0200128 {
129 USB_DEVICE(0x071d, 0x1005),
130 .driver_info = (unsigned long) &((hfcsusb_vdata)
131 {LED_SCHEME1, {0x02, 0, 0x01, 0x04},
132 "Eicon DIVA USB 4.0"}),
133 },
Andrew Mortond6a1a642005-11-15 00:09:13 -0800134 { }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135};
136
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137/* structure defining input+output fifos (interrupt/bulk mode) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138struct usb_fifo; /* forward definition */
139typedef struct iso_urb_struct {
140 struct urb *purb;
141 __u8 buffer[ISO_BUFFER_SIZE]; /* buffer incoming/outgoing data */
142 struct usb_fifo *owner_fifo; /* pointer to owner fifo */
143} iso_urb_struct;
144
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145struct hfcusb_data; /* forward definition */
Martin Bachemd6c59c12007-08-21 14:26:21 +0200146
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147typedef struct usb_fifo {
148 int fifonum; /* fifo index attached to this structure */
149 int active; /* fifo is currently active */
150 struct hfcusb_data *hfc; /* pointer to main structure */
151 int pipe; /* address of endpoint */
152 __u8 usb_packet_maxlen; /* maximum length for usb transfer */
153 unsigned int max_size; /* maximum size of receive/send packet */
154 __u8 intervall; /* interrupt interval */
155 struct sk_buff *skbuff; /* actual used buffer */
156 struct urb *urb; /* transfer structure for usb routines */
157 __u8 buffer[128]; /* buffer incoming/outgoing data */
158 int bit_line; /* how much bits are in the fifo? */
159
160 volatile __u8 usb_transfer_mode; /* switched between ISO and INT */
161 iso_urb_struct iso[2]; /* need two urbs to have one always for pending */
162 struct hisax_if *hif; /* hisax interface */
163 int delete_flg; /* only delete skbuff once */
164 int last_urblen; /* remember length of last packet */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165} usb_fifo;
166
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167/* structure holding all data for one device */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168typedef struct hfcusb_data {
169 /* HiSax Interface for loadable Layer1 drivers */
Martin Bachemd6c59c12007-08-21 14:26:21 +0200170 struct hisax_d_if d_if; /* see hisax_if.h */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 struct hisax_b_if b_if[2]; /* see hisax_if.h */
172 int protocol;
173
174 struct usb_device *dev; /* our device */
175 int if_used; /* used interface number */
176 int alt_used; /* used alternate config */
177 int ctrl_paksize; /* control pipe packet size */
Martin Bachemd6c59c12007-08-21 14:26:21 +0200178 int ctrl_in_pipe, /* handles for control pipe */
179 ctrl_out_pipe;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 int cfg_used; /* configuration index used */
181 int vend_idx; /* vendor found */
182 int b_mode[2]; /* B-channel mode */
183 int l1_activated; /* layer 1 activated */
Martin Bachemd6c59c12007-08-21 14:26:21 +0200184 int disc_flag; /* TRUE if device was disonnected to avoid some USB actions */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 int packet_size, iso_packet_size;
186
187 /* control pipe background handling */
188 ctrl_buft ctrl_buff[HFC_CTRL_BUFSIZE]; /* buffer holding queued data */
189 volatile int ctrl_in_idx, ctrl_out_idx, ctrl_cnt; /* input/output pointer + count */
190 struct urb *ctrl_urb; /* transfer structure for control channel */
191
192 struct usb_ctrlrequest ctrl_write; /* buffer for control write request */
193 struct usb_ctrlrequest ctrl_read; /* same for read request */
194
Martin Bachem7441dd12007-10-14 18:10:30 +0200195 __u8 old_led_state, led_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196
197 volatile __u8 threshold_mask; /* threshold actually reported */
198 volatile __u8 bch_enables; /* or mask for sctrl_r and sctrl register values */
199
200 usb_fifo fifos[HFCUSB_NUM_FIFOS]; /* structure holding all fifo data */
201
202 volatile __u8 l1_state; /* actual l1 state */
203 struct timer_list t3_timer; /* timer 3 for activation/deactivation */
204 struct timer_list t4_timer; /* timer 4 for activation/deactivation */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205} hfcusb_data;
206
207
208static void collect_rx_frame(usb_fifo * fifo, __u8 * data, int len,
209 int finish);
210
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211static inline const char *
212symbolic(struct hfcusb_symbolic_list list[], const int num)
213{
214 int i;
215 for (i = 0; list[i].name != NULL; i++)
216 if (list[i].num == num)
217 return (list[i].name);
Karsten Keilb05121b2006-03-06 15:42:41 -0800218 return "<unknown ERROR>";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219}
220
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221static void
222ctrl_start_transfer(hfcusb_data * hfc)
223{
224 if (hfc->ctrl_cnt) {
225 hfc->ctrl_urb->pipe = hfc->ctrl_out_pipe;
226 hfc->ctrl_urb->setup_packet = (u_char *) & hfc->ctrl_write;
227 hfc->ctrl_urb->transfer_buffer = NULL;
228 hfc->ctrl_urb->transfer_buffer_length = 0;
229 hfc->ctrl_write.wIndex =
Karsten Keilb05121b2006-03-06 15:42:41 -0800230 cpu_to_le16(hfc->ctrl_buff[hfc->ctrl_out_idx].hfc_reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 hfc->ctrl_write.wValue =
Karsten Keilb05121b2006-03-06 15:42:41 -0800232 cpu_to_le16(hfc->ctrl_buff[hfc->ctrl_out_idx].reg_val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233
234 usb_submit_urb(hfc->ctrl_urb, GFP_ATOMIC); /* start transfer */
235 }
236} /* ctrl_start_transfer */
237
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238static int
239queue_control_request(hfcusb_data * hfc, __u8 reg, __u8 val, int action)
240{
241 ctrl_buft *buf;
242
243 if (hfc->ctrl_cnt >= HFC_CTRL_BUFSIZE)
244 return (1); /* no space left */
245 buf = &hfc->ctrl_buff[hfc->ctrl_in_idx]; /* pointer to new index */
246 buf->hfc_reg = reg;
247 buf->reg_val = val;
248 buf->action = action;
249 if (++hfc->ctrl_in_idx >= HFC_CTRL_BUFSIZE)
250 hfc->ctrl_in_idx = 0; /* pointer wrap */
251 if (++hfc->ctrl_cnt == 1)
252 ctrl_start_transfer(hfc);
253 return (0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254}
255
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256static void
David Howells7d12e782006-10-05 14:55:46 +0100257ctrl_complete(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258{
259 hfcusb_data *hfc = (hfcusb_data *) urb->context;
260 ctrl_buft *buf;
261
262 urb->dev = hfc->dev;
263 if (hfc->ctrl_cnt) {
264 buf = &hfc->ctrl_buff[hfc->ctrl_out_idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 hfc->ctrl_cnt--; /* decrement actual count */
266 if (++hfc->ctrl_out_idx >= HFC_CTRL_BUFSIZE)
267 hfc->ctrl_out_idx = 0; /* pointer wrap */
268
269 ctrl_start_transfer(hfc); /* start next transfer */
270 }
Martin Bachem7441dd12007-10-14 18:10:30 +0200271}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273/* write led data to auxport & invert if necessary */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274static void
275write_led(hfcusb_data * hfc, __u8 led_state)
276{
277 if (led_state != hfc->old_led_state) {
278 hfc->old_led_state = led_state;
279 queue_control_request(hfc, HFCUSB_P_DATA, led_state, 1);
280 }
281}
282
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283static void
Martin Bachem7441dd12007-10-14 18:10:30 +0200284set_led_bit(hfcusb_data * hfc, signed short led_bits, int on)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285{
Martin Bachem7441dd12007-10-14 18:10:30 +0200286 if (on) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 if (led_bits < 0)
288 hfc->led_state &= ~abs(led_bits);
289 else
290 hfc->led_state |= led_bits;
Martin Bachem7441dd12007-10-14 18:10:30 +0200291 } else {
292 if (led_bits < 0)
293 hfc->led_state |= abs(led_bits);
294 else
295 hfc->led_state &= ~led_bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 }
297}
298
Martin Bachemd6c59c12007-08-21 14:26:21 +0200299/* handle LED requests */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300static void
301handle_led(hfcusb_data * hfc, int event)
302{
Martin Bachem597a1072005-11-07 01:00:20 -0800303 hfcsusb_vdata *driver_info =
304 (hfcsusb_vdata *) hfcusb_idtab[hfc->vend_idx].driver_info;
305
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 /* if no scheme -> no LED action */
Martin Bachem597a1072005-11-07 01:00:20 -0800307 if (driver_info->led_scheme == LED_OFF)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 return;
309
310 switch (event) {
311 case LED_POWER_ON:
Martin Bachemd6c59c12007-08-21 14:26:21 +0200312 set_led_bit(hfc, driver_info->led_bits[0], 1);
Martin Bachemd6c59c12007-08-21 14:26:21 +0200313 set_led_bit(hfc, driver_info->led_bits[1], 0);
Martin Bachemd6c59c12007-08-21 14:26:21 +0200314 set_led_bit(hfc, driver_info->led_bits[2], 0);
Martin Bachemd6c59c12007-08-21 14:26:21 +0200315 set_led_bit(hfc, driver_info->led_bits[3], 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 break;
Martin Bachem7441dd12007-10-14 18:10:30 +0200317 case LED_POWER_OFF:
318 set_led_bit(hfc, driver_info->led_bits[0], 0);
319 set_led_bit(hfc, driver_info->led_bits[1], 0);
320 set_led_bit(hfc, driver_info->led_bits[2], 0);
321 set_led_bit(hfc, driver_info->led_bits[3], 0);
322 break;
323 case LED_S0_ON:
324 set_led_bit(hfc, driver_info->led_bits[1], 1);
325 break;
326 case LED_S0_OFF:
327 set_led_bit(hfc, driver_info->led_bits[1], 0);
328 break;
329 case LED_B1_ON:
330 set_led_bit(hfc, driver_info->led_bits[2], 1);
331 break;
332 case LED_B1_OFF:
333 set_led_bit(hfc, driver_info->led_bits[2], 0);
334 break;
335 case LED_B2_ON:
Martin Bachemd6c59c12007-08-21 14:26:21 +0200336 set_led_bit(hfc, driver_info->led_bits[3], 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 break;
Martin Bachem7441dd12007-10-14 18:10:30 +0200338 case LED_B2_OFF:
339 set_led_bit(hfc, driver_info->led_bits[3], 0);
340 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 write_led(hfc, hfc->led_state);
343}
344
Martin Bachemd6c59c12007-08-21 14:26:21 +0200345/* ISDN l1 timer T3 expires */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346static void
347l1_timer_expire_t3(hfcusb_data * hfc)
348{
349 hfc->d_if.ifc.l1l2(&hfc->d_if.ifc, PH_DEACTIVATE | INDICATION,
350 NULL);
Martin Bachemd6c59c12007-08-21 14:26:21 +0200351
352 DBG(HFCUSB_DBG_STATES,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 "HFC-S USB: PH_DEACTIVATE | INDICATION sent (T3 expire)");
Martin Bachemd6c59c12007-08-21 14:26:21 +0200354
355 hfc->l1_activated = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 handle_led(hfc, LED_S0_OFF);
357 /* deactivate : */
358 queue_control_request(hfc, HFCUSB_STATES, 0x10, 1);
359 queue_control_request(hfc, HFCUSB_STATES, 3, 1);
360}
361
Martin Bachemd6c59c12007-08-21 14:26:21 +0200362/* ISDN l1 timer T4 expires */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363static void
364l1_timer_expire_t4(hfcusb_data * hfc)
365{
366 hfc->d_if.ifc.l1l2(&hfc->d_if.ifc, PH_DEACTIVATE | INDICATION,
367 NULL);
Martin Bachemd6c59c12007-08-21 14:26:21 +0200368
369 DBG(HFCUSB_DBG_STATES,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 "HFC-S USB: PH_DEACTIVATE | INDICATION sent (T4 expire)");
Martin Bachemd6c59c12007-08-21 14:26:21 +0200371
372 hfc->l1_activated = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 handle_led(hfc, LED_S0_OFF);
374}
375
Martin Bachemd6c59c12007-08-21 14:26:21 +0200376/* S0 state changed */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377static void
Martin Bachemd6c59c12007-08-21 14:26:21 +0200378s0_state_handler(hfcusb_data * hfc, __u8 state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379{
380 __u8 old_state;
381
382 old_state = hfc->l1_state;
383 if (state == old_state || state < 1 || state > 8)
384 return;
385
Martin Bachemd6c59c12007-08-21 14:26:21 +0200386 DBG(HFCUSB_DBG_STATES, "HFC-S USB: S0 statechange(%d -> %d)",
387 old_state, state);
388
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 if (state < 4 || state == 7 || state == 8) {
390 if (timer_pending(&hfc->t3_timer))
391 del_timer(&hfc->t3_timer);
Martin Bachemd6c59c12007-08-21 14:26:21 +0200392 DBG(HFCUSB_DBG_STATES, "HFC-S USB: T3 deactivated");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 }
394 if (state >= 7) {
395 if (timer_pending(&hfc->t4_timer))
396 del_timer(&hfc->t4_timer);
Martin Bachemd6c59c12007-08-21 14:26:21 +0200397 DBG(HFCUSB_DBG_STATES, "HFC-S USB: T4 deactivated");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 }
399
400 if (state == 7 && !hfc->l1_activated) {
401 hfc->d_if.ifc.l1l2(&hfc->d_if.ifc,
402 PH_ACTIVATE | INDICATION, NULL);
Martin Bachemd6c59c12007-08-21 14:26:21 +0200403 DBG(HFCUSB_DBG_STATES, "HFC-S USB: PH_ACTIVATE | INDICATION sent");
404 hfc->l1_activated = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 handle_led(hfc, LED_S0_ON);
406 } else if (state <= 3 /* && activated */ ) {
407 if (old_state == 7 || old_state == 8) {
Martin Bachemd6c59c12007-08-21 14:26:21 +0200408 DBG(HFCUSB_DBG_STATES, "HFC-S USB: T4 activated");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 if (!timer_pending(&hfc->t4_timer)) {
410 hfc->t4_timer.expires =
411 jiffies + (HFC_TIMER_T4 * HZ) / 1000;
412 add_timer(&hfc->t4_timer);
413 }
414 } else {
415 hfc->d_if.ifc.l1l2(&hfc->d_if.ifc,
416 PH_DEACTIVATE | INDICATION,
417 NULL);
Martin Bachemd6c59c12007-08-21 14:26:21 +0200418 DBG(HFCUSB_DBG_STATES,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 "HFC-S USB: PH_DEACTIVATE | INDICATION sent");
Martin Bachemd6c59c12007-08-21 14:26:21 +0200420 hfc->l1_activated = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 handle_led(hfc, LED_S0_OFF);
422 }
423 }
424 hfc->l1_state = state;
425}
426
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427static void
428fill_isoc_urb(struct urb *urb, struct usb_device *dev, unsigned int pipe,
429 void *buf, int num_packets, int packet_size, int interval,
430 usb_complete_t complete, void *context)
431{
432 int k;
433
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 urb->dev = dev;
435 urb->pipe = pipe;
436 urb->complete = complete;
437 urb->number_of_packets = num_packets;
438 urb->transfer_buffer_length = packet_size * num_packets;
439 urb->context = context;
440 urb->transfer_buffer = buf;
441 urb->transfer_flags = URB_ISO_ASAP;
442 urb->actual_length = 0;
443 urb->interval = interval;
444 for (k = 0; k < num_packets; k++) {
445 urb->iso_frame_desc[k].offset = packet_size * k;
446 urb->iso_frame_desc[k].length = packet_size;
447 urb->iso_frame_desc[k].actual_length = 0;
448 }
449}
450
451/* allocs urbs and start isoc transfer with two pending urbs to avoid
Martin Bachemd6c59c12007-08-21 14:26:21 +0200452 * gaps in the transfer chain
453 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454static int
455start_isoc_chain(usb_fifo * fifo, int num_packets_per_urb,
456 usb_complete_t complete, int packet_size)
457{
458 int i, k, errcode;
459
Martin Bachemd6c59c12007-08-21 14:26:21 +0200460 DBG(HFCUSB_DBG_INIT, "HFC-S USB: starting ISO-URBs for fifo:%d\n",
461 fifo->fifonum);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462
463 /* allocate Memory for Iso out Urbs */
464 for (i = 0; i < 2; i++) {
465 if (!(fifo->iso[i].purb)) {
466 fifo->iso[i].purb =
467 usb_alloc_urb(num_packets_per_urb, GFP_KERNEL);
468 if (!(fifo->iso[i].purb)) {
469 printk(KERN_INFO
470 "alloc urb for fifo %i failed!!!",
471 fifo->fifonum);
472 }
473 fifo->iso[i].owner_fifo = (struct usb_fifo *) fifo;
474
475 /* Init the first iso */
476 if (ISO_BUFFER_SIZE >=
477 (fifo->usb_packet_maxlen *
478 num_packets_per_urb)) {
479 fill_isoc_urb(fifo->iso[i].purb,
480 fifo->hfc->dev, fifo->pipe,
481 fifo->iso[i].buffer,
482 num_packets_per_urb,
483 fifo->usb_packet_maxlen,
484 fifo->intervall, complete,
485 &fifo->iso[i]);
486 memset(fifo->iso[i].buffer, 0,
487 sizeof(fifo->iso[i].buffer));
488 /* defining packet delimeters in fifo->buffer */
489 for (k = 0; k < num_packets_per_urb; k++) {
490 fifo->iso[i].purb->
491 iso_frame_desc[k].offset =
492 k * packet_size;
493 fifo->iso[i].purb->
494 iso_frame_desc[k].length =
495 packet_size;
496 }
497 } else {
498 printk(KERN_INFO
499 "HFC-S USB: ISO Buffer size to small!\n");
500 }
501 }
502 fifo->bit_line = BITLINE_INF;
503
504 errcode = usb_submit_urb(fifo->iso[i].purb, GFP_KERNEL);
505 fifo->active = (errcode >= 0) ? 1 : 0;
Martin Bachemd6c59c12007-08-21 14:26:21 +0200506 if (errcode < 0)
507 printk(KERN_INFO "HFC-S USB: usb_submit_urb URB nr:%d, error(%i): '%s'\n",
508 i, errcode, symbolic(urb_errlist, errcode));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 }
510 return (fifo->active);
511}
512
513/* stops running iso chain and frees their pending urbs */
514static void
515stop_isoc_chain(usb_fifo * fifo)
516{
517 int i;
518
519 for (i = 0; i < 2; i++) {
520 if (fifo->iso[i].purb) {
Martin Bachemd6c59c12007-08-21 14:26:21 +0200521 DBG(HFCUSB_DBG_INIT,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 "HFC-S USB: Stopping iso chain for fifo %i.%i",
523 fifo->fifonum, i);
Alan Sternae2d9902007-05-23 13:58:12 -0700524 usb_kill_urb(fifo->iso[i].purb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 usb_free_urb(fifo->iso[i].purb);
526 fifo->iso[i].purb = NULL;
527 }
528 }
Martin Bachemd6c59c12007-08-21 14:26:21 +0200529
Alan Sternae2d9902007-05-23 13:58:12 -0700530 usb_kill_urb(fifo->urb);
531 usb_free_urb(fifo->urb);
532 fifo->urb = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 fifo->active = 0;
534}
535
536/* defines how much ISO packets are handled in one URB */
537static int iso_packets[8] =
538 { ISOC_PACKETS_B, ISOC_PACKETS_B, ISOC_PACKETS_B, ISOC_PACKETS_B,
539 ISOC_PACKETS_D, ISOC_PACKETS_D, ISOC_PACKETS_D, ISOC_PACKETS_D
540};
541
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542static void
David Howells7d12e782006-10-05 14:55:46 +0100543tx_iso_complete(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544{
545 iso_urb_struct *context_iso_urb = (iso_urb_struct *) urb->context;
546 usb_fifo *fifo = context_iso_urb->owner_fifo;
547 hfcusb_data *hfc = fifo->hfc;
548 int k, tx_offset, num_isoc_packets, sink, len, current_len,
549 errcode;
550 int frame_complete, transp_mode, fifon, status;
551 __u8 threshbit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552
553 fifon = fifo->fifonum;
554 status = urb->status;
555
556 tx_offset = 0;
557
Martin Bachemd6c59c12007-08-21 14:26:21 +0200558 /* ISO transfer only partially completed,
559 look at individual frame status for details */
560 if (status == -EXDEV) {
561 DBG(HFCUSB_DBG_VERBOSE_USB, "HFC-S USB: tx_iso_complete with -EXDEV"
562 ", urb->status %d, fifonum %d\n",
563 status, fifon);
564
565 for (k = 0; k < iso_packets[fifon]; ++k) {
566 errcode = urb->iso_frame_desc[k].status;
567 if (errcode)
568 DBG(HFCUSB_DBG_VERBOSE_USB, "HFC-S USB: tx_iso_complete "
569 "packet %i, status: %i\n",
570 k, errcode);
571 }
572
573 // clear status, so go on with ISO transfers
574 status = 0;
575 }
576
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 if (fifo->active && !status) {
578 transp_mode = 0;
579 if (fifon < 4 && hfc->b_mode[fifon / 2] == L1_MODE_TRANS)
Martin Bachemd6c59c12007-08-21 14:26:21 +0200580 transp_mode = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581
582 /* is FifoFull-threshold set for our channel? */
Martin Bachemd6c59c12007-08-21 14:26:21 +0200583 threshbit = (hfc->threshold_mask & (1 << fifon));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 num_isoc_packets = iso_packets[fifon];
585
586 /* predict dataflow to avoid fifo overflow */
587 if (fifon >= HFCUSB_D_TX) {
588 sink = (threshbit) ? SINK_DMIN : SINK_DMAX;
589 } else {
590 sink = (threshbit) ? SINK_MIN : SINK_MAX;
591 }
592 fill_isoc_urb(urb, fifo->hfc->dev, fifo->pipe,
593 context_iso_urb->buffer, num_isoc_packets,
594 fifo->usb_packet_maxlen, fifo->intervall,
595 tx_iso_complete, urb->context);
596 memset(context_iso_urb->buffer, 0,
597 sizeof(context_iso_urb->buffer));
Martin Bachemd6c59c12007-08-21 14:26:21 +0200598 frame_complete = 0;
599
600 /* Generate next ISO Packets */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 for (k = 0; k < num_isoc_packets; ++k) {
602 if (fifo->skbuff) {
603 len = fifo->skbuff->len;
604 /* we lower data margin every msec */
605 fifo->bit_line -= sink;
606 current_len = (0 - fifo->bit_line) / 8;
607 /* maximum 15 byte for every ISO packet makes our life easier */
608 if (current_len > 14)
609 current_len = 14;
610 current_len =
611 (len <=
612 current_len) ? len : current_len;
613 /* how much bit do we put on the line? */
614 fifo->bit_line += current_len * 8;
615
616 context_iso_urb->buffer[tx_offset] = 0;
617 if (current_len == len) {
618 if (!transp_mode) {
619 /* here frame completion */
620 context_iso_urb->
621 buffer[tx_offset] = 1;
622 /* add 2 byte flags and 16bit CRC at end of ISDN frame */
623 fifo->bit_line += 32;
624 }
Martin Bachemd6c59c12007-08-21 14:26:21 +0200625 frame_complete = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 }
627
628 memcpy(context_iso_urb->buffer +
629 tx_offset + 1, fifo->skbuff->data,
630 current_len);
631 skb_pull(fifo->skbuff, current_len);
632
633 /* define packet delimeters within the URB buffer */
634 urb->iso_frame_desc[k].offset = tx_offset;
635 urb->iso_frame_desc[k].length =
636 current_len + 1;
637
638 tx_offset += (current_len + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 } else {
640 urb->iso_frame_desc[k].offset =
641 tx_offset++;
642
643 urb->iso_frame_desc[k].length = 1;
644 fifo->bit_line -= sink; /* we lower data margin every msec */
645
646 if (fifo->bit_line < BITLINE_INF) {
647 fifo->bit_line = BITLINE_INF;
648 }
649 }
650
651 if (frame_complete) {
Martin Bachemd6c59c12007-08-21 14:26:21 +0200652 fifo->delete_flg = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 fifo->hif->l1l2(fifo->hif,
654 PH_DATA | CONFIRM,
Alan Cox17a45062006-10-03 01:13:55 -0700655 (void *) (unsigned long) fifo->skbuff->
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 truesize);
657 if (fifo->skbuff && fifo->delete_flg) {
658 dev_kfree_skb_any(fifo->skbuff);
659 fifo->skbuff = NULL;
Martin Bachemd6c59c12007-08-21 14:26:21 +0200660 fifo->delete_flg = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 }
Martin Bachemd6c59c12007-08-21 14:26:21 +0200662 frame_complete = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 }
664 }
665 errcode = usb_submit_urb(urb, GFP_ATOMIC);
666 if (errcode < 0) {
667 printk(KERN_INFO
Martin Bachemd6c59c12007-08-21 14:26:21 +0200668 "HFC-S USB: error submitting ISO URB: %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 errcode);
670 }
671 } else {
672 if (status && !hfc->disc_flag) {
673 printk(KERN_INFO
Martin Bachemd6c59c12007-08-21 14:26:21 +0200674 "HFC-S USB: tx_iso_complete: error(%i): '%s', fifonum=%d\n",
675 status, symbolic(urb_errlist, status), fifon);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 }
677 }
Martin Bachemd6c59c12007-08-21 14:26:21 +0200678}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680static void
David Howells7d12e782006-10-05 14:55:46 +0100681rx_iso_complete(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682{
683 iso_urb_struct *context_iso_urb = (iso_urb_struct *) urb->context;
684 usb_fifo *fifo = context_iso_urb->owner_fifo;
685 hfcusb_data *hfc = fifo->hfc;
686 int k, len, errcode, offset, num_isoc_packets, fifon, maxlen,
687 status;
688 unsigned int iso_status;
689 __u8 *buf;
690 static __u8 eof[8];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691
692 fifon = fifo->fifonum;
693 status = urb->status;
694
695 if (urb->status == -EOVERFLOW) {
Martin Bachemd6c59c12007-08-21 14:26:21 +0200696 DBG(HFCUSB_DBG_VERBOSE_USB,
697 "HFC-USB: ignoring USB DATAOVERRUN fifo(%i)", fifon);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 status = 0;
699 }
Martin Bachemd6c59c12007-08-21 14:26:21 +0200700
701 /* ISO transfer only partially completed,
702 look at individual frame status for details */
703 if (status == -EXDEV) {
704 DBG(HFCUSB_DBG_VERBOSE_USB, "HFC-S USB: rx_iso_complete with -EXDEV "
705 "urb->status %d, fifonum %d\n",
706 status, fifon);
707 status = 0;
708 }
709
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 if (fifo->active && !status) {
711 num_isoc_packets = iso_packets[fifon];
712 maxlen = fifo->usb_packet_maxlen;
713 for (k = 0; k < num_isoc_packets; ++k) {
714 len = urb->iso_frame_desc[k].actual_length;
715 offset = urb->iso_frame_desc[k].offset;
716 buf = context_iso_urb->buffer + offset;
717 iso_status = urb->iso_frame_desc[k].status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718
Martin Bachemd6c59c12007-08-21 14:26:21 +0200719 if (iso_status && !hfc->disc_flag)
720 DBG(HFCUSB_DBG_VERBOSE_USB,
721 "HFC-S USB: rx_iso_complete "
722 "ISO packet %i, status: %i\n",
723 k, iso_status);
724
725 if (fifon == HFCUSB_D_RX) {
726 DBG(HFCUSB_DBG_VERBOSE_USB,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 "HFC-S USB: ISO-D-RX lst_urblen:%2d "
Martin Bachemd6c59c12007-08-21 14:26:21 +0200728 "act_urblen:%2d max-urblen:%2d EOF:0x%0x",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 fifo->last_urblen, len, maxlen,
730 eof[5]);
Martin Bachemd6c59c12007-08-21 14:26:21 +0200731
732 DBG_PACKET(HFCUSB_DBG_VERBOSE_USB, buf, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 }
Martin Bachemd6c59c12007-08-21 14:26:21 +0200734
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 if (fifo->last_urblen != maxlen) {
736 /* the threshold mask is in the 2nd status byte */
737 hfc->threshold_mask = buf[1];
738 /* care for L1 state only for D-Channel
739 to avoid overlapped iso completions */
Martin Bachemd6c59c12007-08-21 14:26:21 +0200740 if (fifon == HFCUSB_D_RX) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 /* the S0 state is in the upper half
742 of the 1st status byte */
Martin Bachemd6c59c12007-08-21 14:26:21 +0200743 s0_state_handler(hfc, buf[0] >> 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 }
745 eof[fifon] = buf[0] & 1;
746 if (len > 2)
747 collect_rx_frame(fifo, buf + 2,
748 len - 2,
Martin Bachemd6c59c12007-08-21 14:26:21 +0200749 (len < maxlen) ?
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 eof[fifon] : 0);
751 } else {
752 collect_rx_frame(fifo, buf, len,
753 (len <
754 maxlen) ? eof[fifon] :
755 0);
756 }
757 fifo->last_urblen = len;
758 }
759
760 fill_isoc_urb(urb, fifo->hfc->dev, fifo->pipe,
761 context_iso_urb->buffer, num_isoc_packets,
762 fifo->usb_packet_maxlen, fifo->intervall,
763 rx_iso_complete, urb->context);
764 errcode = usb_submit_urb(urb, GFP_ATOMIC);
765 if (errcode < 0) {
Martin Bachemd6c59c12007-08-21 14:26:21 +0200766 printk(KERN_ERR
767 "HFC-S USB: error submitting ISO URB: %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 errcode);
769 }
770 } else {
771 if (status && !hfc->disc_flag) {
Martin Bachemd6c59c12007-08-21 14:26:21 +0200772 printk(KERN_ERR
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 "HFC-S USB: rx_iso_complete : "
774 "urb->status %d, fifonum %d\n",
775 status, fifon);
776 }
777 }
Martin Bachemd6c59c12007-08-21 14:26:21 +0200778}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779
Martin Bachemd6c59c12007-08-21 14:26:21 +0200780/* collect rx data from INT- and ISO-URBs */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781static void
782collect_rx_frame(usb_fifo * fifo, __u8 * data, int len, int finish)
783{
784 hfcusb_data *hfc = fifo->hfc;
785 int transp_mode, fifon;
Martin Bachemd6c59c12007-08-21 14:26:21 +0200786
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 fifon = fifo->fifonum;
788 transp_mode = 0;
789 if (fifon < 4 && hfc->b_mode[fifon / 2] == L1_MODE_TRANS)
Martin Bachemd6c59c12007-08-21 14:26:21 +0200790 transp_mode = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791
792 if (!fifo->skbuff) {
793 fifo->skbuff = dev_alloc_skb(fifo->max_size + 3);
794 if (!fifo->skbuff) {
Martin Bachemd6c59c12007-08-21 14:26:21 +0200795 printk(KERN_ERR
796 "HFC-S USB: cannot allocate buffer for fifo(%d)\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 fifon);
798 return;
799 }
800 }
801 if (len) {
802 if (fifo->skbuff->len + len < fifo->max_size) {
803 memcpy(skb_put(fifo->skbuff, len), data, len);
804 } else {
Martin Bachemd6c59c12007-08-21 14:26:21 +0200805 DBG(HFCUSB_DBG_FIFO_ERR,
806 "HCF-USB: got frame exceeded fifo->max_size(%d) fifo(%d)",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 fifo->max_size, fifon);
Martin Bachemd6c59c12007-08-21 14:26:21 +0200808 DBG_SKB(HFCUSB_DBG_VERBOSE_USB, fifo->skbuff);
809 skb_trim(fifo->skbuff, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 }
811 }
812 if (transp_mode && fifo->skbuff->len >= 128) {
813 fifo->hif->l1l2(fifo->hif, PH_DATA | INDICATION,
814 fifo->skbuff);
815 fifo->skbuff = NULL;
816 return;
817 }
818 /* we have a complete hdlc packet */
819 if (finish) {
Roel Kluin286e6332009-11-04 08:31:59 -0800820 if (fifo->skbuff->len > 3 &&
821 !fifo->skbuff->data[fifo->skbuff->len - 1]) {
Martin Bachemd6c59c12007-08-21 14:26:21 +0200822
823 if (fifon == HFCUSB_D_RX) {
824 DBG(HFCUSB_DBG_DCHANNEL,
825 "HFC-S USB: D-RX len(%d)", fifo->skbuff->len);
826 DBG_SKB(HFCUSB_DBG_DCHANNEL, fifo->skbuff);
827 }
828
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 /* remove CRC & status */
830 skb_trim(fifo->skbuff, fifo->skbuff->len - 3);
831 if (fifon == HFCUSB_PCM_RX) {
832 fifo->hif->l1l2(fifo->hif,
833 PH_DATA_E | INDICATION,
834 fifo->skbuff);
835 } else
836 fifo->hif->l1l2(fifo->hif,
837 PH_DATA | INDICATION,
838 fifo->skbuff);
839 fifo->skbuff = NULL; /* buffer was freed from upper layer */
840 } else {
Martin Bachemd6c59c12007-08-21 14:26:21 +0200841 DBG(HFCUSB_DBG_FIFO_ERR,
842 "HFC-S USB: ERROR frame len(%d) fifo(%d)",
843 fifo->skbuff->len, fifon);
844 DBG_SKB(HFCUSB_DBG_VERBOSE_USB, fifo->skbuff);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845 skb_trim(fifo->skbuff, 0);
846 }
847 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848}
849
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850static void
Martin Bachemd6c59c12007-08-21 14:26:21 +0200851rx_int_complete(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852{
853 int len;
854 int status;
855 __u8 *buf, maxlen, fifon;
856 usb_fifo *fifo = (usb_fifo *) urb->context;
857 hfcusb_data *hfc = fifo->hfc;
858 static __u8 eof[8];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859
860 urb->dev = hfc->dev; /* security init */
861
862 fifon = fifo->fifonum;
863 if ((!fifo->active) || (urb->status)) {
Martin Bachemd6c59c12007-08-21 14:26:21 +0200864 DBG(HFCUSB_DBG_INIT, "HFC-S USB: RX-Fifo %i is going down (%i)",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865 fifon, urb->status);
Martin Bachemd6c59c12007-08-21 14:26:21 +0200866
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867 fifo->urb->interval = 0; /* cancel automatic rescheduling */
868 if (fifo->skbuff) {
869 dev_kfree_skb_any(fifo->skbuff);
870 fifo->skbuff = NULL;
871 }
872 return;
873 }
874 len = urb->actual_length;
875 buf = fifo->buffer;
876 maxlen = fifo->usb_packet_maxlen;
877
Martin Bachemd6c59c12007-08-21 14:26:21 +0200878 if (fifon == HFCUSB_D_RX) {
879 DBG(HFCUSB_DBG_VERBOSE_USB,
880 "HFC-S USB: INT-D-RX lst_urblen:%2d "
881 "act_urblen:%2d max-urblen:%2d EOF:0x%0x",
882 fifo->last_urblen, len, maxlen,
883 eof[5]);
884 DBG_PACKET(HFCUSB_DBG_VERBOSE_USB, buf, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886
887 if (fifo->last_urblen != fifo->usb_packet_maxlen) {
888 /* the threshold mask is in the 2nd status byte */
889 hfc->threshold_mask = buf[1];
890 /* the S0 state is in the upper half of the 1st status byte */
Martin Bachemd6c59c12007-08-21 14:26:21 +0200891 s0_state_handler(hfc, buf[0] >> 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892 eof[fifon] = buf[0] & 1;
893 /* if we have more than the 2 status bytes -> collect data */
894 if (len > 2)
895 collect_rx_frame(fifo, buf + 2,
896 urb->actual_length - 2,
897 (len < maxlen) ? eof[fifon] : 0);
898 } else {
899 collect_rx_frame(fifo, buf, urb->actual_length,
900 (len < maxlen) ? eof[fifon] : 0);
901 }
902 fifo->last_urblen = urb->actual_length;
903 status = usb_submit_urb(urb, GFP_ATOMIC);
904 if (status) {
905 printk(KERN_INFO
Martin Bachemd6c59c12007-08-21 14:26:21 +0200906 "HFC-S USB: %s error resubmitting URB fifo(%d)\n",
Harvey Harrison156f1ed2008-04-28 02:14:40 -0700907 __func__, fifon);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908 }
Martin Bachemd6c59c12007-08-21 14:26:21 +0200909}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910
Martin Bachemd6c59c12007-08-21 14:26:21 +0200911/* start initial INT-URB for certain fifo */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912static void
913start_int_fifo(usb_fifo * fifo)
914{
915 int errcode;
916
Martin Bachemd6c59c12007-08-21 14:26:21 +0200917 DBG(HFCUSB_DBG_INIT, "HFC-S USB: starting RX INT-URB for fifo:%d\n",
918 fifo->fifonum);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919
920 if (!fifo->urb) {
921 fifo->urb = usb_alloc_urb(0, GFP_KERNEL);
922 if (!fifo->urb)
923 return;
924 }
925 usb_fill_int_urb(fifo->urb, fifo->hfc->dev, fifo->pipe,
926 fifo->buffer, fifo->usb_packet_maxlen,
Martin Bachemd6c59c12007-08-21 14:26:21 +0200927 rx_int_complete, fifo, fifo->intervall);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 fifo->active = 1; /* must be marked active */
929 errcode = usb_submit_urb(fifo->urb, GFP_KERNEL);
930 if (errcode) {
Martin Bachemd6c59c12007-08-21 14:26:21 +0200931 printk(KERN_ERR
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 "HFC-S USB: submit URB error(start_int_info): status:%i\n",
933 errcode);
934 fifo->active = 0;
935 fifo->skbuff = NULL;
936 }
Martin Bachemd6c59c12007-08-21 14:26:21 +0200937}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939static void
Martin Bachemd6c59c12007-08-21 14:26:21 +0200940setup_bchannel(hfcusb_data * hfc, int channel, int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941{
942 __u8 val, idx_table[2] = { 0, 2 };
943
944 if (hfc->disc_flag) {
945 return;
946 }
Martin Bachemd6c59c12007-08-21 14:26:21 +0200947 DBG(HFCUSB_DBG_STATES, "HFC-S USB: setting channel %d to mode %d",
948 channel, mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 hfc->b_mode[channel] = mode;
950
951 /* setup CON_HDLC */
952 val = 0;
953 if (mode != L1_MODE_NULL)
954 val = 8; /* enable fifo? */
955 if (mode == L1_MODE_TRANS)
956 val |= 2; /* set transparent bit */
957
958 /* set FIFO to transmit register */
959 queue_control_request(hfc, HFCUSB_FIFO, idx_table[channel], 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 /* set FIFO to receive register */
964 queue_control_request(hfc, HFCUSB_FIFO, idx_table[channel] + 1, 1);
965 queue_control_request(hfc, HFCUSB_CON_HDLC, val, 1);
966 /* reset fifo */
967 queue_control_request(hfc, HFCUSB_INC_RES_F, 2, 1);
968
969 val = 0x40;
970 if (hfc->b_mode[0])
971 val |= 1;
972 if (hfc->b_mode[1])
973 val |= 2;
974 queue_control_request(hfc, HFCUSB_SCTRL, val, 1);
975
976 val = 0;
977 if (hfc->b_mode[0])
978 val |= 1;
979 if (hfc->b_mode[1])
980 val |= 2;
981 queue_control_request(hfc, HFCUSB_SCTRL_R, val, 1);
982
983 if (mode == L1_MODE_NULL) {
984 if (channel)
985 handle_led(hfc, LED_B2_OFF);
986 else
987 handle_led(hfc, LED_B1_OFF);
988 } else {
989 if (channel)
990 handle_led(hfc, LED_B2_ON);
991 else
992 handle_led(hfc, LED_B1_ON);
993 }
994}
995
Adrian Bunk672c3fd2005-06-25 14:59:18 -0700996static void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997hfc_usb_l2l1(struct hisax_if *my_hisax_if, int pr, void *arg)
998{
999 usb_fifo *fifo = my_hisax_if->priv;
1000 hfcusb_data *hfc = fifo->hfc;
1001
1002 switch (pr) {
1003 case PH_ACTIVATE | REQUEST:
1004 if (fifo->fifonum == HFCUSB_D_TX) {
Martin Bachemd6c59c12007-08-21 14:26:21 +02001005 DBG(HFCUSB_DBG_STATES,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006 "HFC_USB: hfc_usb_d_l2l1 D-chan: PH_ACTIVATE | REQUEST");
Martin Bachemd6c59c12007-08-21 14:26:21 +02001007
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 if (hfc->l1_state != 3
1009 && hfc->l1_state != 7) {
1010 hfc->d_if.ifc.l1l2(&hfc->d_if.ifc,
1011 PH_DEACTIVATE |
1012 INDICATION,
1013 NULL);
Martin Bachemd6c59c12007-08-21 14:26:21 +02001014 DBG(HFCUSB_DBG_STATES,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015 "HFC-S USB: PH_DEACTIVATE | INDICATION sent (not state 3 or 7)");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016 } else {
1017 if (hfc->l1_state == 7) { /* l1 already active */
1018 hfc->d_if.ifc.l1l2(&hfc->
1019 d_if.
1020 ifc,
1021 PH_ACTIVATE
1022 |
1023 INDICATION,
1024 NULL);
Martin Bachemd6c59c12007-08-21 14:26:21 +02001025 DBG(HFCUSB_DBG_STATES,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026 "HFC-S USB: PH_ACTIVATE | INDICATION sent again ;)");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027 } else {
1028 /* force sending sending INFO1 */
1029 queue_control_request(hfc,
1030 HFCUSB_STATES,
1031 0x14,
1032 1);
1033 mdelay(1);
1034 /* start l1 activation */
1035 queue_control_request(hfc,
1036 HFCUSB_STATES,
1037 0x04,
1038 1);
1039 if (!timer_pending
1040 (&hfc->t3_timer)) {
1041 hfc->t3_timer.
1042 expires =
1043 jiffies +
1044 (HFC_TIMER_T3 *
1045 HZ) / 1000;
1046 add_timer(&hfc->
1047 t3_timer);
1048 }
1049 }
1050 }
1051 } else {
Martin Bachemd6c59c12007-08-21 14:26:21 +02001052 DBG(HFCUSB_DBG_STATES,
1053 "HFC_USB: hfc_usb_d_l2l1 B-chan: PH_ACTIVATE | REQUEST");
1054 setup_bchannel(hfc,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055 (fifo->fifonum ==
1056 HFCUSB_B1_TX) ? 0 : 1,
Alan Cox17a45062006-10-03 01:13:55 -07001057 (long) arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058 fifo->hif->l1l2(fifo->hif,
1059 PH_ACTIVATE | INDICATION,
1060 NULL);
1061 }
1062 break;
1063 case PH_DEACTIVATE | REQUEST:
1064 if (fifo->fifonum == HFCUSB_D_TX) {
Martin Bachemd6c59c12007-08-21 14:26:21 +02001065 DBG(HFCUSB_DBG_STATES,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066 "HFC_USB: hfc_usb_d_l2l1 D-chan: PH_DEACTIVATE | REQUEST");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067 } else {
Martin Bachemd6c59c12007-08-21 14:26:21 +02001068 DBG(HFCUSB_DBG_STATES,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069 "HFC_USB: hfc_usb_d_l2l1 Bx-chan: PH_DEACTIVATE | REQUEST");
Martin Bachemd6c59c12007-08-21 14:26:21 +02001070 setup_bchannel(hfc,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071 (fifo->fifonum ==
1072 HFCUSB_B1_TX) ? 0 : 1,
1073 (int) L1_MODE_NULL);
1074 fifo->hif->l1l2(fifo->hif,
1075 PH_DEACTIVATE | INDICATION,
1076 NULL);
1077 }
1078 break;
1079 case PH_DATA | REQUEST:
1080 if (fifo->skbuff && fifo->delete_flg) {
1081 dev_kfree_skb_any(fifo->skbuff);
1082 fifo->skbuff = NULL;
Martin Bachemd6c59c12007-08-21 14:26:21 +02001083 fifo->delete_flg = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084 }
1085 fifo->skbuff = arg; /* we have a new buffer */
1086 break;
1087 default:
Martin Bachemd6c59c12007-08-21 14:26:21 +02001088 DBG(HFCUSB_DBG_STATES,
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02001089 "HFC_USB: hfc_usb_d_l2l1: unknown state : %#x", pr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090 break;
1091 }
1092}
1093
Martin Bachemd6c59c12007-08-21 14:26:21 +02001094/* initial init HFC-S USB chip registers, HiSax interface, USB URBs */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095static int
Martin Bachemd6c59c12007-08-21 14:26:21 +02001096hfc_usb_init(hfcusb_data * hfc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097{
1098 usb_fifo *fifo;
1099 int i, err;
1100 u_char b;
1101 struct hisax_b_if *p_b_if[2];
1102
1103 /* check the chip id */
1104 if (read_usb(hfc, HFCUSB_CHIP_ID, &b) != 1) {
1105 printk(KERN_INFO "HFC-USB: cannot read chip id\n");
1106 return (1);
1107 }
1108 if (b != HFCUSB_CHIPID) {
1109 printk(KERN_INFO "HFC-S USB: Invalid chip id 0x%02x\n", b);
1110 return (1);
1111 }
1112
1113 /* first set the needed config, interface and alternate */
1114 err = usb_set_interface(hfc->dev, hfc->if_used, hfc->alt_used);
1115
1116 /* do Chip reset */
1117 write_usb(hfc, HFCUSB_CIRM, 8);
1118 /* aux = output, reset off */
1119 write_usb(hfc, HFCUSB_CIRM, 0x10);
1120
Martin Bachemd6c59c12007-08-21 14:26:21 +02001121 /* set USB_SIZE to match wMaxPacketSize for INT or BULK transfers */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122 write_usb(hfc, HFCUSB_USB_SIZE,
1123 (hfc->packet_size / 8) | ((hfc->packet_size / 8) << 4));
1124
Martin Bachemd6c59c12007-08-21 14:26:21 +02001125 /* set USB_SIZE_I to match wMaxPacketSize for ISO transfers */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126 write_usb(hfc, HFCUSB_USB_SIZE_I, hfc->iso_packet_size);
1127
1128 /* enable PCM/GCI master mode */
1129 write_usb(hfc, HFCUSB_MST_MODE1, 0); /* set default values */
1130 write_usb(hfc, HFCUSB_MST_MODE0, 1); /* enable master mode */
1131
1132 /* init the fifos */
1133 write_usb(hfc, HFCUSB_F_THRES,
1134 (HFCUSB_TX_THRESHOLD /
1135 8) | ((HFCUSB_RX_THRESHOLD / 8) << 4));
1136
1137 fifo = hfc->fifos;
1138 for (i = 0; i < HFCUSB_NUM_FIFOS; i++) {
1139 write_usb(hfc, HFCUSB_FIFO, i); /* select the desired fifo */
1140 fifo[i].skbuff = NULL; /* init buffer pointer */
1141 fifo[i].max_size =
1142 (i <= HFCUSB_B2_RX) ? MAX_BCH_SIZE : MAX_DFRAME_LEN;
1143 fifo[i].last_urblen = 0;
1144 /* set 2 bit for D- & E-channel */
1145 write_usb(hfc, HFCUSB_HDLC_PAR,
1146 ((i <= HFCUSB_B2_RX) ? 0 : 2));
1147 /* rx hdlc, enable IFF for D-channel */
1148 write_usb(hfc, HFCUSB_CON_HDLC,
1149 ((i == HFCUSB_D_TX) ? 0x09 : 0x08));
1150 write_usb(hfc, HFCUSB_INC_RES_F, 2); /* reset the fifo */
1151 }
1152
1153 write_usb(hfc, HFCUSB_CLKDEL, 0x0f); /* clock delay value */
1154 write_usb(hfc, HFCUSB_STATES, 3 | 0x10); /* set deactivated mode */
1155 write_usb(hfc, HFCUSB_STATES, 3); /* enable state machine */
1156
1157 write_usb(hfc, HFCUSB_SCTRL_R, 0); /* disable both B receivers */
1158 write_usb(hfc, HFCUSB_SCTRL, 0x40); /* disable B transmitters + capacitive mode */
1159
1160 /* set both B-channel to not connected */
1161 hfc->b_mode[0] = L1_MODE_NULL;
1162 hfc->b_mode[1] = L1_MODE_NULL;
1163
Martin Bachemd6c59c12007-08-21 14:26:21 +02001164 hfc->l1_activated = 0;
1165 hfc->disc_flag = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166 hfc->led_state = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167 hfc->old_led_state = 0;
1168
1169 /* init the t3 timer */
1170 init_timer(&hfc->t3_timer);
1171 hfc->t3_timer.data = (long) hfc;
1172 hfc->t3_timer.function = (void *) l1_timer_expire_t3;
1173
1174 /* init the t4 timer */
1175 init_timer(&hfc->t4_timer);
1176 hfc->t4_timer.data = (long) hfc;
1177 hfc->t4_timer.function = (void *) l1_timer_expire_t4;
1178
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179 /* init the background machinery for control requests */
1180 hfc->ctrl_read.bRequestType = 0xc0;
1181 hfc->ctrl_read.bRequest = 1;
Karsten Keilb05121b2006-03-06 15:42:41 -08001182 hfc->ctrl_read.wLength = cpu_to_le16(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183 hfc->ctrl_write.bRequestType = 0x40;
1184 hfc->ctrl_write.bRequest = 0;
1185 hfc->ctrl_write.wLength = 0;
1186 usb_fill_control_urb(hfc->ctrl_urb,
1187 hfc->dev,
1188 hfc->ctrl_out_pipe,
1189 (u_char *) & hfc->ctrl_write,
1190 NULL, 0, ctrl_complete, hfc);
1191 /* Init All Fifos */
1192 for (i = 0; i < HFCUSB_NUM_FIFOS; i++) {
1193 hfc->fifos[i].iso[0].purb = NULL;
1194 hfc->fifos[i].iso[1].purb = NULL;
1195 hfc->fifos[i].active = 0;
1196 }
1197 /* register Modul to upper Hisax Layers */
1198 hfc->d_if.owner = THIS_MODULE;
1199 hfc->d_if.ifc.priv = &hfc->fifos[HFCUSB_D_TX];
1200 hfc->d_if.ifc.l2l1 = hfc_usb_l2l1;
1201 for (i = 0; i < 2; i++) {
1202 hfc->b_if[i].ifc.priv = &hfc->fifos[HFCUSB_B1_TX + i * 2];
1203 hfc->b_if[i].ifc.l2l1 = hfc_usb_l2l1;
1204 p_b_if[i] = &hfc->b_if[i];
1205 }
1206 /* default Prot: EURO ISDN, should be a module_param */
1207 hfc->protocol = 2;
Alan Sternae2d9902007-05-23 13:58:12 -07001208 i = hisax_register(&hfc->d_if, p_b_if, "hfc_usb", hfc->protocol);
1209 if (i) {
1210 printk(KERN_INFO "HFC-S USB: hisax_register -> %d\n", i);
1211 return i;
1212 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213
1214#ifdef CONFIG_HISAX_DEBUG
1215 hfc_debug = debug;
1216#endif
1217
1218 for (i = 0; i < 4; i++)
1219 hfc->fifos[i].hif = &p_b_if[i / 2]->ifc;
1220 for (i = 4; i < 8; i++)
1221 hfc->fifos[i].hif = &hfc->d_if.ifc;
1222
1223 /* 3 (+1) INT IN + 3 ISO OUT */
1224 if (hfc->cfg_used == CNF_3INT3ISO || hfc->cfg_used == CNF_4INT3ISO) {
1225 start_int_fifo(hfc->fifos + HFCUSB_D_RX);
1226 if (hfc->fifos[HFCUSB_PCM_RX].pipe)
1227 start_int_fifo(hfc->fifos + HFCUSB_PCM_RX);
1228 start_int_fifo(hfc->fifos + HFCUSB_B1_RX);
1229 start_int_fifo(hfc->fifos + HFCUSB_B2_RX);
1230 }
1231 /* 3 (+1) ISO IN + 3 ISO OUT */
1232 if (hfc->cfg_used == CNF_3ISO3ISO || hfc->cfg_used == CNF_4ISO3ISO) {
1233 start_isoc_chain(hfc->fifos + HFCUSB_D_RX, ISOC_PACKETS_D,
1234 rx_iso_complete, 16);
1235 if (hfc->fifos[HFCUSB_PCM_RX].pipe)
1236 start_isoc_chain(hfc->fifos + HFCUSB_PCM_RX,
1237 ISOC_PACKETS_D, rx_iso_complete,
1238 16);
1239 start_isoc_chain(hfc->fifos + HFCUSB_B1_RX, ISOC_PACKETS_B,
1240 rx_iso_complete, 16);
1241 start_isoc_chain(hfc->fifos + HFCUSB_B2_RX, ISOC_PACKETS_B,
1242 rx_iso_complete, 16);
1243 }
1244
1245 start_isoc_chain(hfc->fifos + HFCUSB_D_TX, ISOC_PACKETS_D,
1246 tx_iso_complete, 1);
1247 start_isoc_chain(hfc->fifos + HFCUSB_B1_TX, ISOC_PACKETS_B,
1248 tx_iso_complete, 1);
1249 start_isoc_chain(hfc->fifos + HFCUSB_B2_TX, ISOC_PACKETS_B,
1250 tx_iso_complete, 1);
1251
1252 handle_led(hfc, LED_POWER_ON);
1253
1254 return (0);
Martin Bachemd6c59c12007-08-21 14:26:21 +02001255}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256
Martin Bachemd6c59c12007-08-21 14:26:21 +02001257/* initial callback for each plugged USB device */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258static int
1259hfc_usb_probe(struct usb_interface *intf, const struct usb_device_id *id)
1260{
1261 struct usb_device *dev = interface_to_usbdev(intf);
1262 hfcusb_data *context;
1263 struct usb_host_interface *iface = intf->cur_altsetting;
1264 struct usb_host_interface *iface_used = NULL;
1265 struct usb_host_endpoint *ep;
1266 int ifnum = iface->desc.bInterfaceNumber;
1267 int i, idx, alt_idx, probe_alt_setting, vend_idx, cfg_used, *vcf,
1268 attr, cfg_found, cidx, ep_addr;
1269 int cmptbl[16], small_match, iso_packet_size, packet_size,
1270 alt_used = 0;
Martin Bachem597a1072005-11-07 01:00:20 -08001271 hfcsusb_vdata *driver_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272
1273 vend_idx = 0xffff;
Martin Bachem597a1072005-11-07 01:00:20 -08001274 for (i = 0; hfcusb_idtab[i].idVendor; i++) {
Karsten Keilb05121b2006-03-06 15:42:41 -08001275 if ((le16_to_cpu(dev->descriptor.idVendor) == hfcusb_idtab[i].idVendor)
1276 && (le16_to_cpu(dev->descriptor.idProduct) == hfcusb_idtab[i].idProduct)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277 vend_idx = i;
Martin Bachem597a1072005-11-07 01:00:20 -08001278 continue;
1279 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280 }
Martin Bachem597a1072005-11-07 01:00:20 -08001281
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282 printk(KERN_INFO
1283 "HFC-S USB: probing interface(%d) actalt(%d) minor(%d)\n",
1284 ifnum, iface->desc.bAlternateSetting, intf->minor);
1285
1286 if (vend_idx != 0xffff) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287 /* if vendor and product ID is OK, start probing alternate settings */
1288 alt_idx = 0;
1289 small_match = 0xffff;
1290
1291 /* default settings */
1292 iso_packet_size = 16;
1293 packet_size = 64;
1294
1295 while (alt_idx < intf->num_altsetting) {
1296 iface = intf->altsetting + alt_idx;
1297 probe_alt_setting = iface->desc.bAlternateSetting;
1298 cfg_used = 0;
1299
1300 /* check for config EOL element */
1301 while (validconf[cfg_used][0]) {
Martin Bachemd6c59c12007-08-21 14:26:21 +02001302 cfg_found = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303 vcf = validconf[cfg_used];
1304 /* first endpoint descriptor */
1305 ep = iface->endpoint;
Martin Bachemd6c59c12007-08-21 14:26:21 +02001306
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307 memcpy(cmptbl, vcf, 16 * sizeof(int));
1308
1309 /* check for all endpoints in this alternate setting */
1310 for (i = 0; i < iface->desc.bNumEndpoints;
1311 i++) {
1312 ep_addr =
1313 ep->desc.bEndpointAddress;
1314 /* get endpoint base */
1315 idx = ((ep_addr & 0x7f) - 1) * 2;
1316 if (ep_addr & 0x80)
1317 idx++;
1318 attr = ep->desc.bmAttributes;
1319 if (cmptbl[idx] == EP_NUL) {
Martin Bachemd6c59c12007-08-21 14:26:21 +02001320 cfg_found = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321 }
1322 if (attr == USB_ENDPOINT_XFER_INT
1323 && cmptbl[idx] == EP_INT)
1324 cmptbl[idx] = EP_NUL;
1325 if (attr == USB_ENDPOINT_XFER_BULK
1326 && cmptbl[idx] == EP_BLK)
1327 cmptbl[idx] = EP_NUL;
1328 if (attr == USB_ENDPOINT_XFER_ISOC
1329 && cmptbl[idx] == EP_ISO)
1330 cmptbl[idx] = EP_NUL;
1331
1332 /* check if all INT endpoints match minimum interval */
Martin Bachemd6c59c12007-08-21 14:26:21 +02001333 if ((attr == USB_ENDPOINT_XFER_INT)
1334 && (ep->desc.bInterval < vcf[17])) {
1335 cfg_found = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336 }
1337 ep++;
1338 }
1339 for (i = 0; i < 16; i++) {
1340 /* all entries must be EP_NOP or EP_NUL for a valid config */
1341 if (cmptbl[i] != EP_NOP
1342 && cmptbl[i] != EP_NUL)
Martin Bachemd6c59c12007-08-21 14:26:21 +02001343 cfg_found = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344 }
1345 if (cfg_found) {
1346 if (cfg_used < small_match) {
1347 small_match = cfg_used;
1348 alt_used =
1349 probe_alt_setting;
1350 iface_used = iface;
1351 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352 }
1353 cfg_used++;
1354 }
1355 alt_idx++;
Martin Bachemd6c59c12007-08-21 14:26:21 +02001356 } /* (alt_idx < intf->num_altsetting) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357
1358 /* found a valid USB Ta Endpint config */
1359 if (small_match != 0xffff) {
1360 iface = iface_used;
Martin Bachemd6c59c12007-08-21 14:26:21 +02001361 if (!(context = kzalloc(sizeof(hfcusb_data), GFP_KERNEL)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362 return (-ENOMEM); /* got no mem */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363
1364 ep = iface->endpoint;
1365 vcf = validconf[small_match];
1366
1367 for (i = 0; i < iface->desc.bNumEndpoints; i++) {
1368 ep_addr = ep->desc.bEndpointAddress;
1369 /* get endpoint base */
1370 idx = ((ep_addr & 0x7f) - 1) * 2;
1371 if (ep_addr & 0x80)
1372 idx++;
1373 cidx = idx & 7;
1374 attr = ep->desc.bmAttributes;
1375
1376 /* init Endpoints */
1377 if (vcf[idx] != EP_NOP
1378 && vcf[idx] != EP_NUL) {
1379 switch (attr) {
1380 case USB_ENDPOINT_XFER_INT:
1381 context->
1382 fifos[cidx].
1383 pipe =
1384 usb_rcvintpipe
1385 (dev,
1386 ep->desc.
1387 bEndpointAddress);
1388 context->
1389 fifos[cidx].
1390 usb_transfer_mode
1391 = USB_INT;
1392 packet_size =
Karsten Keilb05121b2006-03-06 15:42:41 -08001393 le16_to_cpu(ep->desc.wMaxPacketSize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394 break;
1395 case USB_ENDPOINT_XFER_BULK:
1396 if (ep_addr & 0x80)
1397 context->
1398 fifos
1399 [cidx].
1400 pipe =
1401 usb_rcvbulkpipe
1402 (dev,
1403 ep->
1404 desc.
1405 bEndpointAddress);
1406 else
1407 context->
1408 fifos
1409 [cidx].
1410 pipe =
1411 usb_sndbulkpipe
1412 (dev,
1413 ep->
1414 desc.
1415 bEndpointAddress);
1416 context->
1417 fifos[cidx].
1418 usb_transfer_mode
1419 = USB_BULK;
1420 packet_size =
Karsten Keilb05121b2006-03-06 15:42:41 -08001421 le16_to_cpu(ep->desc.wMaxPacketSize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422 break;
1423 case USB_ENDPOINT_XFER_ISOC:
1424 if (ep_addr & 0x80)
1425 context->
1426 fifos
1427 [cidx].
1428 pipe =
1429 usb_rcvisocpipe
1430 (dev,
1431 ep->
1432 desc.
1433 bEndpointAddress);
1434 else
1435 context->
1436 fifos
1437 [cidx].
1438 pipe =
1439 usb_sndisocpipe
1440 (dev,
1441 ep->
1442 desc.
1443 bEndpointAddress);
1444 context->
1445 fifos[cidx].
1446 usb_transfer_mode
1447 = USB_ISOC;
1448 iso_packet_size =
Karsten Keilb05121b2006-03-06 15:42:41 -08001449 le16_to_cpu(ep->desc.wMaxPacketSize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450 break;
1451 default:
1452 context->
1453 fifos[cidx].
1454 pipe = 0;
1455 } /* switch attribute */
1456
1457 if (context->fifos[cidx].pipe) {
1458 context->fifos[cidx].
1459 fifonum = cidx;
1460 context->fifos[cidx].hfc =
1461 context;
Karsten Keilb05121b2006-03-06 15:42:41 -08001462 context->fifos[cidx].usb_packet_maxlen =
1463 le16_to_cpu(ep->desc.wMaxPacketSize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464 context->fifos[cidx].
1465 intervall =
1466 ep->desc.bInterval;
1467 context->fifos[cidx].
1468 skbuff = NULL;
1469 }
1470 }
1471 ep++;
1472 }
1473 context->dev = dev; /* save device */
1474 context->if_used = ifnum; /* save used interface */
1475 context->alt_used = alt_used; /* and alternate config */
1476 context->ctrl_paksize = dev->descriptor.bMaxPacketSize0; /* control size */
1477 context->cfg_used = vcf[16]; /* store used config */
1478 context->vend_idx = vend_idx; /* store found vendor */
1479 context->packet_size = packet_size;
1480 context->iso_packet_size = iso_packet_size;
1481
1482 /* create the control pipes needed for register access */
1483 context->ctrl_in_pipe =
1484 usb_rcvctrlpipe(context->dev, 0);
1485 context->ctrl_out_pipe =
1486 usb_sndctrlpipe(context->dev, 0);
1487 context->ctrl_urb = usb_alloc_urb(0, GFP_KERNEL);
1488
Martin Bachem597a1072005-11-07 01:00:20 -08001489 driver_info =
1490 (hfcsusb_vdata *) hfcusb_idtab[vend_idx].
1491 driver_info;
1492 printk(KERN_INFO "HFC-S USB: detected \"%s\"\n",
1493 driver_info->vend_name);
Martin Bachemd6c59c12007-08-21 14:26:21 +02001494
1495 DBG(HFCUSB_DBG_INIT,
1496 "HFC-S USB: Endpoint-Config: %s (if=%d alt=%d), E-Channel(%d)",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497 conf_str[small_match], context->if_used,
Martin Bachemd6c59c12007-08-21 14:26:21 +02001498 context->alt_used,
1499 validconf[small_match][18]);
1500
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501 /* init the chip and register the driver */
Martin Bachemd6c59c12007-08-21 14:26:21 +02001502 if (hfc_usb_init(context)) {
Alan Sternae2d9902007-05-23 13:58:12 -07001503 usb_kill_urb(context->ctrl_urb);
1504 usb_free_urb(context->ctrl_urb);
1505 context->ctrl_urb = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506 kfree(context);
1507 return (-EIO);
1508 }
1509 usb_set_intfdata(intf, context);
1510 return (0);
1511 }
1512 } else {
1513 printk(KERN_INFO
1514 "HFC-S USB: no valid vendor found in USB descriptor\n");
1515 }
1516 return (-EIO);
1517}
1518
Martin Bachemd6c59c12007-08-21 14:26:21 +02001519/* callback for unplugged USB device */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520static void
Martin Bachem7441dd12007-10-14 18:10:30 +02001521hfc_usb_disconnect(struct usb_interface *intf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522{
1523 hfcusb_data *context = usb_get_intfdata(intf);
1524 int i;
Martin Bachemd6c59c12007-08-21 14:26:21 +02001525
1526 handle_led(context, LED_POWER_OFF);
Martin Bachem7441dd12007-10-14 18:10:30 +02001527 schedule_timeout(HZ / 100);
Martin Bachemd6c59c12007-08-21 14:26:21 +02001528
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529 printk(KERN_INFO "HFC-S USB: device disconnect\n");
Martin Bachemd6c59c12007-08-21 14:26:21 +02001530 context->disc_flag = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531 usb_set_intfdata(intf, NULL);
Martin Bachem7441dd12007-10-14 18:10:30 +02001532
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533 if (timer_pending(&context->t3_timer))
1534 del_timer(&context->t3_timer);
1535 if (timer_pending(&context->t4_timer))
1536 del_timer(&context->t4_timer);
Martin Bachemd6c59c12007-08-21 14:26:21 +02001537
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538 /* tell all fifos to terminate */
1539 for (i = 0; i < HFCUSB_NUM_FIFOS; i++) {
1540 if (context->fifos[i].usb_transfer_mode == USB_ISOC) {
1541 if (context->fifos[i].active > 0) {
1542 stop_isoc_chain(&context->fifos[i]);
Martin Bachemd6c59c12007-08-21 14:26:21 +02001543 DBG(HFCUSB_DBG_INIT,
1544 "HFC-S USB: %s stopping ISOC chain Fifo(%i)",
Harvey Harrison156f1ed2008-04-28 02:14:40 -07001545 __func__, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546 }
1547 } else {
1548 if (context->fifos[i].active > 0) {
1549 context->fifos[i].active = 0;
Martin Bachemd6c59c12007-08-21 14:26:21 +02001550 DBG(HFCUSB_DBG_INIT,
1551 "HFC-S USB: %s unlinking URB for Fifo(%i)",
Harvey Harrison156f1ed2008-04-28 02:14:40 -07001552 __func__, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553 }
Alan Sternae2d9902007-05-23 13:58:12 -07001554 usb_kill_urb(context->fifos[i].urb);
1555 usb_free_urb(context->fifos[i].urb);
1556 context->fifos[i].urb = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557 }
1558 context->fifos[i].active = 0;
1559 }
Alan Sternae2d9902007-05-23 13:58:12 -07001560 usb_kill_urb(context->ctrl_urb);
1561 usb_free_urb(context->ctrl_urb);
1562 context->ctrl_urb = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563 hisax_unregister(&context->d_if);
1564 kfree(context); /* free our structure again */
Martin Bachemd6c59c12007-08-21 14:26:21 +02001565}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567static struct usb_driver hfc_drv = {
Martin Bachem597a1072005-11-07 01:00:20 -08001568 .name = "hfc_usb",
1569 .id_table = hfcusb_idtab,
1570 .probe = hfc_usb_probe,
1571 .disconnect = hfc_usb_disconnect,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572};
Martin Bachemd6c59c12007-08-21 14:26:21 +02001573
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574static void __exit
Martin Bachemd6c59c12007-08-21 14:26:21 +02001575hfc_usb_mod_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576{
Martin Bachemd6c59c12007-08-21 14:26:21 +02001577 usb_deregister(&hfc_drv); /* release our driver */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578 printk(KERN_INFO "HFC-S USB: module removed\n");
1579}
1580
1581static int __init
Martin Bachemd6c59c12007-08-21 14:26:21 +02001582hfc_usb_mod_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584 char revstr[30], datestr[30], dummy[30];
Martin Bachemd6c59c12007-08-21 14:26:21 +02001585#ifndef CONFIG_HISAX_DEBUG
1586 hfc_debug = debug;
1587#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588 sscanf(hfcusb_revision,
1589 "%s %s $ %s %s %s $ ", dummy, revstr,
1590 dummy, datestr, dummy);
1591 printk(KERN_INFO
1592 "HFC-S USB: driver module revision %s date %s loaded, (debug=%i)\n",
1593 revstr, datestr, debug);
1594 if (usb_register(&hfc_drv)) {
1595 printk(KERN_INFO
1596 "HFC-S USB: Unable to register HFC-S USB module at usb stack\n");
1597 return (-1); /* unable to register */
1598 }
1599 return (0);
1600}
1601
Martin Bachemd6c59c12007-08-21 14:26:21 +02001602module_init(hfc_usb_mod_init);
1603module_exit(hfc_usb_mod_exit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604MODULE_AUTHOR(DRIVER_AUTHOR);
1605MODULE_DESCRIPTION(DRIVER_DESC);
1606MODULE_LICENSE("GPL");
Martin Bachem597a1072005-11-07 01:00:20 -08001607MODULE_DEVICE_TABLE(usb, hfcusb_idtab);