blob: 8df889b0c1a97b1a6c3546545840bc3279755486 [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/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 Bachem7441dd12007-10-14 18:10:30 +020048 "$Revision: 2.3.2.24 $ $Date: 2007/10/14 08:40:29 $ ";
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 },
Martin Bachem7441dd12007-10-14 18:10:30 +0200129 {
130 USB_DEVICE(0x071d, 0x1005),
131 .driver_info = (unsigned long) &((hfcsusb_vdata)
132 {LED_SCHEME1, {0x02, 0, 0x01, 0x04},
133 "Eicon DIVA USB 4.0"}),
134 },
Andrew Mortond6a1a642005-11-15 00:09:13 -0800135 { }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136};
137
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138/* structure defining input+output fifos (interrupt/bulk mode) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139struct usb_fifo; /* forward definition */
140typedef struct iso_urb_struct {
141 struct urb *purb;
142 __u8 buffer[ISO_BUFFER_SIZE]; /* buffer incoming/outgoing data */
143 struct usb_fifo *owner_fifo; /* pointer to owner fifo */
144} iso_urb_struct;
145
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146struct hfcusb_data; /* forward definition */
Martin Bachemd6c59c12007-08-21 14:26:21 +0200147
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148typedef struct usb_fifo {
149 int fifonum; /* fifo index attached to this structure */
150 int active; /* fifo is currently active */
151 struct hfcusb_data *hfc; /* pointer to main structure */
152 int pipe; /* address of endpoint */
153 __u8 usb_packet_maxlen; /* maximum length for usb transfer */
154 unsigned int max_size; /* maximum size of receive/send packet */
155 __u8 intervall; /* interrupt interval */
156 struct sk_buff *skbuff; /* actual used buffer */
157 struct urb *urb; /* transfer structure for usb routines */
158 __u8 buffer[128]; /* buffer incoming/outgoing data */
159 int bit_line; /* how much bits are in the fifo? */
160
161 volatile __u8 usb_transfer_mode; /* switched between ISO and INT */
162 iso_urb_struct iso[2]; /* need two urbs to have one always for pending */
163 struct hisax_if *hif; /* hisax interface */
164 int delete_flg; /* only delete skbuff once */
165 int last_urblen; /* remember length of last packet */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166} usb_fifo;
167
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168/* structure holding all data for one device */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169typedef struct hfcusb_data {
170 /* HiSax Interface for loadable Layer1 drivers */
Martin Bachemd6c59c12007-08-21 14:26:21 +0200171 struct hisax_d_if d_if; /* see hisax_if.h */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 struct hisax_b_if b_if[2]; /* see hisax_if.h */
173 int protocol;
174
175 struct usb_device *dev; /* our device */
176 int if_used; /* used interface number */
177 int alt_used; /* used alternate config */
178 int ctrl_paksize; /* control pipe packet size */
Martin Bachemd6c59c12007-08-21 14:26:21 +0200179 int ctrl_in_pipe, /* handles for control pipe */
180 ctrl_out_pipe;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 int cfg_used; /* configuration index used */
182 int vend_idx; /* vendor found */
183 int b_mode[2]; /* B-channel mode */
184 int l1_activated; /* layer 1 activated */
Martin Bachemd6c59c12007-08-21 14:26:21 +0200185 int disc_flag; /* TRUE if device was disonnected to avoid some USB actions */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 int packet_size, iso_packet_size;
187
188 /* control pipe background handling */
189 ctrl_buft ctrl_buff[HFC_CTRL_BUFSIZE]; /* buffer holding queued data */
190 volatile int ctrl_in_idx, ctrl_out_idx, ctrl_cnt; /* input/output pointer + count */
191 struct urb *ctrl_urb; /* transfer structure for control channel */
192
193 struct usb_ctrlrequest ctrl_write; /* buffer for control write request */
194 struct usb_ctrlrequest ctrl_read; /* same for read request */
195
Martin Bachem7441dd12007-10-14 18:10:30 +0200196 __u8 old_led_state, led_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197
198 volatile __u8 threshold_mask; /* threshold actually reported */
199 volatile __u8 bch_enables; /* or mask for sctrl_r and sctrl register values */
200
201 usb_fifo fifos[HFCUSB_NUM_FIFOS]; /* structure holding all fifo data */
202
203 volatile __u8 l1_state; /* actual l1 state */
204 struct timer_list t3_timer; /* timer 3 for activation/deactivation */
205 struct timer_list t4_timer; /* timer 4 for activation/deactivation */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206} hfcusb_data;
207
208
209static void collect_rx_frame(usb_fifo * fifo, __u8 * data, int len,
210 int finish);
211
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212static inline const char *
213symbolic(struct hfcusb_symbolic_list list[], const int num)
214{
215 int i;
216 for (i = 0; list[i].name != NULL; i++)
217 if (list[i].num == num)
218 return (list[i].name);
Karsten Keilb05121b2006-03-06 15:42:41 -0800219 return "<unknown ERROR>";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220}
221
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222static void
223ctrl_start_transfer(hfcusb_data * hfc)
224{
225 if (hfc->ctrl_cnt) {
226 hfc->ctrl_urb->pipe = hfc->ctrl_out_pipe;
227 hfc->ctrl_urb->setup_packet = (u_char *) & hfc->ctrl_write;
228 hfc->ctrl_urb->transfer_buffer = NULL;
229 hfc->ctrl_urb->transfer_buffer_length = 0;
230 hfc->ctrl_write.wIndex =
Karsten Keilb05121b2006-03-06 15:42:41 -0800231 cpu_to_le16(hfc->ctrl_buff[hfc->ctrl_out_idx].hfc_reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 hfc->ctrl_write.wValue =
Karsten Keilb05121b2006-03-06 15:42:41 -0800233 cpu_to_le16(hfc->ctrl_buff[hfc->ctrl_out_idx].reg_val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234
235 usb_submit_urb(hfc->ctrl_urb, GFP_ATOMIC); /* start transfer */
236 }
237} /* ctrl_start_transfer */
238
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239static int
240queue_control_request(hfcusb_data * hfc, __u8 reg, __u8 val, int action)
241{
242 ctrl_buft *buf;
243
244 if (hfc->ctrl_cnt >= HFC_CTRL_BUFSIZE)
245 return (1); /* no space left */
246 buf = &hfc->ctrl_buff[hfc->ctrl_in_idx]; /* pointer to new index */
247 buf->hfc_reg = reg;
248 buf->reg_val = val;
249 buf->action = action;
250 if (++hfc->ctrl_in_idx >= HFC_CTRL_BUFSIZE)
251 hfc->ctrl_in_idx = 0; /* pointer wrap */
252 if (++hfc->ctrl_cnt == 1)
253 ctrl_start_transfer(hfc);
254 return (0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255}
256
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257static void
David Howells7d12e782006-10-05 14:55:46 +0100258ctrl_complete(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259{
260 hfcusb_data *hfc = (hfcusb_data *) urb->context;
261 ctrl_buft *buf;
262
263 urb->dev = hfc->dev;
264 if (hfc->ctrl_cnt) {
265 buf = &hfc->ctrl_buff[hfc->ctrl_out_idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 hfc->ctrl_cnt--; /* decrement actual count */
267 if (++hfc->ctrl_out_idx >= HFC_CTRL_BUFSIZE)
268 hfc->ctrl_out_idx = 0; /* pointer wrap */
269
270 ctrl_start_transfer(hfc); /* start next transfer */
271 }
Martin Bachem7441dd12007-10-14 18:10:30 +0200272}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274/* write led data to auxport & invert if necessary */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275static void
276write_led(hfcusb_data * hfc, __u8 led_state)
277{
278 if (led_state != hfc->old_led_state) {
279 hfc->old_led_state = led_state;
280 queue_control_request(hfc, HFCUSB_P_DATA, led_state, 1);
281 }
282}
283
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284static void
Martin Bachem7441dd12007-10-14 18:10:30 +0200285set_led_bit(hfcusb_data * hfc, signed short led_bits, int on)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286{
Martin Bachem7441dd12007-10-14 18:10:30 +0200287 if (on) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 if (led_bits < 0)
289 hfc->led_state &= ~abs(led_bits);
290 else
291 hfc->led_state |= led_bits;
Martin Bachem7441dd12007-10-14 18:10:30 +0200292 } else {
293 if (led_bits < 0)
294 hfc->led_state |= abs(led_bits);
295 else
296 hfc->led_state &= ~led_bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 }
298}
299
Martin Bachemd6c59c12007-08-21 14:26:21 +0200300/* handle LED requests */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301static void
302handle_led(hfcusb_data * hfc, int event)
303{
Martin Bachem597a1072005-11-07 01:00:20 -0800304 hfcsusb_vdata *driver_info =
305 (hfcsusb_vdata *) hfcusb_idtab[hfc->vend_idx].driver_info;
306
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 /* if no scheme -> no LED action */
Martin Bachem597a1072005-11-07 01:00:20 -0800308 if (driver_info->led_scheme == LED_OFF)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 return;
310
311 switch (event) {
312 case LED_POWER_ON:
Martin Bachemd6c59c12007-08-21 14:26:21 +0200313 set_led_bit(hfc, driver_info->led_bits[0], 1);
Martin Bachemd6c59c12007-08-21 14:26:21 +0200314 set_led_bit(hfc, driver_info->led_bits[1], 0);
Martin Bachemd6c59c12007-08-21 14:26:21 +0200315 set_led_bit(hfc, driver_info->led_bits[2], 0);
Martin Bachemd6c59c12007-08-21 14:26:21 +0200316 set_led_bit(hfc, driver_info->led_bits[3], 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 break;
Martin Bachem7441dd12007-10-14 18:10:30 +0200318 case LED_POWER_OFF:
319 set_led_bit(hfc, driver_info->led_bits[0], 0);
320 set_led_bit(hfc, driver_info->led_bits[1], 0);
321 set_led_bit(hfc, driver_info->led_bits[2], 0);
322 set_led_bit(hfc, driver_info->led_bits[3], 0);
323 break;
324 case LED_S0_ON:
325 set_led_bit(hfc, driver_info->led_bits[1], 1);
326 break;
327 case LED_S0_OFF:
328 set_led_bit(hfc, driver_info->led_bits[1], 0);
329 break;
330 case LED_B1_ON:
331 set_led_bit(hfc, driver_info->led_bits[2], 1);
332 break;
333 case LED_B1_OFF:
334 set_led_bit(hfc, driver_info->led_bits[2], 0);
335 break;
336 case LED_B2_ON:
Martin Bachemd6c59c12007-08-21 14:26:21 +0200337 set_led_bit(hfc, driver_info->led_bits[3], 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 break;
Martin Bachem7441dd12007-10-14 18:10:30 +0200339 case LED_B2_OFF:
340 set_led_bit(hfc, driver_info->led_bits[3], 0);
341 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 write_led(hfc, hfc->led_state);
344}
345
Martin Bachemd6c59c12007-08-21 14:26:21 +0200346/* ISDN l1 timer T3 expires */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347static void
348l1_timer_expire_t3(hfcusb_data * hfc)
349{
350 hfc->d_if.ifc.l1l2(&hfc->d_if.ifc, PH_DEACTIVATE | INDICATION,
351 NULL);
Martin Bachemd6c59c12007-08-21 14:26:21 +0200352
353 DBG(HFCUSB_DBG_STATES,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 "HFC-S USB: PH_DEACTIVATE | INDICATION sent (T3 expire)");
Martin Bachemd6c59c12007-08-21 14:26:21 +0200355
356 hfc->l1_activated = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 handle_led(hfc, LED_S0_OFF);
358 /* deactivate : */
359 queue_control_request(hfc, HFCUSB_STATES, 0x10, 1);
360 queue_control_request(hfc, HFCUSB_STATES, 3, 1);
361}
362
Martin Bachemd6c59c12007-08-21 14:26:21 +0200363/* ISDN l1 timer T4 expires */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364static void
365l1_timer_expire_t4(hfcusb_data * hfc)
366{
367 hfc->d_if.ifc.l1l2(&hfc->d_if.ifc, PH_DEACTIVATE | INDICATION,
368 NULL);
Martin Bachemd6c59c12007-08-21 14:26:21 +0200369
370 DBG(HFCUSB_DBG_STATES,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 "HFC-S USB: PH_DEACTIVATE | INDICATION sent (T4 expire)");
Martin Bachemd6c59c12007-08-21 14:26:21 +0200372
373 hfc->l1_activated = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 handle_led(hfc, LED_S0_OFF);
375}
376
Martin Bachemd6c59c12007-08-21 14:26:21 +0200377/* S0 state changed */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378static void
Martin Bachemd6c59c12007-08-21 14:26:21 +0200379s0_state_handler(hfcusb_data * hfc, __u8 state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380{
381 __u8 old_state;
382
383 old_state = hfc->l1_state;
384 if (state == old_state || state < 1 || state > 8)
385 return;
386
Martin Bachemd6c59c12007-08-21 14:26:21 +0200387 DBG(HFCUSB_DBG_STATES, "HFC-S USB: S0 statechange(%d -> %d)",
388 old_state, state);
389
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 if (state < 4 || state == 7 || state == 8) {
391 if (timer_pending(&hfc->t3_timer))
392 del_timer(&hfc->t3_timer);
Martin Bachemd6c59c12007-08-21 14:26:21 +0200393 DBG(HFCUSB_DBG_STATES, "HFC-S USB: T3 deactivated");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 }
395 if (state >= 7) {
396 if (timer_pending(&hfc->t4_timer))
397 del_timer(&hfc->t4_timer);
Martin Bachemd6c59c12007-08-21 14:26:21 +0200398 DBG(HFCUSB_DBG_STATES, "HFC-S USB: T4 deactivated");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 }
400
401 if (state == 7 && !hfc->l1_activated) {
402 hfc->d_if.ifc.l1l2(&hfc->d_if.ifc,
403 PH_ACTIVATE | INDICATION, NULL);
Martin Bachemd6c59c12007-08-21 14:26:21 +0200404 DBG(HFCUSB_DBG_STATES, "HFC-S USB: PH_ACTIVATE | INDICATION sent");
405 hfc->l1_activated = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 handle_led(hfc, LED_S0_ON);
407 } else if (state <= 3 /* && activated */ ) {
408 if (old_state == 7 || old_state == 8) {
Martin Bachemd6c59c12007-08-21 14:26:21 +0200409 DBG(HFCUSB_DBG_STATES, "HFC-S USB: T4 activated");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 if (!timer_pending(&hfc->t4_timer)) {
411 hfc->t4_timer.expires =
412 jiffies + (HFC_TIMER_T4 * HZ) / 1000;
413 add_timer(&hfc->t4_timer);
414 }
415 } else {
416 hfc->d_if.ifc.l1l2(&hfc->d_if.ifc,
417 PH_DEACTIVATE | INDICATION,
418 NULL);
Martin Bachemd6c59c12007-08-21 14:26:21 +0200419 DBG(HFCUSB_DBG_STATES,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 "HFC-S USB: PH_DEACTIVATE | INDICATION sent");
Martin Bachemd6c59c12007-08-21 14:26:21 +0200421 hfc->l1_activated = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 handle_led(hfc, LED_S0_OFF);
423 }
424 }
425 hfc->l1_state = state;
426}
427
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428static void
429fill_isoc_urb(struct urb *urb, struct usb_device *dev, unsigned int pipe,
430 void *buf, int num_packets, int packet_size, int interval,
431 usb_complete_t complete, void *context)
432{
433 int k;
434
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 urb->dev = dev;
436 urb->pipe = pipe;
437 urb->complete = complete;
438 urb->number_of_packets = num_packets;
439 urb->transfer_buffer_length = packet_size * num_packets;
440 urb->context = context;
441 urb->transfer_buffer = buf;
442 urb->transfer_flags = URB_ISO_ASAP;
443 urb->actual_length = 0;
444 urb->interval = interval;
445 for (k = 0; k < num_packets; k++) {
446 urb->iso_frame_desc[k].offset = packet_size * k;
447 urb->iso_frame_desc[k].length = packet_size;
448 urb->iso_frame_desc[k].actual_length = 0;
449 }
450}
451
452/* allocs urbs and start isoc transfer with two pending urbs to avoid
Martin Bachemd6c59c12007-08-21 14:26:21 +0200453 * gaps in the transfer chain
454 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455static int
456start_isoc_chain(usb_fifo * fifo, int num_packets_per_urb,
457 usb_complete_t complete, int packet_size)
458{
459 int i, k, errcode;
460
Martin Bachemd6c59c12007-08-21 14:26:21 +0200461 DBG(HFCUSB_DBG_INIT, "HFC-S USB: starting ISO-URBs for fifo:%d\n",
462 fifo->fifonum);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463
464 /* allocate Memory for Iso out Urbs */
465 for (i = 0; i < 2; i++) {
466 if (!(fifo->iso[i].purb)) {
467 fifo->iso[i].purb =
468 usb_alloc_urb(num_packets_per_urb, GFP_KERNEL);
469 if (!(fifo->iso[i].purb)) {
470 printk(KERN_INFO
471 "alloc urb for fifo %i failed!!!",
472 fifo->fifonum);
473 }
474 fifo->iso[i].owner_fifo = (struct usb_fifo *) fifo;
475
476 /* Init the first iso */
477 if (ISO_BUFFER_SIZE >=
478 (fifo->usb_packet_maxlen *
479 num_packets_per_urb)) {
480 fill_isoc_urb(fifo->iso[i].purb,
481 fifo->hfc->dev, fifo->pipe,
482 fifo->iso[i].buffer,
483 num_packets_per_urb,
484 fifo->usb_packet_maxlen,
485 fifo->intervall, complete,
486 &fifo->iso[i]);
487 memset(fifo->iso[i].buffer, 0,
488 sizeof(fifo->iso[i].buffer));
489 /* defining packet delimeters in fifo->buffer */
490 for (k = 0; k < num_packets_per_urb; k++) {
491 fifo->iso[i].purb->
492 iso_frame_desc[k].offset =
493 k * packet_size;
494 fifo->iso[i].purb->
495 iso_frame_desc[k].length =
496 packet_size;
497 }
498 } else {
499 printk(KERN_INFO
500 "HFC-S USB: ISO Buffer size to small!\n");
501 }
502 }
503 fifo->bit_line = BITLINE_INF;
504
505 errcode = usb_submit_urb(fifo->iso[i].purb, GFP_KERNEL);
506 fifo->active = (errcode >= 0) ? 1 : 0;
Martin Bachemd6c59c12007-08-21 14:26:21 +0200507 if (errcode < 0)
508 printk(KERN_INFO "HFC-S USB: usb_submit_urb URB nr:%d, error(%i): '%s'\n",
509 i, errcode, symbolic(urb_errlist, errcode));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 }
511 return (fifo->active);
512}
513
514/* stops running iso chain and frees their pending urbs */
515static void
516stop_isoc_chain(usb_fifo * fifo)
517{
518 int i;
519
520 for (i = 0; i < 2; i++) {
521 if (fifo->iso[i].purb) {
Martin Bachemd6c59c12007-08-21 14:26:21 +0200522 DBG(HFCUSB_DBG_INIT,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 "HFC-S USB: Stopping iso chain for fifo %i.%i",
524 fifo->fifonum, i);
Alan Sternae2d9902007-05-23 13:58:12 -0700525 usb_kill_urb(fifo->iso[i].purb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 usb_free_urb(fifo->iso[i].purb);
527 fifo->iso[i].purb = NULL;
528 }
529 }
Martin Bachemd6c59c12007-08-21 14:26:21 +0200530
Alan Sternae2d9902007-05-23 13:58:12 -0700531 usb_kill_urb(fifo->urb);
532 usb_free_urb(fifo->urb);
533 fifo->urb = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 fifo->active = 0;
535}
536
537/* defines how much ISO packets are handled in one URB */
538static int iso_packets[8] =
539 { ISOC_PACKETS_B, ISOC_PACKETS_B, ISOC_PACKETS_B, ISOC_PACKETS_B,
540 ISOC_PACKETS_D, ISOC_PACKETS_D, ISOC_PACKETS_D, ISOC_PACKETS_D
541};
542
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543static void
David Howells7d12e782006-10-05 14:55:46 +0100544tx_iso_complete(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545{
546 iso_urb_struct *context_iso_urb = (iso_urb_struct *) urb->context;
547 usb_fifo *fifo = context_iso_urb->owner_fifo;
548 hfcusb_data *hfc = fifo->hfc;
549 int k, tx_offset, num_isoc_packets, sink, len, current_len,
550 errcode;
551 int frame_complete, transp_mode, fifon, status;
552 __u8 threshbit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553
554 fifon = fifo->fifonum;
555 status = urb->status;
556
557 tx_offset = 0;
558
Martin Bachemd6c59c12007-08-21 14:26:21 +0200559 /* ISO transfer only partially completed,
560 look at individual frame status for details */
561 if (status == -EXDEV) {
562 DBG(HFCUSB_DBG_VERBOSE_USB, "HFC-S USB: tx_iso_complete with -EXDEV"
563 ", urb->status %d, fifonum %d\n",
564 status, fifon);
565
566 for (k = 0; k < iso_packets[fifon]; ++k) {
567 errcode = urb->iso_frame_desc[k].status;
568 if (errcode)
569 DBG(HFCUSB_DBG_VERBOSE_USB, "HFC-S USB: tx_iso_complete "
570 "packet %i, status: %i\n",
571 k, errcode);
572 }
573
574 // clear status, so go on with ISO transfers
575 status = 0;
576 }
577
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 if (fifo->active && !status) {
579 transp_mode = 0;
580 if (fifon < 4 && hfc->b_mode[fifon / 2] == L1_MODE_TRANS)
Martin Bachemd6c59c12007-08-21 14:26:21 +0200581 transp_mode = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582
583 /* is FifoFull-threshold set for our channel? */
Martin Bachemd6c59c12007-08-21 14:26:21 +0200584 threshbit = (hfc->threshold_mask & (1 << fifon));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 num_isoc_packets = iso_packets[fifon];
586
587 /* predict dataflow to avoid fifo overflow */
588 if (fifon >= HFCUSB_D_TX) {
589 sink = (threshbit) ? SINK_DMIN : SINK_DMAX;
590 } else {
591 sink = (threshbit) ? SINK_MIN : SINK_MAX;
592 }
593 fill_isoc_urb(urb, fifo->hfc->dev, fifo->pipe,
594 context_iso_urb->buffer, num_isoc_packets,
595 fifo->usb_packet_maxlen, fifo->intervall,
596 tx_iso_complete, urb->context);
597 memset(context_iso_urb->buffer, 0,
598 sizeof(context_iso_urb->buffer));
Martin Bachemd6c59c12007-08-21 14:26:21 +0200599 frame_complete = 0;
600
601 /* Generate next ISO Packets */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 for (k = 0; k < num_isoc_packets; ++k) {
603 if (fifo->skbuff) {
604 len = fifo->skbuff->len;
605 /* we lower data margin every msec */
606 fifo->bit_line -= sink;
607 current_len = (0 - fifo->bit_line) / 8;
608 /* maximum 15 byte for every ISO packet makes our life easier */
609 if (current_len > 14)
610 current_len = 14;
611 current_len =
612 (len <=
613 current_len) ? len : current_len;
614 /* how much bit do we put on the line? */
615 fifo->bit_line += current_len * 8;
616
617 context_iso_urb->buffer[tx_offset] = 0;
618 if (current_len == len) {
619 if (!transp_mode) {
620 /* here frame completion */
621 context_iso_urb->
622 buffer[tx_offset] = 1;
623 /* add 2 byte flags and 16bit CRC at end of ISDN frame */
624 fifo->bit_line += 32;
625 }
Martin Bachemd6c59c12007-08-21 14:26:21 +0200626 frame_complete = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 }
628
629 memcpy(context_iso_urb->buffer +
630 tx_offset + 1, fifo->skbuff->data,
631 current_len);
632 skb_pull(fifo->skbuff, current_len);
633
634 /* define packet delimeters within the URB buffer */
635 urb->iso_frame_desc[k].offset = tx_offset;
636 urb->iso_frame_desc[k].length =
637 current_len + 1;
638
639 tx_offset += (current_len + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 } else {
641 urb->iso_frame_desc[k].offset =
642 tx_offset++;
643
644 urb->iso_frame_desc[k].length = 1;
645 fifo->bit_line -= sink; /* we lower data margin every msec */
646
647 if (fifo->bit_line < BITLINE_INF) {
648 fifo->bit_line = BITLINE_INF;
649 }
650 }
651
652 if (frame_complete) {
Martin Bachemd6c59c12007-08-21 14:26:21 +0200653 fifo->delete_flg = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 fifo->hif->l1l2(fifo->hif,
655 PH_DATA | CONFIRM,
Alan Cox17a45062006-10-03 01:13:55 -0700656 (void *) (unsigned long) fifo->skbuff->
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 truesize);
658 if (fifo->skbuff && fifo->delete_flg) {
659 dev_kfree_skb_any(fifo->skbuff);
660 fifo->skbuff = NULL;
Martin Bachemd6c59c12007-08-21 14:26:21 +0200661 fifo->delete_flg = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 }
Martin Bachemd6c59c12007-08-21 14:26:21 +0200663 frame_complete = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 }
665 }
666 errcode = usb_submit_urb(urb, GFP_ATOMIC);
667 if (errcode < 0) {
668 printk(KERN_INFO
Martin Bachemd6c59c12007-08-21 14:26:21 +0200669 "HFC-S USB: error submitting ISO URB: %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 errcode);
671 }
672 } else {
673 if (status && !hfc->disc_flag) {
674 printk(KERN_INFO
Martin Bachemd6c59c12007-08-21 14:26:21 +0200675 "HFC-S USB: tx_iso_complete: error(%i): '%s', fifonum=%d\n",
676 status, symbolic(urb_errlist, status), fifon);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 }
678 }
Martin Bachemd6c59c12007-08-21 14:26:21 +0200679}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681static void
David Howells7d12e782006-10-05 14:55:46 +0100682rx_iso_complete(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683{
684 iso_urb_struct *context_iso_urb = (iso_urb_struct *) urb->context;
685 usb_fifo *fifo = context_iso_urb->owner_fifo;
686 hfcusb_data *hfc = fifo->hfc;
687 int k, len, errcode, offset, num_isoc_packets, fifon, maxlen,
688 status;
689 unsigned int iso_status;
690 __u8 *buf;
691 static __u8 eof[8];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692
693 fifon = fifo->fifonum;
694 status = urb->status;
695
696 if (urb->status == -EOVERFLOW) {
Martin Bachemd6c59c12007-08-21 14:26:21 +0200697 DBG(HFCUSB_DBG_VERBOSE_USB,
698 "HFC-USB: ignoring USB DATAOVERRUN fifo(%i)", fifon);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 status = 0;
700 }
Martin Bachemd6c59c12007-08-21 14:26:21 +0200701
702 /* ISO transfer only partially completed,
703 look at individual frame status for details */
704 if (status == -EXDEV) {
705 DBG(HFCUSB_DBG_VERBOSE_USB, "HFC-S USB: rx_iso_complete with -EXDEV "
706 "urb->status %d, fifonum %d\n",
707 status, fifon);
708 status = 0;
709 }
710
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 if (fifo->active && !status) {
712 num_isoc_packets = iso_packets[fifon];
713 maxlen = fifo->usb_packet_maxlen;
714 for (k = 0; k < num_isoc_packets; ++k) {
715 len = urb->iso_frame_desc[k].actual_length;
716 offset = urb->iso_frame_desc[k].offset;
717 buf = context_iso_urb->buffer + offset;
718 iso_status = urb->iso_frame_desc[k].status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719
Martin Bachemd6c59c12007-08-21 14:26:21 +0200720 if (iso_status && !hfc->disc_flag)
721 DBG(HFCUSB_DBG_VERBOSE_USB,
722 "HFC-S USB: rx_iso_complete "
723 "ISO packet %i, status: %i\n",
724 k, iso_status);
725
726 if (fifon == HFCUSB_D_RX) {
727 DBG(HFCUSB_DBG_VERBOSE_USB,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 "HFC-S USB: ISO-D-RX lst_urblen:%2d "
Martin Bachemd6c59c12007-08-21 14:26:21 +0200729 "act_urblen:%2d max-urblen:%2d EOF:0x%0x",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 fifo->last_urblen, len, maxlen,
731 eof[5]);
Martin Bachemd6c59c12007-08-21 14:26:21 +0200732
733 DBG_PACKET(HFCUSB_DBG_VERBOSE_USB, buf, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 }
Martin Bachemd6c59c12007-08-21 14:26:21 +0200735
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 if (fifo->last_urblen != maxlen) {
737 /* the threshold mask is in the 2nd status byte */
738 hfc->threshold_mask = buf[1];
739 /* care for L1 state only for D-Channel
740 to avoid overlapped iso completions */
Martin Bachemd6c59c12007-08-21 14:26:21 +0200741 if (fifon == HFCUSB_D_RX) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 /* the S0 state is in the upper half
743 of the 1st status byte */
Martin Bachemd6c59c12007-08-21 14:26:21 +0200744 s0_state_handler(hfc, buf[0] >> 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 }
746 eof[fifon] = buf[0] & 1;
747 if (len > 2)
748 collect_rx_frame(fifo, buf + 2,
749 len - 2,
Martin Bachemd6c59c12007-08-21 14:26:21 +0200750 (len < maxlen) ?
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 eof[fifon] : 0);
752 } else {
753 collect_rx_frame(fifo, buf, len,
754 (len <
755 maxlen) ? eof[fifon] :
756 0);
757 }
758 fifo->last_urblen = len;
759 }
760
761 fill_isoc_urb(urb, fifo->hfc->dev, fifo->pipe,
762 context_iso_urb->buffer, num_isoc_packets,
763 fifo->usb_packet_maxlen, fifo->intervall,
764 rx_iso_complete, urb->context);
765 errcode = usb_submit_urb(urb, GFP_ATOMIC);
766 if (errcode < 0) {
Martin Bachemd6c59c12007-08-21 14:26:21 +0200767 printk(KERN_ERR
768 "HFC-S USB: error submitting ISO URB: %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 errcode);
770 }
771 } else {
772 if (status && !hfc->disc_flag) {
Martin Bachemd6c59c12007-08-21 14:26:21 +0200773 printk(KERN_ERR
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 "HFC-S USB: rx_iso_complete : "
775 "urb->status %d, fifonum %d\n",
776 status, fifon);
777 }
778 }
Martin Bachemd6c59c12007-08-21 14:26:21 +0200779}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780
Martin Bachemd6c59c12007-08-21 14:26:21 +0200781/* collect rx data from INT- and ISO-URBs */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782static void
783collect_rx_frame(usb_fifo * fifo, __u8 * data, int len, int finish)
784{
785 hfcusb_data *hfc = fifo->hfc;
786 int transp_mode, fifon;
Martin Bachemd6c59c12007-08-21 14:26:21 +0200787
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788 fifon = fifo->fifonum;
789 transp_mode = 0;
790 if (fifon < 4 && hfc->b_mode[fifon / 2] == L1_MODE_TRANS)
Martin Bachemd6c59c12007-08-21 14:26:21 +0200791 transp_mode = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792
793 if (!fifo->skbuff) {
794 fifo->skbuff = dev_alloc_skb(fifo->max_size + 3);
795 if (!fifo->skbuff) {
Martin Bachemd6c59c12007-08-21 14:26:21 +0200796 printk(KERN_ERR
797 "HFC-S USB: cannot allocate buffer for fifo(%d)\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 fifon);
799 return;
800 }
801 }
802 if (len) {
803 if (fifo->skbuff->len + len < fifo->max_size) {
804 memcpy(skb_put(fifo->skbuff, len), data, len);
805 } else {
Martin Bachemd6c59c12007-08-21 14:26:21 +0200806 DBG(HFCUSB_DBG_FIFO_ERR,
807 "HCF-USB: got frame exceeded fifo->max_size(%d) fifo(%d)",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 fifo->max_size, fifon);
Martin Bachemd6c59c12007-08-21 14:26:21 +0200809 DBG_SKB(HFCUSB_DBG_VERBOSE_USB, fifo->skbuff);
810 skb_trim(fifo->skbuff, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 }
812 }
813 if (transp_mode && fifo->skbuff->len >= 128) {
814 fifo->hif->l1l2(fifo->hif, PH_DATA | INDICATION,
815 fifo->skbuff);
816 fifo->skbuff = NULL;
817 return;
818 }
819 /* we have a complete hdlc packet */
820 if (finish) {
821 if ((!fifo->skbuff->data[fifo->skbuff->len - 1])
822 && (fifo->skbuff->len > 3)) {
Martin Bachemd6c59c12007-08-21 14:26:21 +0200823
824 if (fifon == HFCUSB_D_RX) {
825 DBG(HFCUSB_DBG_DCHANNEL,
826 "HFC-S USB: D-RX len(%d)", fifo->skbuff->len);
827 DBG_SKB(HFCUSB_DBG_DCHANNEL, fifo->skbuff);
828 }
829
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 /* remove CRC & status */
831 skb_trim(fifo->skbuff, fifo->skbuff->len - 3);
832 if (fifon == HFCUSB_PCM_RX) {
833 fifo->hif->l1l2(fifo->hif,
834 PH_DATA_E | INDICATION,
835 fifo->skbuff);
836 } else
837 fifo->hif->l1l2(fifo->hif,
838 PH_DATA | INDICATION,
839 fifo->skbuff);
840 fifo->skbuff = NULL; /* buffer was freed from upper layer */
841 } else {
Martin Bachemd6c59c12007-08-21 14:26:21 +0200842 DBG(HFCUSB_DBG_FIFO_ERR,
843 "HFC-S USB: ERROR frame len(%d) fifo(%d)",
844 fifo->skbuff->len, fifon);
845 DBG_SKB(HFCUSB_DBG_VERBOSE_USB, fifo->skbuff);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 skb_trim(fifo->skbuff, 0);
847 }
848 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849}
850
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851static void
Martin Bachemd6c59c12007-08-21 14:26:21 +0200852rx_int_complete(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853{
854 int len;
855 int status;
856 __u8 *buf, maxlen, fifon;
857 usb_fifo *fifo = (usb_fifo *) urb->context;
858 hfcusb_data *hfc = fifo->hfc;
859 static __u8 eof[8];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860
861 urb->dev = hfc->dev; /* security init */
862
863 fifon = fifo->fifonum;
864 if ((!fifo->active) || (urb->status)) {
Martin Bachemd6c59c12007-08-21 14:26:21 +0200865 DBG(HFCUSB_DBG_INIT, "HFC-S USB: RX-Fifo %i is going down (%i)",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866 fifon, urb->status);
Martin Bachemd6c59c12007-08-21 14:26:21 +0200867
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 fifo->urb->interval = 0; /* cancel automatic rescheduling */
869 if (fifo->skbuff) {
870 dev_kfree_skb_any(fifo->skbuff);
871 fifo->skbuff = NULL;
872 }
873 return;
874 }
875 len = urb->actual_length;
876 buf = fifo->buffer;
877 maxlen = fifo->usb_packet_maxlen;
878
Martin Bachemd6c59c12007-08-21 14:26:21 +0200879 if (fifon == HFCUSB_D_RX) {
880 DBG(HFCUSB_DBG_VERBOSE_USB,
881 "HFC-S USB: INT-D-RX lst_urblen:%2d "
882 "act_urblen:%2d max-urblen:%2d EOF:0x%0x",
883 fifo->last_urblen, len, maxlen,
884 eof[5]);
885 DBG_PACKET(HFCUSB_DBG_VERBOSE_USB, buf, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887
888 if (fifo->last_urblen != fifo->usb_packet_maxlen) {
889 /* the threshold mask is in the 2nd status byte */
890 hfc->threshold_mask = buf[1];
891 /* the S0 state is in the upper half of the 1st status byte */
Martin Bachemd6c59c12007-08-21 14:26:21 +0200892 s0_state_handler(hfc, buf[0] >> 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893 eof[fifon] = buf[0] & 1;
894 /* if we have more than the 2 status bytes -> collect data */
895 if (len > 2)
896 collect_rx_frame(fifo, buf + 2,
897 urb->actual_length - 2,
898 (len < maxlen) ? eof[fifon] : 0);
899 } else {
900 collect_rx_frame(fifo, buf, urb->actual_length,
901 (len < maxlen) ? eof[fifon] : 0);
902 }
903 fifo->last_urblen = urb->actual_length;
904 status = usb_submit_urb(urb, GFP_ATOMIC);
905 if (status) {
906 printk(KERN_INFO
Martin Bachemd6c59c12007-08-21 14:26:21 +0200907 "HFC-S USB: %s error resubmitting URB fifo(%d)\n",
Harvey Harrison156f1ed2008-04-28 02:14:40 -0700908 __func__, fifon);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 }
Martin Bachemd6c59c12007-08-21 14:26:21 +0200910}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911
Martin Bachemd6c59c12007-08-21 14:26:21 +0200912/* start initial INT-URB for certain fifo */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913static void
914start_int_fifo(usb_fifo * fifo)
915{
916 int errcode;
917
Martin Bachemd6c59c12007-08-21 14:26:21 +0200918 DBG(HFCUSB_DBG_INIT, "HFC-S USB: starting RX INT-URB for fifo:%d\n",
919 fifo->fifonum);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920
921 if (!fifo->urb) {
922 fifo->urb = usb_alloc_urb(0, GFP_KERNEL);
923 if (!fifo->urb)
924 return;
925 }
926 usb_fill_int_urb(fifo->urb, fifo->hfc->dev, fifo->pipe,
927 fifo->buffer, fifo->usb_packet_maxlen,
Martin Bachemd6c59c12007-08-21 14:26:21 +0200928 rx_int_complete, fifo, fifo->intervall);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 fifo->active = 1; /* must be marked active */
930 errcode = usb_submit_urb(fifo->urb, GFP_KERNEL);
931 if (errcode) {
Martin Bachemd6c59c12007-08-21 14:26:21 +0200932 printk(KERN_ERR
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933 "HFC-S USB: submit URB error(start_int_info): status:%i\n",
934 errcode);
935 fifo->active = 0;
936 fifo->skbuff = NULL;
937 }
Martin Bachemd6c59c12007-08-21 14:26:21 +0200938}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940static void
Martin Bachemd6c59c12007-08-21 14:26:21 +0200941setup_bchannel(hfcusb_data * hfc, int channel, int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942{
943 __u8 val, idx_table[2] = { 0, 2 };
944
945 if (hfc->disc_flag) {
946 return;
947 }
Martin Bachemd6c59c12007-08-21 14:26:21 +0200948 DBG(HFCUSB_DBG_STATES, "HFC-S USB: setting channel %d to mode %d",
949 channel, mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950 hfc->b_mode[channel] = mode;
951
952 /* setup CON_HDLC */
953 val = 0;
954 if (mode != L1_MODE_NULL)
955 val = 8; /* enable fifo? */
956 if (mode == L1_MODE_TRANS)
957 val |= 2; /* set transparent bit */
958
959 /* set FIFO to transmit register */
960 queue_control_request(hfc, HFCUSB_FIFO, idx_table[channel], 1);
961 queue_control_request(hfc, HFCUSB_CON_HDLC, val, 1);
962 /* reset fifo */
963 queue_control_request(hfc, HFCUSB_INC_RES_F, 2, 1);
964 /* set FIFO to receive register */
965 queue_control_request(hfc, HFCUSB_FIFO, idx_table[channel] + 1, 1);
966 queue_control_request(hfc, HFCUSB_CON_HDLC, val, 1);
967 /* reset fifo */
968 queue_control_request(hfc, HFCUSB_INC_RES_F, 2, 1);
969
970 val = 0x40;
971 if (hfc->b_mode[0])
972 val |= 1;
973 if (hfc->b_mode[1])
974 val |= 2;
975 queue_control_request(hfc, HFCUSB_SCTRL, val, 1);
976
977 val = 0;
978 if (hfc->b_mode[0])
979 val |= 1;
980 if (hfc->b_mode[1])
981 val |= 2;
982 queue_control_request(hfc, HFCUSB_SCTRL_R, val, 1);
983
984 if (mode == L1_MODE_NULL) {
985 if (channel)
986 handle_led(hfc, LED_B2_OFF);
987 else
988 handle_led(hfc, LED_B1_OFF);
989 } else {
990 if (channel)
991 handle_led(hfc, LED_B2_ON);
992 else
993 handle_led(hfc, LED_B1_ON);
994 }
995}
996
Adrian Bunk672c3fd2005-06-25 14:59:18 -0700997static void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998hfc_usb_l2l1(struct hisax_if *my_hisax_if, int pr, void *arg)
999{
1000 usb_fifo *fifo = my_hisax_if->priv;
1001 hfcusb_data *hfc = fifo->hfc;
1002
1003 switch (pr) {
1004 case PH_ACTIVATE | REQUEST:
1005 if (fifo->fifonum == HFCUSB_D_TX) {
Martin Bachemd6c59c12007-08-21 14:26:21 +02001006 DBG(HFCUSB_DBG_STATES,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007 "HFC_USB: hfc_usb_d_l2l1 D-chan: PH_ACTIVATE | REQUEST");
Martin Bachemd6c59c12007-08-21 14:26:21 +02001008
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009 if (hfc->l1_state != 3
1010 && hfc->l1_state != 7) {
1011 hfc->d_if.ifc.l1l2(&hfc->d_if.ifc,
1012 PH_DEACTIVATE |
1013 INDICATION,
1014 NULL);
Martin Bachemd6c59c12007-08-21 14:26:21 +02001015 DBG(HFCUSB_DBG_STATES,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016 "HFC-S USB: PH_DEACTIVATE | INDICATION sent (not state 3 or 7)");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017 } else {
1018 if (hfc->l1_state == 7) { /* l1 already active */
1019 hfc->d_if.ifc.l1l2(&hfc->
1020 d_if.
1021 ifc,
1022 PH_ACTIVATE
1023 |
1024 INDICATION,
1025 NULL);
Martin Bachemd6c59c12007-08-21 14:26:21 +02001026 DBG(HFCUSB_DBG_STATES,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027 "HFC-S USB: PH_ACTIVATE | INDICATION sent again ;)");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028 } else {
1029 /* force sending sending INFO1 */
1030 queue_control_request(hfc,
1031 HFCUSB_STATES,
1032 0x14,
1033 1);
1034 mdelay(1);
1035 /* start l1 activation */
1036 queue_control_request(hfc,
1037 HFCUSB_STATES,
1038 0x04,
1039 1);
1040 if (!timer_pending
1041 (&hfc->t3_timer)) {
1042 hfc->t3_timer.
1043 expires =
1044 jiffies +
1045 (HFC_TIMER_T3 *
1046 HZ) / 1000;
1047 add_timer(&hfc->
1048 t3_timer);
1049 }
1050 }
1051 }
1052 } else {
Martin Bachemd6c59c12007-08-21 14:26:21 +02001053 DBG(HFCUSB_DBG_STATES,
1054 "HFC_USB: hfc_usb_d_l2l1 B-chan: PH_ACTIVATE | REQUEST");
1055 setup_bchannel(hfc,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056 (fifo->fifonum ==
1057 HFCUSB_B1_TX) ? 0 : 1,
Alan Cox17a45062006-10-03 01:13:55 -07001058 (long) arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059 fifo->hif->l1l2(fifo->hif,
1060 PH_ACTIVATE | INDICATION,
1061 NULL);
1062 }
1063 break;
1064 case PH_DEACTIVATE | REQUEST:
1065 if (fifo->fifonum == HFCUSB_D_TX) {
Martin Bachemd6c59c12007-08-21 14:26:21 +02001066 DBG(HFCUSB_DBG_STATES,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067 "HFC_USB: hfc_usb_d_l2l1 D-chan: PH_DEACTIVATE | REQUEST");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068 } else {
Martin Bachemd6c59c12007-08-21 14:26:21 +02001069 DBG(HFCUSB_DBG_STATES,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070 "HFC_USB: hfc_usb_d_l2l1 Bx-chan: PH_DEACTIVATE | REQUEST");
Martin Bachemd6c59c12007-08-21 14:26:21 +02001071 setup_bchannel(hfc,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072 (fifo->fifonum ==
1073 HFCUSB_B1_TX) ? 0 : 1,
1074 (int) L1_MODE_NULL);
1075 fifo->hif->l1l2(fifo->hif,
1076 PH_DEACTIVATE | INDICATION,
1077 NULL);
1078 }
1079 break;
1080 case PH_DATA | REQUEST:
1081 if (fifo->skbuff && fifo->delete_flg) {
1082 dev_kfree_skb_any(fifo->skbuff);
1083 fifo->skbuff = NULL;
Martin Bachemd6c59c12007-08-21 14:26:21 +02001084 fifo->delete_flg = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085 }
1086 fifo->skbuff = arg; /* we have a new buffer */
1087 break;
1088 default:
Martin Bachemd6c59c12007-08-21 14:26:21 +02001089 DBG(HFCUSB_DBG_STATES,
1090 "HFC_USB: hfc_usb_d_l2l1: unkown state : %#x", pr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091 break;
1092 }
1093}
1094
Martin Bachemd6c59c12007-08-21 14:26:21 +02001095/* initial init HFC-S USB chip registers, HiSax interface, USB URBs */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096static int
Martin Bachemd6c59c12007-08-21 14:26:21 +02001097hfc_usb_init(hfcusb_data * hfc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098{
1099 usb_fifo *fifo;
1100 int i, err;
1101 u_char b;
1102 struct hisax_b_if *p_b_if[2];
1103
1104 /* check the chip id */
1105 if (read_usb(hfc, HFCUSB_CHIP_ID, &b) != 1) {
1106 printk(KERN_INFO "HFC-USB: cannot read chip id\n");
1107 return (1);
1108 }
1109 if (b != HFCUSB_CHIPID) {
1110 printk(KERN_INFO "HFC-S USB: Invalid chip id 0x%02x\n", b);
1111 return (1);
1112 }
1113
1114 /* first set the needed config, interface and alternate */
1115 err = usb_set_interface(hfc->dev, hfc->if_used, hfc->alt_used);
1116
1117 /* do Chip reset */
1118 write_usb(hfc, HFCUSB_CIRM, 8);
1119 /* aux = output, reset off */
1120 write_usb(hfc, HFCUSB_CIRM, 0x10);
1121
Martin Bachemd6c59c12007-08-21 14:26:21 +02001122 /* set USB_SIZE to match wMaxPacketSize for INT or BULK transfers */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123 write_usb(hfc, HFCUSB_USB_SIZE,
1124 (hfc->packet_size / 8) | ((hfc->packet_size / 8) << 4));
1125
Martin Bachemd6c59c12007-08-21 14:26:21 +02001126 /* set USB_SIZE_I to match wMaxPacketSize for ISO transfers */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127 write_usb(hfc, HFCUSB_USB_SIZE_I, hfc->iso_packet_size);
1128
1129 /* enable PCM/GCI master mode */
1130 write_usb(hfc, HFCUSB_MST_MODE1, 0); /* set default values */
1131 write_usb(hfc, HFCUSB_MST_MODE0, 1); /* enable master mode */
1132
1133 /* init the fifos */
1134 write_usb(hfc, HFCUSB_F_THRES,
1135 (HFCUSB_TX_THRESHOLD /
1136 8) | ((HFCUSB_RX_THRESHOLD / 8) << 4));
1137
1138 fifo = hfc->fifos;
1139 for (i = 0; i < HFCUSB_NUM_FIFOS; i++) {
1140 write_usb(hfc, HFCUSB_FIFO, i); /* select the desired fifo */
1141 fifo[i].skbuff = NULL; /* init buffer pointer */
1142 fifo[i].max_size =
1143 (i <= HFCUSB_B2_RX) ? MAX_BCH_SIZE : MAX_DFRAME_LEN;
1144 fifo[i].last_urblen = 0;
1145 /* set 2 bit for D- & E-channel */
1146 write_usb(hfc, HFCUSB_HDLC_PAR,
1147 ((i <= HFCUSB_B2_RX) ? 0 : 2));
1148 /* rx hdlc, enable IFF for D-channel */
1149 write_usb(hfc, HFCUSB_CON_HDLC,
1150 ((i == HFCUSB_D_TX) ? 0x09 : 0x08));
1151 write_usb(hfc, HFCUSB_INC_RES_F, 2); /* reset the fifo */
1152 }
1153
1154 write_usb(hfc, HFCUSB_CLKDEL, 0x0f); /* clock delay value */
1155 write_usb(hfc, HFCUSB_STATES, 3 | 0x10); /* set deactivated mode */
1156 write_usb(hfc, HFCUSB_STATES, 3); /* enable state machine */
1157
1158 write_usb(hfc, HFCUSB_SCTRL_R, 0); /* disable both B receivers */
1159 write_usb(hfc, HFCUSB_SCTRL, 0x40); /* disable B transmitters + capacitive mode */
1160
1161 /* set both B-channel to not connected */
1162 hfc->b_mode[0] = L1_MODE_NULL;
1163 hfc->b_mode[1] = L1_MODE_NULL;
1164
Martin Bachemd6c59c12007-08-21 14:26:21 +02001165 hfc->l1_activated = 0;
1166 hfc->disc_flag = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167 hfc->led_state = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168 hfc->old_led_state = 0;
1169
1170 /* init the t3 timer */
1171 init_timer(&hfc->t3_timer);
1172 hfc->t3_timer.data = (long) hfc;
1173 hfc->t3_timer.function = (void *) l1_timer_expire_t3;
1174
1175 /* init the t4 timer */
1176 init_timer(&hfc->t4_timer);
1177 hfc->t4_timer.data = (long) hfc;
1178 hfc->t4_timer.function = (void *) l1_timer_expire_t4;
1179
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180 /* init the background machinery for control requests */
1181 hfc->ctrl_read.bRequestType = 0xc0;
1182 hfc->ctrl_read.bRequest = 1;
Karsten Keilb05121b2006-03-06 15:42:41 -08001183 hfc->ctrl_read.wLength = cpu_to_le16(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184 hfc->ctrl_write.bRequestType = 0x40;
1185 hfc->ctrl_write.bRequest = 0;
1186 hfc->ctrl_write.wLength = 0;
1187 usb_fill_control_urb(hfc->ctrl_urb,
1188 hfc->dev,
1189 hfc->ctrl_out_pipe,
1190 (u_char *) & hfc->ctrl_write,
1191 NULL, 0, ctrl_complete, hfc);
1192 /* Init All Fifos */
1193 for (i = 0; i < HFCUSB_NUM_FIFOS; i++) {
1194 hfc->fifos[i].iso[0].purb = NULL;
1195 hfc->fifos[i].iso[1].purb = NULL;
1196 hfc->fifos[i].active = 0;
1197 }
1198 /* register Modul to upper Hisax Layers */
1199 hfc->d_if.owner = THIS_MODULE;
1200 hfc->d_if.ifc.priv = &hfc->fifos[HFCUSB_D_TX];
1201 hfc->d_if.ifc.l2l1 = hfc_usb_l2l1;
1202 for (i = 0; i < 2; i++) {
1203 hfc->b_if[i].ifc.priv = &hfc->fifos[HFCUSB_B1_TX + i * 2];
1204 hfc->b_if[i].ifc.l2l1 = hfc_usb_l2l1;
1205 p_b_if[i] = &hfc->b_if[i];
1206 }
1207 /* default Prot: EURO ISDN, should be a module_param */
1208 hfc->protocol = 2;
Alan Sternae2d9902007-05-23 13:58:12 -07001209 i = hisax_register(&hfc->d_if, p_b_if, "hfc_usb", hfc->protocol);
1210 if (i) {
1211 printk(KERN_INFO "HFC-S USB: hisax_register -> %d\n", i);
1212 return i;
1213 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214
1215#ifdef CONFIG_HISAX_DEBUG
1216 hfc_debug = debug;
1217#endif
1218
1219 for (i = 0; i < 4; i++)
1220 hfc->fifos[i].hif = &p_b_if[i / 2]->ifc;
1221 for (i = 4; i < 8; i++)
1222 hfc->fifos[i].hif = &hfc->d_if.ifc;
1223
1224 /* 3 (+1) INT IN + 3 ISO OUT */
1225 if (hfc->cfg_used == CNF_3INT3ISO || hfc->cfg_used == CNF_4INT3ISO) {
1226 start_int_fifo(hfc->fifos + HFCUSB_D_RX);
1227 if (hfc->fifos[HFCUSB_PCM_RX].pipe)
1228 start_int_fifo(hfc->fifos + HFCUSB_PCM_RX);
1229 start_int_fifo(hfc->fifos + HFCUSB_B1_RX);
1230 start_int_fifo(hfc->fifos + HFCUSB_B2_RX);
1231 }
1232 /* 3 (+1) ISO IN + 3 ISO OUT */
1233 if (hfc->cfg_used == CNF_3ISO3ISO || hfc->cfg_used == CNF_4ISO3ISO) {
1234 start_isoc_chain(hfc->fifos + HFCUSB_D_RX, ISOC_PACKETS_D,
1235 rx_iso_complete, 16);
1236 if (hfc->fifos[HFCUSB_PCM_RX].pipe)
1237 start_isoc_chain(hfc->fifos + HFCUSB_PCM_RX,
1238 ISOC_PACKETS_D, rx_iso_complete,
1239 16);
1240 start_isoc_chain(hfc->fifos + HFCUSB_B1_RX, ISOC_PACKETS_B,
1241 rx_iso_complete, 16);
1242 start_isoc_chain(hfc->fifos + HFCUSB_B2_RX, ISOC_PACKETS_B,
1243 rx_iso_complete, 16);
1244 }
1245
1246 start_isoc_chain(hfc->fifos + HFCUSB_D_TX, ISOC_PACKETS_D,
1247 tx_iso_complete, 1);
1248 start_isoc_chain(hfc->fifos + HFCUSB_B1_TX, ISOC_PACKETS_B,
1249 tx_iso_complete, 1);
1250 start_isoc_chain(hfc->fifos + HFCUSB_B2_TX, ISOC_PACKETS_B,
1251 tx_iso_complete, 1);
1252
1253 handle_led(hfc, LED_POWER_ON);
1254
1255 return (0);
Martin Bachemd6c59c12007-08-21 14:26:21 +02001256}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257
Martin Bachemd6c59c12007-08-21 14:26:21 +02001258/* initial callback for each plugged USB device */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259static int
1260hfc_usb_probe(struct usb_interface *intf, const struct usb_device_id *id)
1261{
1262 struct usb_device *dev = interface_to_usbdev(intf);
1263 hfcusb_data *context;
1264 struct usb_host_interface *iface = intf->cur_altsetting;
1265 struct usb_host_interface *iface_used = NULL;
1266 struct usb_host_endpoint *ep;
1267 int ifnum = iface->desc.bInterfaceNumber;
1268 int i, idx, alt_idx, probe_alt_setting, vend_idx, cfg_used, *vcf,
1269 attr, cfg_found, cidx, ep_addr;
1270 int cmptbl[16], small_match, iso_packet_size, packet_size,
1271 alt_used = 0;
Martin Bachem597a1072005-11-07 01:00:20 -08001272 hfcsusb_vdata *driver_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273
1274 vend_idx = 0xffff;
Martin Bachem597a1072005-11-07 01:00:20 -08001275 for (i = 0; hfcusb_idtab[i].idVendor; i++) {
Karsten Keilb05121b2006-03-06 15:42:41 -08001276 if ((le16_to_cpu(dev->descriptor.idVendor) == hfcusb_idtab[i].idVendor)
1277 && (le16_to_cpu(dev->descriptor.idProduct) == hfcusb_idtab[i].idProduct)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278 vend_idx = i;
Martin Bachem597a1072005-11-07 01:00:20 -08001279 continue;
1280 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281 }
Martin Bachem597a1072005-11-07 01:00:20 -08001282
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283 printk(KERN_INFO
1284 "HFC-S USB: probing interface(%d) actalt(%d) minor(%d)\n",
1285 ifnum, iface->desc.bAlternateSetting, intf->minor);
1286
1287 if (vend_idx != 0xffff) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288 /* if vendor and product ID is OK, start probing alternate settings */
1289 alt_idx = 0;
1290 small_match = 0xffff;
1291
1292 /* default settings */
1293 iso_packet_size = 16;
1294 packet_size = 64;
1295
1296 while (alt_idx < intf->num_altsetting) {
1297 iface = intf->altsetting + alt_idx;
1298 probe_alt_setting = iface->desc.bAlternateSetting;
1299 cfg_used = 0;
1300
1301 /* check for config EOL element */
1302 while (validconf[cfg_used][0]) {
Martin Bachemd6c59c12007-08-21 14:26:21 +02001303 cfg_found = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304 vcf = validconf[cfg_used];
1305 /* first endpoint descriptor */
1306 ep = iface->endpoint;
Martin Bachemd6c59c12007-08-21 14:26:21 +02001307
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308 memcpy(cmptbl, vcf, 16 * sizeof(int));
1309
1310 /* check for all endpoints in this alternate setting */
1311 for (i = 0; i < iface->desc.bNumEndpoints;
1312 i++) {
1313 ep_addr =
1314 ep->desc.bEndpointAddress;
1315 /* get endpoint base */
1316 idx = ((ep_addr & 0x7f) - 1) * 2;
1317 if (ep_addr & 0x80)
1318 idx++;
1319 attr = ep->desc.bmAttributes;
1320 if (cmptbl[idx] == EP_NUL) {
Martin Bachemd6c59c12007-08-21 14:26:21 +02001321 cfg_found = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322 }
1323 if (attr == USB_ENDPOINT_XFER_INT
1324 && cmptbl[idx] == EP_INT)
1325 cmptbl[idx] = EP_NUL;
1326 if (attr == USB_ENDPOINT_XFER_BULK
1327 && cmptbl[idx] == EP_BLK)
1328 cmptbl[idx] = EP_NUL;
1329 if (attr == USB_ENDPOINT_XFER_ISOC
1330 && cmptbl[idx] == EP_ISO)
1331 cmptbl[idx] = EP_NUL;
1332
1333 /* check if all INT endpoints match minimum interval */
Martin Bachemd6c59c12007-08-21 14:26:21 +02001334 if ((attr == USB_ENDPOINT_XFER_INT)
1335 && (ep->desc.bInterval < vcf[17])) {
1336 cfg_found = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337 }
1338 ep++;
1339 }
1340 for (i = 0; i < 16; i++) {
1341 /* all entries must be EP_NOP or EP_NUL for a valid config */
1342 if (cmptbl[i] != EP_NOP
1343 && cmptbl[i] != EP_NUL)
Martin Bachemd6c59c12007-08-21 14:26:21 +02001344 cfg_found = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345 }
1346 if (cfg_found) {
1347 if (cfg_used < small_match) {
1348 small_match = cfg_used;
1349 alt_used =
1350 probe_alt_setting;
1351 iface_used = iface;
1352 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353 }
1354 cfg_used++;
1355 }
1356 alt_idx++;
Martin Bachemd6c59c12007-08-21 14:26:21 +02001357 } /* (alt_idx < intf->num_altsetting) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358
1359 /* found a valid USB Ta Endpint config */
1360 if (small_match != 0xffff) {
1361 iface = iface_used;
Martin Bachemd6c59c12007-08-21 14:26:21 +02001362 if (!(context = kzalloc(sizeof(hfcusb_data), GFP_KERNEL)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363 return (-ENOMEM); /* got no mem */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364
1365 ep = iface->endpoint;
1366 vcf = validconf[small_match];
1367
1368 for (i = 0; i < iface->desc.bNumEndpoints; i++) {
1369 ep_addr = ep->desc.bEndpointAddress;
1370 /* get endpoint base */
1371 idx = ((ep_addr & 0x7f) - 1) * 2;
1372 if (ep_addr & 0x80)
1373 idx++;
1374 cidx = idx & 7;
1375 attr = ep->desc.bmAttributes;
1376
1377 /* init Endpoints */
1378 if (vcf[idx] != EP_NOP
1379 && vcf[idx] != EP_NUL) {
1380 switch (attr) {
1381 case USB_ENDPOINT_XFER_INT:
1382 context->
1383 fifos[cidx].
1384 pipe =
1385 usb_rcvintpipe
1386 (dev,
1387 ep->desc.
1388 bEndpointAddress);
1389 context->
1390 fifos[cidx].
1391 usb_transfer_mode
1392 = USB_INT;
1393 packet_size =
Karsten Keilb05121b2006-03-06 15:42:41 -08001394 le16_to_cpu(ep->desc.wMaxPacketSize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395 break;
1396 case USB_ENDPOINT_XFER_BULK:
1397 if (ep_addr & 0x80)
1398 context->
1399 fifos
1400 [cidx].
1401 pipe =
1402 usb_rcvbulkpipe
1403 (dev,
1404 ep->
1405 desc.
1406 bEndpointAddress);
1407 else
1408 context->
1409 fifos
1410 [cidx].
1411 pipe =
1412 usb_sndbulkpipe
1413 (dev,
1414 ep->
1415 desc.
1416 bEndpointAddress);
1417 context->
1418 fifos[cidx].
1419 usb_transfer_mode
1420 = USB_BULK;
1421 packet_size =
Karsten Keilb05121b2006-03-06 15:42:41 -08001422 le16_to_cpu(ep->desc.wMaxPacketSize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423 break;
1424 case USB_ENDPOINT_XFER_ISOC:
1425 if (ep_addr & 0x80)
1426 context->
1427 fifos
1428 [cidx].
1429 pipe =
1430 usb_rcvisocpipe
1431 (dev,
1432 ep->
1433 desc.
1434 bEndpointAddress);
1435 else
1436 context->
1437 fifos
1438 [cidx].
1439 pipe =
1440 usb_sndisocpipe
1441 (dev,
1442 ep->
1443 desc.
1444 bEndpointAddress);
1445 context->
1446 fifos[cidx].
1447 usb_transfer_mode
1448 = USB_ISOC;
1449 iso_packet_size =
Karsten Keilb05121b2006-03-06 15:42:41 -08001450 le16_to_cpu(ep->desc.wMaxPacketSize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451 break;
1452 default:
1453 context->
1454 fifos[cidx].
1455 pipe = 0;
1456 } /* switch attribute */
1457
1458 if (context->fifos[cidx].pipe) {
1459 context->fifos[cidx].
1460 fifonum = cidx;
1461 context->fifos[cidx].hfc =
1462 context;
Karsten Keilb05121b2006-03-06 15:42:41 -08001463 context->fifos[cidx].usb_packet_maxlen =
1464 le16_to_cpu(ep->desc.wMaxPacketSize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465 context->fifos[cidx].
1466 intervall =
1467 ep->desc.bInterval;
1468 context->fifos[cidx].
1469 skbuff = NULL;
1470 }
1471 }
1472 ep++;
1473 }
1474 context->dev = dev; /* save device */
1475 context->if_used = ifnum; /* save used interface */
1476 context->alt_used = alt_used; /* and alternate config */
1477 context->ctrl_paksize = dev->descriptor.bMaxPacketSize0; /* control size */
1478 context->cfg_used = vcf[16]; /* store used config */
1479 context->vend_idx = vend_idx; /* store found vendor */
1480 context->packet_size = packet_size;
1481 context->iso_packet_size = iso_packet_size;
1482
1483 /* create the control pipes needed for register access */
1484 context->ctrl_in_pipe =
1485 usb_rcvctrlpipe(context->dev, 0);
1486 context->ctrl_out_pipe =
1487 usb_sndctrlpipe(context->dev, 0);
1488 context->ctrl_urb = usb_alloc_urb(0, GFP_KERNEL);
1489
Martin Bachem597a1072005-11-07 01:00:20 -08001490 driver_info =
1491 (hfcsusb_vdata *) hfcusb_idtab[vend_idx].
1492 driver_info;
1493 printk(KERN_INFO "HFC-S USB: detected \"%s\"\n",
1494 driver_info->vend_name);
Martin Bachemd6c59c12007-08-21 14:26:21 +02001495
1496 DBG(HFCUSB_DBG_INIT,
1497 "HFC-S USB: Endpoint-Config: %s (if=%d alt=%d), E-Channel(%d)",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498 conf_str[small_match], context->if_used,
Martin Bachemd6c59c12007-08-21 14:26:21 +02001499 context->alt_used,
1500 validconf[small_match][18]);
1501
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502 /* init the chip and register the driver */
Martin Bachemd6c59c12007-08-21 14:26:21 +02001503 if (hfc_usb_init(context)) {
Alan Sternae2d9902007-05-23 13:58:12 -07001504 usb_kill_urb(context->ctrl_urb);
1505 usb_free_urb(context->ctrl_urb);
1506 context->ctrl_urb = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507 kfree(context);
1508 return (-EIO);
1509 }
1510 usb_set_intfdata(intf, context);
1511 return (0);
1512 }
1513 } else {
1514 printk(KERN_INFO
1515 "HFC-S USB: no valid vendor found in USB descriptor\n");
1516 }
1517 return (-EIO);
1518}
1519
Martin Bachemd6c59c12007-08-21 14:26:21 +02001520/* callback for unplugged USB device */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521static void
Martin Bachem7441dd12007-10-14 18:10:30 +02001522hfc_usb_disconnect(struct usb_interface *intf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523{
1524 hfcusb_data *context = usb_get_intfdata(intf);
1525 int i;
Martin Bachemd6c59c12007-08-21 14:26:21 +02001526
1527 handle_led(context, LED_POWER_OFF);
Martin Bachem7441dd12007-10-14 18:10:30 +02001528 schedule_timeout(HZ / 100);
Martin Bachemd6c59c12007-08-21 14:26:21 +02001529
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530 printk(KERN_INFO "HFC-S USB: device disconnect\n");
Martin Bachemd6c59c12007-08-21 14:26:21 +02001531 context->disc_flag = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532 usb_set_intfdata(intf, NULL);
Martin Bachem7441dd12007-10-14 18:10:30 +02001533
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534 if (timer_pending(&context->t3_timer))
1535 del_timer(&context->t3_timer);
1536 if (timer_pending(&context->t4_timer))
1537 del_timer(&context->t4_timer);
Martin Bachemd6c59c12007-08-21 14:26:21 +02001538
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539 /* tell all fifos to terminate */
1540 for (i = 0; i < HFCUSB_NUM_FIFOS; i++) {
1541 if (context->fifos[i].usb_transfer_mode == USB_ISOC) {
1542 if (context->fifos[i].active > 0) {
1543 stop_isoc_chain(&context->fifos[i]);
Martin Bachemd6c59c12007-08-21 14:26:21 +02001544 DBG(HFCUSB_DBG_INIT,
1545 "HFC-S USB: %s stopping ISOC chain Fifo(%i)",
Harvey Harrison156f1ed2008-04-28 02:14:40 -07001546 __func__, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547 }
1548 } else {
1549 if (context->fifos[i].active > 0) {
1550 context->fifos[i].active = 0;
Martin Bachemd6c59c12007-08-21 14:26:21 +02001551 DBG(HFCUSB_DBG_INIT,
1552 "HFC-S USB: %s unlinking URB for Fifo(%i)",
Harvey Harrison156f1ed2008-04-28 02:14:40 -07001553 __func__, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554 }
Alan Sternae2d9902007-05-23 13:58:12 -07001555 usb_kill_urb(context->fifos[i].urb);
1556 usb_free_urb(context->fifos[i].urb);
1557 context->fifos[i].urb = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558 }
1559 context->fifos[i].active = 0;
1560 }
Alan Sternae2d9902007-05-23 13:58:12 -07001561 usb_kill_urb(context->ctrl_urb);
1562 usb_free_urb(context->ctrl_urb);
1563 context->ctrl_urb = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564 hisax_unregister(&context->d_if);
1565 kfree(context); /* free our structure again */
Martin Bachemd6c59c12007-08-21 14:26:21 +02001566}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568static struct usb_driver hfc_drv = {
Martin Bachem597a1072005-11-07 01:00:20 -08001569 .name = "hfc_usb",
1570 .id_table = hfcusb_idtab,
1571 .probe = hfc_usb_probe,
1572 .disconnect = hfc_usb_disconnect,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573};
Martin Bachemd6c59c12007-08-21 14:26:21 +02001574
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575static void __exit
Martin Bachemd6c59c12007-08-21 14:26:21 +02001576hfc_usb_mod_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577{
Martin Bachemd6c59c12007-08-21 14:26:21 +02001578 usb_deregister(&hfc_drv); /* release our driver */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579 printk(KERN_INFO "HFC-S USB: module removed\n");
1580}
1581
1582static int __init
Martin Bachemd6c59c12007-08-21 14:26:21 +02001583hfc_usb_mod_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585 char revstr[30], datestr[30], dummy[30];
Martin Bachemd6c59c12007-08-21 14:26:21 +02001586#ifndef CONFIG_HISAX_DEBUG
1587 hfc_debug = debug;
1588#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589 sscanf(hfcusb_revision,
1590 "%s %s $ %s %s %s $ ", dummy, revstr,
1591 dummy, datestr, dummy);
1592 printk(KERN_INFO
1593 "HFC-S USB: driver module revision %s date %s loaded, (debug=%i)\n",
1594 revstr, datestr, debug);
1595 if (usb_register(&hfc_drv)) {
1596 printk(KERN_INFO
1597 "HFC-S USB: Unable to register HFC-S USB module at usb stack\n");
1598 return (-1); /* unable to register */
1599 }
1600 return (0);
1601}
1602
Martin Bachemd6c59c12007-08-21 14:26:21 +02001603module_init(hfc_usb_mod_init);
1604module_exit(hfc_usb_mod_exit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605MODULE_AUTHOR(DRIVER_AUTHOR);
1606MODULE_DESCRIPTION(DRIVER_DESC);
1607MODULE_LICENSE("GPL");
Martin Bachem597a1072005-11-07 01:00:20 -08001608MODULE_DEVICE_TABLE(usb, hfcusb_idtab);