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