1 /*
2  * hfcsusb.h, HFC-S USB mISDN driver
3  */
4 
5 #ifndef __HFCSUSB_H__
6 #define __HFCSUSB_H__
7 
8 
9 #define DRIVER_NAME "HFC-S_USB"
10 
11 #define DBG_HFC_CALL_TRACE	0x00010000
12 #define DBG_HFC_FIFO_VERBOSE	0x00020000
13 #define DBG_HFC_USB_VERBOSE	0x00100000
14 #define DBG_HFC_URB_INFO	0x00200000
15 #define DBG_HFC_URB_ERROR	0x00400000
16 
17 #define DEFAULT_TRANSP_BURST_SZ 128
18 
19 #define HFC_CTRL_TIMEOUT	20	/* 5ms timeout writing/reading regs */
20 #define CLKDEL_TE		0x0f	/* CLKDEL in TE mode */
21 #define CLKDEL_NT		0x6c	/* CLKDEL in NT mode */
22 
23 /* hfcsusb Layer1 commands */
24 #define HFC_L1_ACTIVATE_TE		1
25 #define HFC_L1_ACTIVATE_NT		2
26 #define HFC_L1_DEACTIVATE_NT		3
27 #define HFC_L1_FORCE_DEACTIVATE_TE	4
28 
29 /* cmd FLAGS in HFCUSB_STATES register */
30 #define HFCUSB_LOAD_STATE	0x10
31 #define HFCUSB_ACTIVATE		0x20
32 #define HFCUSB_DO_ACTION	0x40
33 #define HFCUSB_NT_G2_G3		0x80
34 
35 /* timers */
36 #define NT_ACTIVATION_TIMER	0x01	/* enables NT mode activation Timer */
37 #define NT_T1_COUNT		10
38 
39 #define MAX_BCH_SIZE		2048	/* allowed B-channel packet size */
40 
41 #define HFCUSB_RX_THRESHOLD	64	/* threshold for fifo report bit rx */
42 #define HFCUSB_TX_THRESHOLD	96	/* threshold for fifo report bit tx */
43 
44 #define HFCUSB_CHIP_ID		0x16	/* Chip ID register index */
45 #define HFCUSB_CIRM		0x00	/* cirm register index */
46 #define HFCUSB_USB_SIZE		0x07	/* int length register */
47 #define HFCUSB_USB_SIZE_I	0x06	/* iso length register */
48 #define HFCUSB_F_CROSS		0x0b	/* bit order register */
49 #define HFCUSB_CLKDEL		0x37	/* bit delay register */
50 #define HFCUSB_CON_HDLC		0xfa	/* channel connect register */
51 #define HFCUSB_HDLC_PAR		0xfb
52 #define HFCUSB_SCTRL		0x31	/* S-bus control register (tx) */
53 #define HFCUSB_SCTRL_E		0x32	/* same for E and special funcs */
54 #define HFCUSB_SCTRL_R		0x33	/* S-bus control register (rx) */
55 #define HFCUSB_F_THRES		0x0c	/* threshold register */
56 #define HFCUSB_FIFO		0x0f	/* fifo select register */
57 #define HFCUSB_F_USAGE		0x1a	/* fifo usage register */
58 #define HFCUSB_MST_MODE0	0x14
59 #define HFCUSB_MST_MODE1	0x15
60 #define HFCUSB_P_DATA		0x1f
61 #define HFCUSB_INC_RES_F	0x0e
62 #define HFCUSB_B1_SSL		0x20
63 #define HFCUSB_B2_SSL		0x21
64 #define HFCUSB_B1_RSL		0x24
65 #define HFCUSB_B2_RSL		0x25
66 #define HFCUSB_STATES		0x30
67 
68 
69 #define HFCUSB_CHIPID		0x40	/* ID value of HFC-S USB */
70 
71 /* fifo registers */
72 #define HFCUSB_NUM_FIFOS	8	/* maximum number of fifos */
73 #define HFCUSB_B1_TX		0	/* index for B1 transmit bulk/int */
74 #define HFCUSB_B1_RX		1	/* index for B1 receive bulk/int */
75 #define HFCUSB_B2_TX		2
76 #define HFCUSB_B2_RX		3
77 #define HFCUSB_D_TX		4
78 #define HFCUSB_D_RX		5
79 #define HFCUSB_PCM_TX		6
80 #define HFCUSB_PCM_RX		7
81 
82 
83 #define USB_INT		0
84 #define USB_BULK	1
85 #define USB_ISOC	2
86 
87 #define ISOC_PACKETS_D	8
88 #define ISOC_PACKETS_B	8
89 #define ISO_BUFFER_SIZE	128
90 
91 /* defines how much ISO packets are handled in one URB */
92 static int iso_packets[8] =
93 { ISOC_PACKETS_B, ISOC_PACKETS_B, ISOC_PACKETS_B, ISOC_PACKETS_B,
94   ISOC_PACKETS_D, ISOC_PACKETS_D, ISOC_PACKETS_D, ISOC_PACKETS_D
95 };
96 
97 
98 /* Fifo flow Control for TX ISO */
99 #define SINK_MAX	68
100 #define SINK_MIN	48
101 #define SINK_DMIN	12
102 #define SINK_DMAX	18
103 #define BITLINE_INF	(-96 * 8)
104 
105 /* HFC-S USB register access by Control-URSs */
106 #define write_reg_atomic(a, b, c)					\
107 	usb_control_msg((a)->dev, (a)->ctrl_out_pipe, 0, 0x40, (c), (b), \
108 			0, 0, HFC_CTRL_TIMEOUT)
109 #define read_reg_atomic(a, b, c)					\
110 	usb_control_msg((a)->dev, (a)->ctrl_in_pipe, 1, 0xC0, 0, (b), (c), \
111 			1, HFC_CTRL_TIMEOUT)
112 #define HFC_CTRL_BUFSIZE 64
113 
114 struct ctrl_buf {
115 	__u8 hfcs_reg;		/* register number */
116 	__u8 reg_val;		/* value to be written (or read) */
117 };
118 
119 /*
120  * URB error codes
121  * Used to represent a list of values and their respective symbolic names
122  */
123 struct hfcusb_symbolic_list {
124 	const int num;
125 	const char *name;
126 };
127 
128 static struct hfcusb_symbolic_list urb_errlist[] = {
129 	{-ENOMEM, "No memory for allocation of internal structures"},
130 	{-ENOSPC, "The host controller's bandwidth is already consumed"},
131 	{-ENOENT, "URB was canceled by unlink_urb"},
132 	{-EXDEV, "ISO transfer only partially completed"},
133 	{-EAGAIN, "Too match scheduled for the future"},
134 	{-ENXIO, "URB already queued"},
135 	{-EFBIG, "Too much ISO frames requested"},
136 	{-ENOSR, "Buffer error (overrun)"},
137 	{-EPIPE, "Specified endpoint is stalled (device not responding)"},
138 	{-EOVERFLOW, "Babble (bad cable?)"},
139 	{-EPROTO, "Bit-stuff error (bad cable?)"},
140 	{-EILSEQ, "CRC/Timeout"},
141 	{-ETIMEDOUT, "NAK (device does not respond)"},
142 	{-ESHUTDOWN, "Device unplugged"},
143 	{-1, NULL}
144 };
145 
146 static inline const char *
147 symbolic(struct hfcusb_symbolic_list list[], const int num)
148 {
149 	int i;
150 	for (i = 0; list[i].name != NULL; i++)
151 		if (list[i].num == num)
152 			return list[i].name;
153 	return "<unknown USB Error>";
154 }
155 
156 /* USB descriptor need to contain one of the following EndPoint combination: */
157 #define CNF_4INT3ISO	1	/* 4 INT IN, 3 ISO OUT */
158 #define CNF_3INT3ISO	2	/* 3 INT IN, 3 ISO OUT */
159 #define CNF_4ISO3ISO	3	/* 4 ISO IN, 3 ISO OUT */
160 #define CNF_3ISO3ISO	4	/* 3 ISO IN, 3 ISO OUT */
161 
162 #define EP_NUL 1	/* Endpoint at this position not allowed */
163 #define EP_NOP 2	/* all type of endpoints allowed at this position */
164 #define EP_ISO 3	/* Isochron endpoint mandatory at this position */
165 #define EP_BLK 4	/* Bulk endpoint mandatory at this position */
166 #define EP_INT 5	/* Interrupt endpoint mandatory at this position */
167 
168 #define HFC_CHAN_B1	0
169 #define HFC_CHAN_B2	1
170 #define HFC_CHAN_D	2
171 #define HFC_CHAN_E	3
172 
173 
174 /*
175  * List of all supported enpoints configiration sets, used to find the
176  * best matching endpoint configuration within a devices' USB descriptor.
177  * We need at least 3 RX endpoints, and 3 TX endpoints, either
178  * INT-in and ISO-out, or ISO-in and ISO-out)
179  * with 4 RX endpoints even E-Channel logging is possible
180  */
181 static int
182 validconf[][19] = {
183 	/* INT in, ISO out config */
184 	{EP_NUL, EP_INT, EP_NUL, EP_INT, EP_NUL, EP_INT, EP_NOP, EP_INT,
185 	 EP_ISO, EP_NUL, EP_ISO, EP_NUL, EP_ISO, EP_NUL, EP_NUL, EP_NUL,
186 	 CNF_4INT3ISO, 2, 1},
187 	{EP_NUL, EP_INT, EP_NUL, EP_INT, EP_NUL, EP_INT, EP_NUL, EP_NUL,
188 	 EP_ISO, EP_NUL, EP_ISO, EP_NUL, EP_ISO, EP_NUL, EP_NUL, EP_NUL,
189 	 CNF_3INT3ISO, 2, 0},
190 	/* ISO in, ISO out config */
191 	{EP_NOP, EP_NOP, EP_NOP, EP_NOP, EP_NOP, EP_NOP, EP_NOP, EP_NOP,
192 	 EP_ISO, EP_ISO, EP_ISO, EP_ISO, EP_ISO, EP_ISO, EP_NOP, EP_ISO,
193 	 CNF_4ISO3ISO, 2, 1},
194 	{EP_NUL, EP_NUL, EP_NUL, EP_NUL, EP_NUL, EP_NUL, EP_NUL, EP_NUL,
195 	 EP_ISO, EP_ISO, EP_ISO, EP_ISO, EP_ISO, EP_ISO, EP_NUL, EP_NUL,
196 	 CNF_3ISO3ISO, 2, 0},
197 	{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} /* EOL element */
198 };
199 
200 /* string description of chosen config */
201 static char *conf_str[] = {
202 	"4 Interrupt IN + 3 Isochron OUT",
203 	"3 Interrupt IN + 3 Isochron OUT",
204 	"4 Isochron IN + 3 Isochron OUT",
205 	"3 Isochron IN + 3 Isochron OUT"
206 };
207 
208 
209 #define LED_OFF		0	/* no LED support */
210 #define LED_SCHEME1	1	/* LED standard scheme */
211 #define LED_SCHEME2	2	/* not used yet... */
212 
213 #define LED_POWER_ON	1
214 #define LED_POWER_OFF	2
215 #define LED_S0_ON	3
216 #define LED_S0_OFF	4
217 #define LED_B1_ON	5
218 #define LED_B1_OFF	6
219 #define LED_B1_DATA	7
220 #define LED_B2_ON	8
221 #define LED_B2_OFF	9
222 #define LED_B2_DATA	10
223 
224 #define LED_NORMAL	0	/* LEDs are normal */
225 #define LED_INVERTED	1	/* LEDs are inverted */
226 
227 /* time in ms to perform a Flashing LED when B-Channel has traffic */
228 #define LED_TIME      250
229 
230 
231 
232 struct hfcsusb;
233 struct usb_fifo;
234 
235 /* structure defining input+output fifos (interrupt/bulk mode) */
236 struct iso_urb {
237 	struct urb *urb;
238 	__u8 buffer[ISO_BUFFER_SIZE];	/* buffer rx/tx USB URB data */
239 	struct usb_fifo *owner_fifo;	/* pointer to owner fifo */
240 	__u8 indx; /* Fifos's ISO double buffer 0 or 1 ? */
241 #ifdef ISO_FRAME_START_DEBUG
242 	int start_frames[ISO_FRAME_START_RING_COUNT];
243 	__u8 iso_frm_strt_pos; /* index in start_frame[] */
244 #endif
245 };
246 
247 struct usb_fifo {
248 	int fifonum;		/* fifo index attached to this structure */
249 	int active;		/* fifo is currently active */
250 	struct hfcsusb *hw;	/* pointer to main structure */
251 	int pipe;		/* address of endpoint */
252 	__u8 usb_packet_maxlen;	/* maximum length for usb transfer */
253 	unsigned int max_size;	/* maximum size of receive/send packet */
254 	__u8 intervall;		/* interrupt interval */
255 	struct urb *urb;	/* transfer structure for usb routines */
256 	__u8 buffer[128];	/* buffer USB INT OUT URB data */
257 	int bit_line;		/* how much bits are in the fifo? */
258 
259 	__u8 usb_transfer_mode; /* switched between ISO and INT */
260 	struct iso_urb	iso[2]; /* two urbs to have one always
261 				   one pending */
262 
263 	struct dchannel *dch;	/* link to hfcsusb_t->dch */
264 	struct bchannel *bch;	/* link to hfcsusb_t->bch */
265 	struct dchannel *ech;	/* link to hfcsusb_t->ech, TODO: E-CHANNEL */
266 	int last_urblen;	/* remember length of last packet */
267 	__u8 stop_gracefull;	/* stops URB retransmission */
268 };
269 
270 struct hfcsusb {
271 	struct list_head	list;
272 	struct dchannel		dch;
273 	struct bchannel		bch[2];
274 	struct dchannel		ech; /* TODO : wait for struct echannel ;) */
275 
276 	struct usb_device	*dev;		/* our device */
277 	struct usb_interface	*intf;		/* used interface */
278 	int			if_used;	/* used interface number */
279 	int			alt_used;	/* used alternate config */
280 	int			cfg_used;	/* configuration index used */
281 	int			vend_idx;	/* index in hfcsusb_idtab */
282 	int			packet_size;
283 	int			iso_packet_size;
284 	struct usb_fifo		fifos[HFCUSB_NUM_FIFOS];
285 
286 	/* control pipe background handling */
287 	struct ctrl_buf		ctrl_buff[HFC_CTRL_BUFSIZE];
288 	int			ctrl_in_idx, ctrl_out_idx, ctrl_cnt;
289 	struct urb		*ctrl_urb;
290 	struct usb_ctrlrequest	ctrl_write;
291 	struct usb_ctrlrequest	ctrl_read;
292 	int			ctrl_paksize;
293 	int			ctrl_in_pipe, ctrl_out_pipe;
294 	spinlock_t		ctrl_lock; /* lock for ctrl */
295 	spinlock_t              lock;
296 
297 	__u8			threshold_mask;
298 	__u8			led_state;
299 
300 	__u8			protocol;
301 	int			nt_timer;
302 	int			open;
303 	__u8			timers;
304 	__u8			initdone;
305 	char			name[MISDN_MAX_IDLEN];
306 };
307 
308 /* private vendor specific data */
309 struct hfcsusb_vdata {
310 	__u8		led_scheme;  /* led display scheme */
311 	signed short	led_bits[8]; /* array of 8 possible LED bitmask */
312 	char		*vend_name;  /* device name */
313 };
314 
315 
316 #define HFC_MAX_TE_LAYER1_STATE 8
317 #define HFC_MAX_NT_LAYER1_STATE 4
318 
319 static const char *HFC_TE_LAYER1_STATES[HFC_MAX_TE_LAYER1_STATE + 1] = {
320 	"TE F0 - Reset",
321 	"TE F1 - Reset",
322 	"TE F2 - Sensing",
323 	"TE F3 - Deactivated",
324 	"TE F4 - Awaiting signal",
325 	"TE F5 - Identifying input",
326 	"TE F6 - Synchronized",
327 	"TE F7 - Activated",
328 	"TE F8 - Lost framing",
329 };
330 
331 static const char *HFC_NT_LAYER1_STATES[HFC_MAX_NT_LAYER1_STATE + 1] = {
332 	"NT G0 - Reset",
333 	"NT G1 - Deactive",
334 	"NT G2 - Pending activation",
335 	"NT G3 - Active",
336 	"NT G4 - Pending deactivation",
337 };
338 
339 /* supported devices */
340 static struct usb_device_id hfcsusb_idtab[] = {
341 	{
342 		USB_DEVICE(0x0959, 0x2bd0),
343 		.driver_info = (unsigned long) &((struct hfcsusb_vdata)
344 			{LED_OFF, {4, 0, 2, 1},
345 					"ISDN USB TA (Cologne Chip HFC-S USB based)"}),
346 	},
347 	{
348 		USB_DEVICE(0x0675, 0x1688),
349 		.driver_info = (unsigned long) &((struct hfcsusb_vdata)
350 			{LED_SCHEME1, {1, 2, 0, 0},
351 					"DrayTek miniVigor 128 USB ISDN TA"}),
352 	},
353 	{
354 		USB_DEVICE(0x07b0, 0x0007),
355 		.driver_info = (unsigned long) &((struct hfcsusb_vdata)
356 			{LED_SCHEME1, {0x80, -64, -32, -16},
357 					"Billion tiny USB ISDN TA 128"}),
358 	},
359 	{
360 		USB_DEVICE(0x0742, 0x2008),
361 		.driver_info = (unsigned long) &((struct hfcsusb_vdata)
362 			{LED_SCHEME1, {4, 0, 2, 1},
363 					"Stollmann USB TA"}),
364 	},
365 	{
366 		USB_DEVICE(0x0742, 0x2009),
367 		.driver_info = (unsigned long) &((struct hfcsusb_vdata)
368 			{LED_SCHEME1, {4, 0, 2, 1},
369 					"Aceex USB ISDN TA"}),
370 	},
371 	{
372 		USB_DEVICE(0x0742, 0x200A),
373 		.driver_info = (unsigned long) &((struct hfcsusb_vdata)
374 			{LED_SCHEME1, {4, 0, 2, 1},
375 					"OEM USB ISDN TA"}),
376 	},
377 	{
378 		USB_DEVICE(0x08e3, 0x0301),
379 		.driver_info = (unsigned long) &((struct hfcsusb_vdata)
380 			{LED_SCHEME1, {2, 0, 1, 4},
381 					"Olitec USB RNIS"}),
382 	},
383 	{
384 		USB_DEVICE(0x07fa, 0x0846),
385 		.driver_info = (unsigned long) &((struct hfcsusb_vdata)
386 			{LED_SCHEME1, {0x80, -64, -32, -16},
387 					"Bewan Modem RNIS USB"}),
388 	},
389 	{
390 		USB_DEVICE(0x07fa, 0x0847),
391 		.driver_info = (unsigned long) &((struct hfcsusb_vdata)
392 			{LED_SCHEME1, {0x80, -64, -32, -16},
393 					"Djinn Numeris USB"}),
394 	},
395 	{
396 		USB_DEVICE(0x07b0, 0x0006),
397 		.driver_info = (unsigned long) &((struct hfcsusb_vdata)
398 			{LED_SCHEME1, {0x80, -64, -32, -16},
399 					"Twister ISDN TA"}),
400 	},
401 	{
402 		USB_DEVICE(0x071d, 0x1005),
403 		.driver_info = (unsigned long) &((struct hfcsusb_vdata)
404 			{LED_SCHEME1, {0x02, 0, 0x01, 0x04},
405 					"Eicon DIVA USB 4.0"}),
406 	},
407 	{
408 		USB_DEVICE(0x0586, 0x0102),
409 		.driver_info = (unsigned long) &((struct hfcsusb_vdata)
410 			{LED_SCHEME1, {0x88, -64, -32, -16},
411 					"ZyXEL OMNI.NET USB II"}),
412 	},
413 	{
414 		USB_DEVICE(0x1ae7, 0x0525),
415 		.driver_info = (unsigned long) &((struct hfcsusb_vdata)
416 			{LED_SCHEME1, {0x88, -64, -32, -16},
417 					"X-Tensions USB ISDN TA XC-525"}),
418 	},
419 	{ }
420 };
421 
422 MODULE_DEVICE_TABLE(usb, hfcsusb_idtab);
423 
424 #endif	/* __HFCSUSB_H__ */
425