Debian Codegraph @

drivers/dahdi/icE1usb/icE1usb.c

raw symbols SCIP
1 // SPDX-License-Identifier: GPL-2.0-or-later
2
3 /* Osmocom icE1usb driver
4 * Copyright (C) 2020-2022 by Harald Welte <[email protected]>
5 * inspired by osmo-e1d by Sylvain Munaut */
6
7 /* The Osmocom icE1usb is a modern, software-defined open hardware and
8 open source firmware design for a USB E1 interface. You can find more
9 information at https://osmocom.org/projects/e1-t1-adapter/wiki/IcE1usb
10
11 The hardware design can be found at https://git.osmocom.org/osmo-e1-hardware
12
13 The FPGA gateware and associated embedded firmware is hosted in the same
14 git repository. Some parts are in submodules (be sure to use recursive
15 clone)
16
17 This DAHDI driver allows the use of the icE1usb just like any other
18 E1/PRI interface device supported by DAHDI. When using this DAHDI
19 driver, osmo-e1d most not be used. The DAHDI driver is a replacement /
20 alternative for osmo-e1d.
21
22 */
23
24 #define DEBUG
25
26 #include <linux/module.h>
27 #include <linux/init.h>
28 #include <linux/usb.h>
29 #include <linux/slab.h>
30 #include <linux/kernel.h>
31 #include <linux/spinlock.h>
32 #include <linux/pm_runtime.h>
33
34 #include <dahdi/kernel.h>
35
36 #include "ice1usb_proto.h"
37
38 #define VERSION "0.2"
39
40 /* number of isochronous frames per URB */
41 #define ICE1USB_MAX_ISOC_FRAMES 4
42
43 /* number of URBs per endpoint */
44 #define ICE1USB_NUM_URBS 4
45
46 #define ieu_dbg(x, fmt, args ...) \
47 dev_dbg(&((x)->usb_intf->dev), fmt, ## args)
48 #define ieu_info(x, fmt, args ...) \
49 dev_info(&((x)->usb_intf->dev), fmt, ## args)
50 #define ieu_warn(x, fmt, args ...) \
51 dev_warn(&((x)->usb_intf->dev), fmt, ## args)
52 #define ieu_err(x, fmt, args ...) \
53 dev_err(&((x)->usb_intf->dev), fmt, ## args)
54
55 static const struct usb_device_id ice1usb_products[] = {
56 {
57 USB_DEVICE(0x1d50, 0x6145),
58 },
59 {}
60 };
61
62 /***********************************************************************
63 * data structures
64 ***********************************************************************/
65
66 /* per-device global state */
67 struct ice1usb_gpsdo {
68 /* USB device we operate on */
69 struct usb_device *usb_dev;
70 struct usb_interface *usb_intf;
71
72 struct e1usb_gpsdo_status gpsdo_status;
73 char fw_build[128];
74
75 /* is the device still present (true) or already absent/unplugged (false) */
76 bool present;
77 /* spinlock protecting concurrent access to fc, {read,write}chunk_idx, ... */
78 spinlock_t lock;
79 };
80
81
82 enum ice1usb_flags {
83 ICE1USB_ISOC_RUNNING,
84 ICE1USB_IRQ_RUNNING,
85 };
86
87 /* per-interface state */
88 struct ice1usb {
89 /* USB device and interface we operate on */
90 struct usb_device *usb_dev;
91 struct usb_interface *usb_intf;
92 /* altsetting for 'OFF' state */
93 const struct usb_host_interface *alt_off;
94 /* altsetting for 'ON' state */
95 const struct usb_host_interface *alt_on;
96 struct {
97 struct usb_anchor iso_in;
98 struct usb_anchor iso_out;
99 struct usb_anchor iso_fb;
100 struct usb_anchor irq;
101 } anchor;
102 /* USB endpoint numbers */
103 struct {
104 const struct usb_endpoint_descriptor *iso_in;
105 const struct usb_endpoint_descriptor *iso_out;
106 const struct usb_endpoint_descriptor *iso_fb;
107 const struct usb_endpoint_descriptor *irq;
108 } ep;
109 struct {
110 struct ice1usb_tx_config tx;
111 struct ice1usb_rx_config rx;
112 } cfg;
113 /* last received error interrupt */
114 struct ice1usb_irq_err last_err;
115 struct {
116 uint32_t ovfl;
117 uint32_t unfl;
118 } count;
119 /* enum ice1usb_flags */
120 unsigned long flags;
121 /* feedback flow-control */
122 struct {
123 uint32_t r_acc;
124 uint32_t r_sw;
125 } fc;
126 /* DAHDI driver related bits */
127 struct {
128 struct dahdi_device *dev;
129 struct dahdi_span span;
130 struct dahdi_chan *chans[31];
131 /* [next rx byte] offset into chan[i]->readchunk */
132 unsigned int readchunk_idx;
133 /* [next tx byte] offset into chan[i]->writechunk */
134 unsigned int writechunk_idx;
135 } dahdi;
136 /* is the device still present (true) or already absent/unplugged (false) */
137 bool present;
138 /* spinlock protecting concurrent access to fc, {read,write}chunk_idx, ... */
139 spinlock_t lock;
140 /* maximum number of 32-byte E1 frames to send in one ISO OUT packet */
141 unsigned int max_fts;
142 };
143
144 static void ice1usb_free(struct ice1usb *ieu)
145 {
146 usb_put_dev(ieu->usb_dev);
147 kfree(ieu);
148 }
149
150 static void e1u_free_channels(struct ice1usb *ieu)
151 {
152 unsigned int i;
153
154 for (i = 0; i < ARRAY_SIZE(ieu->dahdi.chans); i++) {
155 kfree(ieu->dahdi.chans[i]);
156 ieu->dahdi.chans[i] = NULL;
157 }
158 }
159
160 static int e1u_alloc_channels(struct ice1usb *ieu)
161 {
162 unsigned int i;
163
164 for (i = 0; i < ARRAY_SIZE(ieu->dahdi.chans); i++) {
165 struct dahdi_chan *chan;
166 kfree(ieu->dahdi.chans[i]);
167 chan = ieu->dahdi.chans[i] = kzalloc(sizeof(*chan), GFP_KERNEL);
168 if (!chan) {
169 e1u_free_channels(ieu);
170 return -ENOMEM;
171 }
172
173 chan->pvt = ieu;
174 snprintf(chan->name, sizeof(chan->name)-1, "%s/%d",
175 ieu->dahdi.span.name, i+1);
176 chan->chanpos = i+1;
177 chan->sigcap = DAHDI_SIG_CLEAR | DAHDI_SIG_MTP2 | DAHDI_SIG_SF;
178 }
179 return 0;
180 }
181
182 static const char *tx_mode_str(enum ice1usb_tx_mode tx_mode)
183 {
184 switch (tx_mode) {
185 case ICE1USB_TX_MODE_TRANSP:
186 return "TRANSPARENT";
187 case ICE1USB_TX_MODE_TS0:
188 return "TS0";
189 case ICE1USB_TX_MODE_TS0_CRC4:
190 return "TS0_CRC4";
191 case ICE1USB_TX_MODE_TS0_CRC4_E:
192 return "TS0_CRC4_E";
193 default:
194 return "unknown";
195 }
196 }
197
198 static const char *tx_timing_str(enum ice1usb_tx_timing tx_timing)
199 {
200 switch (tx_timing) {
201 case ICE1USB_TX_TIME_SRC_LOCAL:
202 return "LOCAL";
203 case ICE1USB_TX_TIME_SRC_REMOTE:
204 return "REMOTE";
205 default:
206 return "unknown";
207 }
208 }
209
210 static const char *tx_ext_loopback_str(enum ice1usb_tx_ext_loopback ext_lb)
211 {
212 switch (ext_lb) {
213 case ICE1USB_TX_EXT_LOOPBACK_OFF:
214 return "OFF";
215 case ICE1USB_TX_EXT_LOOPBACK_SAME:
216 return "SAME";
217 case ICE1USB_TX_EXT_LOOPBACK_CROSS:
218 return "CROSS";
219 default:
220 return "unknown";
221 }
222 }
223
224 static const char *rx_mode_str(enum ice1usb_rx_mode rx_mode)
225 {
226 switch (rx_mode) {
227 case ICE1USB_RX_MODE_FRAME:
228 return "FRAME";
229 case ICE1USB_RX_MODE_MULTIFRAME:
230 return "MULTIFRAME";
231 default:
232 return "unknown";
233 }
234 }
235
236
237 #define USB_RT_VEND_IF (USB_TYPE_VENDOR | USB_RECIP_INTERFACE)
238 #define USB_RT_VEND_DEV (USB_TYPE_VENDOR | USB_RECIP_DEVICE)
239
240 /* synchronous requests, may block up to 1s, only called from process context! */
241 static int ice1usb_tx_config(struct ice1usb *ieu)
242 {
243 int rc;
244 uint8_t if_num = ieu->usb_intf->cur_altsetting->desc.bInterfaceNumber;
245
246 ieu_info(ieu, "TX-CONFIG (crc4=%s, timing=%s, ext_loopback=%s, tx_yellow_alarm=%u)\n",
247 tx_mode_str(ieu->cfg.tx.mode), tx_timing_str(ieu->cfg.tx.timing),
248 tx_ext_loopback_str(ieu->cfg.tx.ext_loopback), ieu->cfg.tx.alarm);
249
250 rc = usb_control_msg(ieu->usb_dev, usb_sndctrlpipe(ieu->usb_dev, 0),
251 ICE1USB_INTF_SET_TX_CFG, USB_RT_VEND_IF,
252 0, if_num, &ieu->cfg.tx, sizeof(ieu->cfg.tx),
253 USB_CTRL_SET_TIMEOUT);
254 if (rc < 0)
255 return rc;
256 if (rc != sizeof(ieu->cfg.tx))
257 return -EIO;
258 return 0;
259 }
260
261 static int ice1usb_rx_config(struct ice1usb *ieu)
262 {
263 int rc;
264 uint8_t if_num = ieu->usb_intf->cur_altsetting->desc.bInterfaceNumber;
265
266 ieu_info(ieu, "RX-CONFIG (mode=%s)\n",
267 rx_mode_str(ieu->cfg.rx.mode));
268
269 rc = usb_control_msg(ieu->usb_dev, usb_sndctrlpipe(ieu->usb_dev, 0),
270 ICE1USB_INTF_SET_RX_CFG, USB_RT_VEND_IF,
271 0, if_num, &ieu->cfg.rx, sizeof(ieu->cfg.rx),
272 USB_CTRL_SET_TIMEOUT);
273 if (rc < 0)
274 return rc;
275 if (rc != sizeof(ieu->cfg.rx))
276 return -EIO;
277 return 0;
278 }
279
280
281 /***********************************************************************
282 * ISOCHRONOUS transfers
283 ***********************************************************************/
284
285 static inline void __fill_isoc_descriptor(struct urb *urb, unsigned int max_pack_size)
286 {
287 unsigned int i, offset = 0;
288
289 for (i = 0; i < ICE1USB_MAX_ISOC_FRAMES; i++) {
290 urb->iso_frame_desc[i].offset = offset;
291 urb->iso_frame_desc[i].length = max_pack_size;
292 offset += max_pack_size;
293 };
294 urb->number_of_packets = i;
295 }
296
297 /* allocate + submit an isochronous URB for given EP; anchor it */
298 static int ice1usb_submit_isoc_urb(struct ice1usb *ieu,
299 const struct usb_endpoint_descriptor *ep,
300 struct usb_anchor *anchor,
301 usb_complete_t compl, gfp_t mem_flags)
302 {
303 unsigned int max_pack_size = le16_to_cpu(ep->wMaxPacketSize);
304 unsigned int pipe, size, rc;
305 struct urb *urb;
306 uint8_t *buf;
307
308 urb = usb_alloc_urb(ICE1USB_MAX_ISOC_FRAMES, mem_flags);
309 if (!urb)
310 return -ENOMEM;
311
312 size = max_pack_size * ICE1USB_MAX_ISOC_FRAMES;
313 buf = kmalloc(size, mem_flags);
314 if (!buf) {
315 usb_free_urb(urb);
316 return -ENOMEM;
317 }
318
319 if (ep->bEndpointAddress & USB_DIR_IN)
320 pipe = usb_rcvisocpipe(ieu->usb_dev, ep->bEndpointAddress);
321 else
322 pipe = usb_sndisocpipe(ieu->usb_dev, ep->bEndpointAddress);
323
324 usb_fill_int_urb(urb, ieu->usb_dev, pipe, buf, size, compl, ieu,
325 ep->bInterval);
326 urb->transfer_flags = URB_FREE_BUFFER | URB_ISO_ASAP;
327
328 __fill_isoc_descriptor(urb, max_pack_size);
329
330 usb_anchor_urb(urb, anchor);
331
332 rc = usb_submit_urb(urb, mem_flags);
333 if (rc < 0) {
334 /* -EPERM: urb is being killed; -ENODEV: device got disconnected */
335 if (rc != -EPERM && rc != -ENODEV && rc != -ENOENT)
336 ieu_err(ieu, "EP 0x%02x urb %p submission failed (%d)",
337 ep->bEndpointAddress, urb, -rc);
338 usb_unanchor_urb(urb);
339 }
340
341 /* automatically free the URB (and its transfer buffer) once complete */
342 usb_free_urb(urb);
343
344 return rc;
345 }
346
347 /* process one incoming E1 frame (32 bytes; one for each TS) */
348 static void _span_demux_one_frame(struct ice1usb *ieu, const uint8_t *data)
349 {
350 struct dahdi_span *dspan = &ieu->dahdi.span;
351 unsigned int i;
352
353 for (i = 0; i < ARRAY_SIZE(ieu->dahdi.chans); i++) {
354 struct dahdi_chan *chan = ieu->dahdi.chans[i];
355 chan->readchunk[ieu->dahdi.readchunk_idx] = data[1+i];
356 }
357 ieu->dahdi.readchunk_idx++;
358
359 /* DAHDI_CHUNKSIZE is 8, meaning every channel (timeslot) wants data for 8
360 * bytes (PCM samples) every time _dahdi_receive() is called */
361 if (ieu->dahdi.readchunk_idx == DAHDI_CHUNKSIZE) {
362 _dahdi_receive(dspan);
363 ieu->dahdi.readchunk_idx = 0;
364 }
365 }
366
367 /* IN endpoint URB completes */
368 static void iso_in_complete(struct urb *urb)
369 {
370 struct ice1usb *ieu = urb->context;
371 unsigned int i;
372 int rc;
373
374 //ieu_dbg(ieu, "IN urb %p completion (%d)", urb, urb->status);
375
376 switch (urb->status) {
377 case 0:
378 for (i = 0; i < urb->number_of_packets; i++) {
379 unsigned int offset = urb->iso_frame_desc[i].offset;
380 unsigned int length = urb->iso_frame_desc[i].actual_length;
381 unsigned long flags;
382 unsigned int j;
383
384 if (urb->iso_frame_desc[i].status) {
385 ieu_err(ieu, "IN urb %p frame %u status %d",
386 urb, i, urb->iso_frame_desc[i].status);
387 continue;
388 }
389
390 /* process received data */
391 spin_lock_irqsave(&ieu->lock, flags);
392 for (j = 4; j < length; j += 32)
393 _span_demux_one_frame(ieu, urb->transfer_buffer + offset + j);
394 spin_unlock_irqrestore(&ieu->lock, flags);
395 }
396 break;
397 case -ENOENT:
398 case -ESHUTDOWN:
399 /* Avoid suspend failed when usb_kill_urb */
400 return;
401 default:
402 ieu_err(ieu, "IN urb %p completion (%d)", urb, urb->status);
403 break;
404 }
405
406 /* re-submit unless stopped */
407 if (!test_bit(ICE1USB_ISOC_RUNNING, &ieu->flags))
408 return;
409
410 usb_anchor_urb(urb, &ieu->anchor.iso_in);
411 usb_mark_last_busy(ieu->usb_dev);
412
413 rc = usb_submit_urb(urb, GFP_ATOMIC);
414 if (rc < 0) {
415 /* -EPERM: urb is being killed; -ENODEV: device got disconnected */
416 if (rc != -EPERM && rc != -ENODEV && rc != -ENOENT)
417 ieu_err(ieu, "IN urb %p submission failed (%d)", urb, -rc);
418 usb_unanchor_urb(urb);
419 }
420 }
421
422 /* Feedback endpoint URB completes */
423 static void iso_fb_complete(struct urb *urb)
424 {
425 struct ice1usb *ieu = urb->context;
426 unsigned int i;
427 int rc;
428
429 //ieu_dbg(ieu, "FB urb %p completion (%d)", urb, urb->status);
430
431 switch (urb->status) {
432 case 0:
433 for (i = 0; i < urb->number_of_packets; i++) {
434 unsigned int offset = urb->iso_frame_desc[i].offset;
435 unsigned int length = urb->iso_frame_desc[i].actual_length;
436 const uint8_t *rx = urb->transfer_buffer + offset;
437 unsigned long flags;
438
439 if (urb->iso_frame_desc[i].status) {
440 ieu_err(ieu, "FB urb %p frame %u status %d",
441 urb, i, urb->iso_frame_desc[i].status);
442 }
443
444 if (urb->iso_frame_desc[i].status || length < 3)
445 continue;
446
447 /* process received data */
448 spin_lock_irqsave(&ieu->lock, flags);
449 ieu->fc.r_sw = (rx[2] << 16) | (rx[1] << 8) | rx[0];
450 spin_unlock_irqrestore(&ieu->lock, flags);
451 }
452 break;
453 case -ENOENT:
454 case -ESHUTDOWN:
455 /* Avoid suspend failed when usb_kill_urb */
456 return;
457 default:
458 ieu_err(ieu, "FB urb %p completion (%d)", urb, urb->status);
459 break;
460 }
461
462 /* re-submit unless stopped */
463 if (!test_bit(ICE1USB_ISOC_RUNNING, &ieu->flags))
464 return;
465
466 usb_anchor_urb(urb, &ieu->anchor.iso_fb);
467 usb_mark_last_busy(ieu->usb_dev);
468
469 rc = usb_submit_urb(urb, GFP_ATOMIC);
470 if (rc < 0) {
471 /* -EPERM: urb is being killed; -ENODEV: device got disconnected */
472 if (rc != -EPERM && rc != -ENODEV && rc != -ENOENT)
473 dev_err(&ieu->usb_dev->dev, "FB urb %p submission failed (%d)", urb, -rc);
474 usb_unanchor_urb(urb);
475 }
476 }
477
478 /* multiplex one E1 frame (32 bytes) and write it to 'out'; caller must hold ieu->lock */
479 static void _span_mux_one_frame(struct ice1usb *ieu, uint8_t *out)
480 {
481 struct dahdi_span *dspan = &ieu->dahdi.span;
482 unsigned int i;
483
484 out[0] = 0;
485
486 for (i = 0; i < ARRAY_SIZE(ieu->dahdi.chans); i++) {
487 struct dahdi_chan *chan = ieu->dahdi.chans[i];
488 out[1+i] = chan->writechunk[ieu->dahdi.writechunk_idx];
489 }
490 ieu->dahdi.writechunk_idx++;
491
492 /* DAHDI_CHUNKSIZE is 8, meaning every channel (timeslot) wants data for 8
493 * bytes (PCM samples) every time _dahdi_receive() is called */
494 if (ieu->dahdi.writechunk_idx == DAHDI_CHUNKSIZE) {
495 _dahdi_transmit(dspan);
496 ieu->dahdi.writechunk_idx = 0;
497 }
498 }
499
500 /* OUT endpoint URB completes */
501 static void iso_out_complete(struct urb *urb)
502 {
503 struct ice1usb *ieu = urb->context;
504 unsigned int i, j;
505 int rc;
506
507 //ieu_dbg(ieu, "OUT urb %p completion (%d) %d", urb, urb->status, urb->number_of_packets);
508
509 switch (urb->status) {
510 case 0:
511 for (i = 0; i < urb->number_of_packets; i++) {
512 unsigned int offset = urb->iso_frame_desc[i].offset;
513 uint8_t *tx = urb->transfer_buffer + offset;
514 unsigned int fts; // frames to send
515 unsigned long flags;
516
517 if (urb->iso_frame_desc[i].status) {
518 ieu_err(ieu, "OUT urb %p frame %u status %d",
519 urb, i, urb->iso_frame_desc[i].status);
520 }
521
522
523 spin_lock_irqsave(&ieu->lock, flags);
524
525 /* flow control */
526 ieu->fc.r_acc += ieu->fc.r_sw;
527 fts = ieu->fc.r_acc >> 10;
528 if (fts < 4)
529 fts = 4;
530 else if (fts > ieu->max_fts)
531 fts = ieu->max_fts;
532
533 ieu->fc.r_acc -= fts << 10;
534 if (ieu->fc.r_acc & 0x80000000)
535 ieu->fc.r_acc = 0;
536
537 for (j = 0; j < fts; j++)
538 _span_mux_one_frame(ieu, tx + 4 + j*32);
539
540 spin_unlock_irqrestore(&ieu->lock, flags);
541
542 urb->iso_frame_desc[i].length = 4 + fts * 32;
543 }
544 break;
545 case -ENOENT:
546 case -ESHUTDOWN:
547 /* Avoid suspend failed when usb_kill_urb */
548 return;
549 default:
550 ieu_err(ieu, "OUT urb %p completion (%d)", urb, urb->status);
551 break;
552 }
553
554 /* re-submit unless stopped */
555 if (!test_bit(ICE1USB_ISOC_RUNNING, &ieu->flags))
556 return;
557
558 usb_anchor_urb(urb, &ieu->anchor.iso_out);
559 usb_mark_last_busy(ieu->usb_dev);
560
561 rc = usb_submit_urb(urb, GFP_ATOMIC);
562 if (rc < 0) {
563 /* -EPERM: urb is being killed; -ENODEV: device got disconnected */
564 if (rc != -EPERM && rc != -ENODEV && rc != -ENOENT)
565 ieu_err(ieu, "OUT urb %p submission failed (%d)", urb, -rc);
566 usb_unanchor_urb(urb);
567 }
568 }
569
570 /***********************************************************************
571 * INTERRUPT transfers
572 ***********************************************************************/
573
574 static uint32_t counter_delta(uint16_t old, uint16_t new)
575 {
576 if (new == old)
577 return 0;
578 else if (new > old)
579 return new - old;
580 else
581 return 0xffff - (old - new);
582 }
583
584 static void ice1usb_update_counters(struct ice1usb *ieu, const struct ice1usb_irq_err *err)
585 {
586 ieu->dahdi.span.count.fe += counter_delta(ieu->last_err.align, err->align);
587 ieu->dahdi.span.count.crc4 += counter_delta(ieu->last_err.crc, err->crc);
588 /* no DAHDI general counters for overflows / underflows */
589 ieu->count.ovfl += counter_delta(ieu->last_err.ovfl, err->ovfl);
590 ieu->count.unfl += counter_delta(ieu->last_err.unfl, err->unfl);
591 memcpy(&ieu->last_err, err, sizeof(ieu->last_err));
592 }
593
594 #define ALL_RY_ALARMS (DAHDI_ALARM_RED | DAHDI_ALARM_YELLOW | DAHDI_ALARM_BLUE | \
595 DAHDI_ALARM_LFA | DAHDI_ALARM_LMFA | DAHDI_ALARM_LOS)
596
597 /* interrupt EP completes: Process and resubmit */
598 static void ice1usb_irq_complete(struct urb *urb)
599 {
600 const struct ice1usb_irq *irq;
601 struct ice1usb *ieu = urb->context;
602 int rc;
603
604 //ieu_dbg(ieu, "IRQ urb %p completion (%d)", urb, urb->status);
605
606 if (urb->status == 0 && urb->actual_length >= sizeof(*irq)) {
607 const struct ice1usb_irq_err *err;
608 unsigned int alarms = 0;
609 irq = (struct ice1usb_irq *) urb->transfer_buffer;
610 switch (irq->type) {
611 case ICE1USB_IRQ_T_ERRCNT:
612 err = &irq->u.errors;
613 ieu_dbg(ieu, "IRQ: crc=%u, align=%u, ovfl=%u, unfl=%u, flags=%x",
614 le16_to_cpu(err->crc), le16_to_cpu(err->align),
615 le16_to_cpu(err->ovfl), le16_to_cpu(err->unfl), err->flags);
616 /* update alarms. Other drivers have some de-bouncing timers, I guess
617 * we can get away without doing this. */
618 if (err->flags & ICE1USB_ERR_F_LOS)
619 alarms |= DAHDI_ALARM_RED | DAHDI_ALARM_LOS;
620 if (err->flags & ICE1USB_ERR_F_ALIGN_ERR)
621 alarms |= DAHDI_ALARM_RED | DAHDI_ALARM_LFA | DAHDI_ALARM_LMFA;
622 if (err->flags & ICE1USB_ERR_F_RAI)
623 alarms |= DAHDI_ALARM_YELLOW;
624 if (err->flags & ICE1USB_ERR_F_AIS)
625 alarms |= DAHDI_ALARM_BLUE;
626 ieu->dahdi.span.alarms &= ~ALL_RY_ALARMS;
627 ieu->dahdi.span.alarms |= alarms;
628 dahdi_alarm_notify(&ieu->dahdi.span);
629 ice1usb_update_counters(ieu, err);
630 break;
631 }
632 } else if (urb->status == -ENOENT) {
633 /* Avoid suspend failed when usb_kill_urb */
634 return;
635 } else
636 ieu_err(ieu, "IRQ urb %p completion (%d)", urb, urb->status);
637
638 /* re-submit unless stopped */
639 if (!test_bit(ICE1USB_IRQ_RUNNING, &ieu->flags))
640 return;
641
642 usb_anchor_urb(urb, &ieu->anchor.irq);
643 usb_mark_last_busy(ieu->usb_dev);
644
645 rc = usb_submit_urb(urb, GFP_ATOMIC);
646 if (rc < 0) {
647 /* -EPERM: urb is being killed; -ENODEV: device got disconnected */
648 if (rc != -EPERM && rc != -ENODEV && rc != -ENOENT)
649 ieu_err(ieu, "IRU urb %p submission failed (%d)", urb, -rc);
650 usb_unanchor_urb(urb);
651 }
652 }
653
654 /* allocate + submit + anchor an URB for the interrupt endpoint */
655 static int ice1usb_submit_irq_urb(struct ice1usb *ieu, gfp_t mem_flags)
656 {
657 unsigned int pipe, size, rc;
658 struct urb *urb;
659 uint8_t *buf;
660
661 if (!ieu->ep.irq)
662 return -ENODEV;
663
664 urb = usb_alloc_urb(0, mem_flags);
665 if (!urb)
666 return -ENOMEM;
667
668 size = le16_to_cpu(ieu->ep.irq->wMaxPacketSize);
669 buf = kmalloc(size, mem_flags);
670 if (!buf) {
671 usb_free_urb(urb);
672 return -ENOMEM;
673 }
674
675 pipe = usb_rcvintpipe(ieu->usb_dev, ieu->ep.irq->bEndpointAddress);
676
677 usb_fill_int_urb(urb, ieu->usb_dev, pipe, buf, size,
678 ice1usb_irq_complete, ieu, ieu->ep.irq->bInterval);
679
680 urb->transfer_flags |= URB_FREE_BUFFER;
681
682 usb_anchor_urb(urb, &ieu->anchor.irq);
683
684 rc = usb_submit_urb(urb, mem_flags);
685 if (rc < 0) {
686 /* -EPERM: urb is being killed; -ENODEV: device got disconnected */
687 if (rc != -EPERM && rc != -ENODEV && rc != -ENOENT)
688 ieu_err(ieu, "IRQ urb %p submission failed (%d)", urb, -rc);
689 usb_unanchor_urb(urb);
690 }
691
692 /* automatically free the URB (and its transfer buffer) once complete */
693 usb_free_urb(urb);
694
695 return rc;
696 }
697
698
699 /***********************************************************************
700 * DAHDI integration
701 ***********************************************************************/
702
703 static int e1u_d_startup(struct file *file, struct dahdi_span *span);
704 static int e1u_d_shutdown(struct dahdi_span *span);
705
706 static int e1u_d_spanconfig(struct file *file, struct dahdi_span *span,
707 struct dahdi_lineconfig *lc)
708 {
709 struct ice1usb *ieu = container_of(span, struct ice1usb, dahdi.span);
710 unsigned int i;
711 int rc;
712
713 ieu_dbg(ieu, "entering %s", __FUNCTION__);
714
715 if (lc->sync < 0)
716 lc->sync = 0;
717 if (lc->sync > 1) {
718 ieu_warn(ieu, "Cannot set clock priority on span %d to %d\n",
719 span->spanno, lc->sync);
720 lc->sync = 0;
721 }
722
723 if (span->lineconfig & DAHDI_CONFIG_CRC4) {
724 ieu->cfg.tx.mode = ICE1USB_TX_MODE_TS0_CRC4_E;
725 ieu->cfg.rx.mode = ICE1USB_RX_MODE_MULTIFRAME;
726 } else {
727 ieu->cfg.tx.mode = ICE1USB_TX_MODE_TS0;
728 ieu->cfg.rx.mode = ICE1USB_RX_MODE_FRAME;
729 }
730
731 if (lc->sync > 0)
732 ieu->cfg.tx.timing = ICE1USB_TX_TIME_SRC_REMOTE;
733 else
734 ieu->cfg.tx.timing = ICE1USB_TX_TIME_SRC_LOCAL;
735
736 /* (re-)set to sane defaults */
737 ieu->cfg.tx.ext_loopback = ICE1USB_TX_EXT_LOOPBACK_OFF;
738 ieu->cfg.tx.alarm = 0;
739
740 for (i = 0; i < span->channels; i++) {
741 struct dahdi_chan *const chan = ieu->dahdi.chans[i];
742 chan->sigcap = DAHDI_SIG_CLEAR | DAHDI_SIG_MTP2 | DAHDI_SIG_SF;
743 }
744
745 /* If we're already running, then go ahead and apply the changes */
746 if (span->flags & DAHDI_FLAG_RUNNING) {
747 /* there should probably be a more elegant way to do this, but
748 * we don't expect people to keep re-configuring their span all
749 * day long, so a brief interruption is deemed acceptable */
750 e1u_d_shutdown(span);
751 rc = ice1usb_tx_config(ieu);
752 if (rc < 0)
753 return rc;
754 rc = ice1usb_rx_config(ieu);
755 if (rc < 0)
756 return rc;
757 rc = e1u_d_startup(file, span);
758 } else {
759 rc = ice1usb_tx_config(ieu);
760 if (rc < 0)
761 return rc;
762 rc = ice1usb_rx_config(ieu);
763 }
764
765 return rc;
766 }
767
768 static int e1u_d_chanconfig(struct file *file, struct dahdi_chan *chan,
769 int sigtype)
770 {
771 struct ice1usb *ieu = chan->pvt;
772 bool already_running;
773
774 already_running = ieu->dahdi.span.flags & DAHDI_FLAG_RUNNING;
775 ieu_info(ieu, "%sconfigured channel %d (%s) sigtype %d\n",
776 already_running ? "Re":"", chan->channo, chan->name, sigtype);
777
778 /* FIXME: we can probably remove this completely? */
779 return 0;
780 }
781
782 static int ice1usb_set_altif(struct ice1usb *ieu, bool on);
783
784 static int e1u_d_startup(struct file *file, struct dahdi_span *span)
785 {
786 struct ice1usb *ieu = container_of(span, struct ice1usb, dahdi.span);
787 unsigned long flags;
788 unsigned int i;
789 int rc;
790
791 ieu_dbg(ieu, "entering %s", __FUNCTION__);
792
793 /* Ensure we are in the right altsetting */
794 rc = ice1usb_set_altif(ieu, true);
795 if (rc < 0)
796 goto err;
797
798 if (!test_and_set_bit(ICE1USB_ISOC_RUNNING, &ieu->flags)) {
799 for (i = 0; i < ICE1USB_NUM_URBS; i++) {
800 rc = ice1usb_submit_isoc_urb(ieu, ieu->ep.iso_in, &ieu->anchor.iso_in,
801 iso_in_complete, GFP_KERNEL);
802 if (rc) {
803 ieu_err(ieu, "error submitting IN ep URB %u (%d)", i, rc);
804 goto err_isoc_in;
805 }
806 rc = ice1usb_submit_isoc_urb(ieu, ieu->ep.iso_fb, &ieu->anchor.iso_fb,
807 iso_fb_complete, GFP_KERNEL);
808 if (rc) {
809 ieu_err(ieu, "error submitting FB ep URB %u (%d)", i, rc);
810 goto err_isoc_fb;
811 }
812 rc = ice1usb_submit_isoc_urb(ieu, ieu->ep.iso_out, &ieu->anchor.iso_out,
813 iso_out_complete, GFP_KERNEL);
814 if (rc) {
815 ieu_err(ieu, "error submitting OUT ep URB %u (%d)", i, rc);
816 goto err_isoc_out;
817 }
818 }
819 }
820
821 if (!test_and_set_bit(ICE1USB_IRQ_RUNNING, &ieu->flags)) {
822 for (i = 0; i < ICE1USB_NUM_URBS; i++) {
823 rc = ice1usb_submit_irq_urb(ieu, GFP_KERNEL);
824 if (rc) {
825 ieu_err(ieu, "error submitting IRQ ep %u (%d)", i, rc);
826 goto err_irq;
827 }
828 }
829 }
830
831 spin_lock_irqsave(&span->lock, flags);
832 span->alarms &= ~DAHDI_ALARM_NOTOPEN;
833 dahdi_alarm_notify(span);
834 spin_unlock_irqrestore(&span->lock, flags);
835 return 0;
836
837 err_irq:
838 usb_kill_anchored_urbs(&ieu->anchor.irq);
839 clear_bit(ICE1USB_IRQ_RUNNING, &ieu->flags);
840 usb_kill_anchored_urbs(&ieu->anchor.iso_out);
841 err_isoc_out:
842 usb_kill_anchored_urbs(&ieu->anchor.iso_fb);
843 err_isoc_fb:
844 usb_kill_anchored_urbs(&ieu->anchor.iso_in);
845 err_isoc_in:
846 clear_bit(ICE1USB_ISOC_RUNNING, &ieu->flags);
847 ice1usb_set_altif(ieu, false);
848 err:
849 spin_lock_irqsave(&span->lock, flags);
850 span->alarms |= DAHDI_ALARM_NOTOPEN;
851 dahdi_alarm_notify(span);
852 spin_unlock_irqrestore(&span->lock, flags);
853 return rc;
854 }
855
856 static int e1u_d_shutdown(struct dahdi_span *span)
857 {
858 struct ice1usb *ieu = container_of(span, struct ice1usb, dahdi.span);
859 int rc;
860
861 ieu_dbg(ieu, "entering %s", __FUNCTION__);
862
863 if (test_and_clear_bit(ICE1USB_ISOC_RUNNING, &ieu->flags)) {
864 usb_kill_anchored_urbs(&ieu->anchor.iso_in);
865 usb_kill_anchored_urbs(&ieu->anchor.iso_out);
866 usb_kill_anchored_urbs(&ieu->anchor.iso_fb);
867 }
868
869 if (test_and_clear_bit(ICE1USB_IRQ_RUNNING, &ieu->flags))
870 usb_kill_anchored_urbs(&ieu->anchor.irq);
871
872 /* guard against devices unplugged (see ice1usb_disconnect) */
873 if (ieu->present) {
874 /* switch to 'off' altsetting */
875 rc = ice1usb_set_altif(ieu, false);
876 if (rc < 0)
877 return rc;
878 }
879
880 return 0;
881 }
882
883 /* set some maintenance mode according to 'cmd' */
884 static int e1u_d_maint(struct dahdi_span *span, int cmd)
885 {
886 struct ice1usb *ieu = container_of(span, struct ice1usb, dahdi.span);
887 int rc = 0;
888
889 ieu_dbg(ieu, "entering %s(%d)", __FUNCTION__, cmd);
890
891 switch (cmd) {
892 case DAHDI_MAINT_NONE:
893 ieu_info(ieu, "Clearing all maint modes\n");
894 ieu->cfg.tx.ext_loopback = ICE1USB_TX_EXT_LOOPBACK_OFF;
895 rc = ice1usb_tx_config(ieu);
896 break;
897 case DAHDI_MAINT_NETWORKLINELOOP:
898 ieu_info(ieu, "Turning on network line loopback\n");
899 ieu->cfg.tx.ext_loopback = ICE1USB_TX_EXT_LOOPBACK_SAME;
900 rc = ice1usb_tx_config(ieu);
901 break;
902 /* TODO: DAHDI_MAINT_*_DEFECT */
903 /* TODO: DAHDI_MAINT_ALARM_SIM */
904 case DAHDI_RESET_COUNTERS:
905 memset(&ieu->dahdi.span.count, 0, sizeof(ieu->dahdi.span.count));
906 /* don't reset last_err, as we need it to count the _new_ errors */
907 break;
908 default:
909 ieu_info(ieu, "Unknown E1 maint command: %d\n", cmd);
910 return -ENOSYS;
911 }
912
913 return rc;
914 }
915
916 static const struct dahdi_span_ops ice1usb_span_ops = {
917 .owner = THIS_MODULE,
918 .spanconfig = e1u_d_spanconfig,
919 .chanconfig = e1u_d_chanconfig,
920 .startup = e1u_d_startup,
921 .shutdown = e1u_d_shutdown,
922 .maint = e1u_d_maint,
923 };
924
925 /***********************************************************************
926 * USB GPSDO driver (sysfs files) - NOT _interface_ driver
927 ***********************************************************************/
928
929 static void ice1usb_gpsdo_free(struct ice1usb_gpsdo *e1d)
930 {
931 usb_put_dev(e1d->usb_dev);
932 kfree(e1d);
933 }
934
935 /* The GPS-DO exists only once for the entire USB device, while all the E1 related
936 * bits exist once per USB interface! */
937
938 /* update our internal, cached GPS-DO state */
939 static int e1u_update_gpsdo(struct ice1usb_gpsdo *e1d)
940 {
941 uint8_t if_num = e1d->usb_intf->cur_altsetting->desc.bInterfaceNumber;
942 int rc;
943
944 rc = usb_control_msg(e1d->usb_dev, usb_rcvctrlpipe(e1d->usb_dev, 0),
945 ICE1USB_INTF_GET_GPSDO_STATUS, USB_DIR_IN | USB_RT_VEND_IF,
946 0, if_num, &e1d->gpsdo_status, sizeof(e1d->gpsdo_status),
947 USB_CTRL_SET_TIMEOUT);
948 if (rc < 0) {
949 dev_err(&e1d->usb_intf->dev, "Error during GPSDO CTRL GET transfer: %d\n", rc);
950 return rc;
951 }
952 if (rc != sizeof(e1d->gpsdo_status)) {
953 dev_err(&e1d->usb_dev->dev, "Short read during GPSDO CTRL GET transfer: %d\n", rc);
954 return -EIO;
955 }
956
957 return 0;
958 }
959
960 static const char *gpsdo_mode_str(enum ice1usb_gpsdo_mode mode)
961 {
962 switch (mode) {
963 case ICE1USB_GPSDO_MODE_DISABLED:
964 return "disabled";
965 case ICE1USB_GPSDO_MODE_AUTO:
966 return "auto";
967 default:
968 return "invalid";
969 }
970 }
971
972 static const char *gpsdo_antenna_state_str(enum ice1usb_gpsdo_antenna_state st)
973 {
974 switch (st) {
975 case ICE1USB_GPSDO_ANT_UNKNOWN:
976 return "unknown";
977 case ICE1USB_GPSDO_ANT_OK:
978 return "ok";
979 case ICE1USB_GPSDO_ANT_OPEN:
980 return "open";
981 case ICE1USB_GPSDO_ANT_SHORT:
982 return "short";
983 default:
984 return "invalid";
985 }
986 }
987
988 static const char *gpsdo_state_str(enum ice1usb_gpsdo_state st)
989 {
990 switch (st) {
991 case ICE1USB_GPSDO_STATE_DISABLED:
992 return "disabled";
993 case ICE1USB_GPSDO_STATE_CALIBRATE:
994 return "calibrate";
995 case ICE1USB_GPSDO_STATE_HOLD_OVER:
996 return "hold_over";
997 case ICE1USB_GPSDO_STATE_TUNE_COARSE:
998 return "tune_coarse";
999 case ICE1USB_GPSDO_STATE_TUNE_FINE:
1000 return "tune_fine";
1001 default:
1002 return "invalid";
1003 }
1004 }
1005
1006
1007 static ssize_t e1u_gpsdo_mode_show(struct device *dev, struct device_attribute *attr,
1008 char *buf)
1009 {
1010 struct ice1usb_gpsdo *e1d = dev_get_drvdata(dev);
1011 int rc = e1u_update_gpsdo(e1d);
1012 if (rc < 0)
1013 return rc;
1014 return sprintf(buf, "%s\n", gpsdo_mode_str(e1d->gpsdo_status.mode));
1015 }
1016
1017 static ssize_t e1u_gpsdo_mode_store(struct device *dev, struct device_attribute *attr,
1018 const char *buf, size_t count)
1019 {
1020 struct ice1usb_gpsdo *e1d = dev_get_drvdata(dev);
1021 uint8_t if_num = e1d->usb_intf->cur_altsetting->desc.bInterfaceNumber;
1022 uint16_t imode;
1023 int rc;
1024
1025 if (!strcmp(buf, "disabled"))
1026 imode = ICE1USB_GPSDO_MODE_DISABLED;
1027 else if (!strcmp(buf, "auto"))
1028 imode = ICE1USB_GPSDO_MODE_AUTO;
1029 else
1030 return -EINVAL;
1031
1032 rc = usb_control_msg(e1d->usb_dev, usb_sndctrlpipe(e1d->usb_dev, 0),
1033 ICE1USB_INTF_SET_GPSDO_MODE, USB_RT_VEND_IF,
1034 imode, if_num, NULL, 0, USB_CTRL_SET_TIMEOUT);
1035 if (rc < 0) {
1036 dev_err(&e1d->usb_intf->dev, "Error during GPSDO CTRL SET transfer: %d\n", rc);
1037 return -EIO;
1038 }
1039 e1d->gpsdo_status.mode = imode;
1040
1041 return count;
1042 }
1043
1044 static DEVICE_ATTR(gpsdo_mode, 0644, e1u_gpsdo_mode_show, e1u_gpsdo_mode_store);
1045
1046 static ssize_t e1u_gpsdo_ant_state_show(struct device *dev,
1047 struct device_attribute *attr, char *buf)
1048 {
1049 struct ice1usb_gpsdo *e1d = dev_get_drvdata(dev);
1050 int rc = e1u_update_gpsdo(e1d);
1051 if (rc < 0)
1052 return rc;
1053 return sprintf(buf, "%s\n", gpsdo_antenna_state_str(e1d->gpsdo_status.antenna_state));
1054 }
1055
1056 static DEVICE_ATTR(gpsdo_antenna_state, 0444, e1u_gpsdo_ant_state_show, NULL);
1057
1058 static ssize_t e1u_gpsdo_state_show(struct device *dev,
1059 struct device_attribute *attr, char *buf)
1060 {
1061 struct ice1usb_gpsdo *e1d = dev_get_drvdata(dev);
1062 int rc = e1u_update_gpsdo(e1d);
1063 if (rc < 0)
1064 return rc;
1065 return sprintf(buf, "%s\n", gpsdo_state_str(e1d->gpsdo_status.state));
1066 }
1067
1068 static DEVICE_ATTR(gpsdo_state, 0444, e1u_gpsdo_state_show, NULL);
1069
1070 static ssize_t e1u_gpsdo_valid_fix_show(struct device *dev,
1071 struct device_attribute *attr, char *buf)
1072 {
1073 struct ice1usb_gpsdo *e1d = dev_get_drvdata(dev);
1074 int rc = e1u_update_gpsdo(e1d);
1075 if (rc < 0)
1076 return rc;
1077 return sprintf(buf, "%u\n", e1d->gpsdo_status.valid_fix);
1078 }
1079
1080 static DEVICE_ATTR(gpsdo_valid_fix, 0444, e1u_gpsdo_valid_fix_show, NULL);
1081
1082
1083 static ssize_t e1u_gpsdo_tune_coarse_show(struct device *dev,
1084 struct device_attribute *attr, char *buf)
1085 {
1086 struct ice1usb_gpsdo *e1d = dev_get_drvdata(dev);
1087 int rc = e1u_update_gpsdo(e1d);
1088 if (rc < 0)
1089 return rc;
1090 return sprintf(buf, "%u\n", e1d->gpsdo_status.tune.coarse);
1091 }
1092
1093 /* TODO: set tuning */
1094 static DEVICE_ATTR(gpsdo_tune_coarse, 0444, e1u_gpsdo_tune_coarse_show, NULL);
1095
1096
1097 static ssize_t e1u_gpsdo_tune_fine_show(struct device *dev,
1098 struct device_attribute *attr, char *buf)
1099 {
1100 struct ice1usb_gpsdo *e1d = dev_get_drvdata(dev);
1101 int rc = e1u_update_gpsdo(e1d);
1102 if (rc < 0)
1103 return rc;
1104 return sprintf(buf, "%u\n", e1d->gpsdo_status.tune.fine);
1105 }
1106
1107 /* TODO: set tuning */
1108 static DEVICE_ATTR(gpsdo_tune_fine, 0444, e1u_gpsdo_tune_fine_show, NULL);
1109
1110 static ssize_t e1u_gpsdo_freq_est_show(struct device *dev,
1111 struct device_attribute *attr, char *buf)
1112 {
1113 struct ice1usb_gpsdo *e1d = dev_get_drvdata(dev);
1114 int rc = e1u_update_gpsdo(e1d);
1115 if (rc < 0)
1116 return rc;
1117 return sprintf(buf, "%u\n", e1d->gpsdo_status.freq_est);
1118 }
1119
1120 static DEVICE_ATTR(gpsdo_freq_est, 0444, e1u_gpsdo_freq_est_show, NULL);
1121
1122 static ssize_t e1u_gpsdo_err_acc_show(struct device *dev,
1123 struct device_attribute *attr, char *buf)
1124 {
1125 struct ice1usb_gpsdo *e1d = dev_get_drvdata(dev);
1126 int rc = e1u_update_gpsdo(e1d);
1127 if (rc < 0)
1128 return rc;
1129 return sprintf(buf, "%d\n", (int)e1d->gpsdo_status.err_acc);
1130 }
1131
1132 static DEVICE_ATTR(gpsdo_err_acc, 0444, e1u_gpsdo_err_acc_show, NULL);
1133
1134 static ssize_t e1u_fw_build_show(struct device *dev,
1135 struct device_attribute *attr, char *buf)
1136 {
1137 struct ice1usb_gpsdo *e1d = dev_get_drvdata(dev);
1138
1139 return sprintf(buf, "%s\n", e1d->fw_build);
1140 }
1141
1142 static DEVICE_ATTR(fw_build, 0444, e1u_fw_build_show, NULL);
1143
1144 static void create_sysfs_files(struct ice1usb_gpsdo *e1d)
1145 {
1146 device_create_file(&e1d->usb_intf->dev, &dev_attr_gpsdo_mode);
1147 device_create_file(&e1d->usb_intf->dev, &dev_attr_gpsdo_antenna_state);
1148 device_create_file(&e1d->usb_intf->dev, &dev_attr_gpsdo_state);
1149 device_create_file(&e1d->usb_intf->dev, &dev_attr_gpsdo_valid_fix);
1150 device_create_file(&e1d->usb_intf->dev, &dev_attr_gpsdo_tune_coarse);
1151 device_create_file(&e1d->usb_intf->dev, &dev_attr_gpsdo_tune_fine);
1152 device_create_file(&e1d->usb_intf->dev, &dev_attr_gpsdo_freq_est);
1153 device_create_file(&e1d->usb_intf->dev, &dev_attr_gpsdo_err_acc);
1154 device_create_file(&e1d->usb_intf->dev, &dev_attr_fw_build);
1155 }
1156
1157 static void remove_sysfs_files(struct ice1usb_gpsdo *e1d)
1158 {
1159 device_remove_file(&e1d->usb_intf->dev, &dev_attr_gpsdo_mode);
1160 device_remove_file(&e1d->usb_intf->dev, &dev_attr_gpsdo_antenna_state);
1161 device_remove_file(&e1d->usb_intf->dev, &dev_attr_gpsdo_state);
1162 device_remove_file(&e1d->usb_intf->dev, &dev_attr_gpsdo_valid_fix);
1163 device_remove_file(&e1d->usb_intf->dev, &dev_attr_gpsdo_tune_coarse);
1164 device_remove_file(&e1d->usb_intf->dev, &dev_attr_gpsdo_tune_fine);
1165 device_remove_file(&e1d->usb_intf->dev, &dev_attr_gpsdo_freq_est);
1166 device_remove_file(&e1d->usb_intf->dev, &dev_attr_gpsdo_err_acc);
1167 device_remove_file(&e1d->usb_intf->dev, &dev_attr_fw_build);
1168 }
1169
1170 static int ice1usb_gpsdo_probe(struct usb_interface *intf, const struct usb_device_id *prod)
1171 {
1172 struct usb_device *usb_dev = usb_get_dev(interface_to_usbdev(intf));
1173 const struct usb_interface_descriptor *ifdesc = &intf->altsetting->desc;
1174 struct ice1usb_gpsdo *e1d;
1175 int ret = -ENODEV;
1176 int rc;
1177
1178 dev_dbg(&intf->dev, "entering %s", __FUNCTION__);
1179
1180 if (ifdesc->bInterfaceClass != 0xff || ifdesc->bInterfaceSubClass != 0xE1) {
1181 dev_dbg(&intf->dev, "Unsupported Interface Class/SubClass %02x/%02x",
1182 ifdesc->bInterfaceClass, ifdesc->bInterfaceSubClass);
1183 ret = -ENODEV;
1184 goto error;
1185 }
1186
1187 /* we only support protocol 0 so far */
1188 if (ifdesc->bInterfaceProtocol != 0xd0) {
1189 ret = -ENODEV;
1190 goto error;
1191 }
1192
1193 e1d = kzalloc(sizeof(*e1d), GFP_KERNEL);
1194 if (!e1d) {
1195 dev_err(&intf->dev, "Out of memory\n");
1196 ret = -ENOMEM;
1197 goto error;
1198 }
1199
1200 e1d->usb_dev = usb_dev;
1201 e1d->usb_intf = intf;
1202 e1d->present = true;
1203 spin_lock_init(&e1d->lock);
1204
1205 usb_set_intfdata(intf, e1d);
1206
1207 /* obtain firmware build information */
1208 e1d->fw_build[0] = '\0';
1209 rc = usb_control_msg(e1d->usb_dev, usb_rcvctrlpipe(e1d->usb_dev, 0),
1210 ICE1USB_DEV_GET_FW_BUILD, USB_DIR_IN | USB_RT_VEND_DEV,
1211 0, 0, e1d->fw_build, sizeof(e1d->fw_build), USB_CTRL_SET_TIMEOUT);
1212 if (rc < 0) {
1213 dev_err(&e1d->usb_intf->dev, "Error during FW BUILD CTRL GET transfer: %d\n", rc);
1214 ret = rc;
1215 goto error_free;
1216 }
1217 e1d->fw_build[sizeof(e1d->fw_build)-1] = '\0';
1218 dev_info(&e1d->usb_intf->dev, "icE1usb firmware build: '%s'\n", e1d->fw_build);
1219
1220 create_sysfs_files(e1d);
1221
1222 return 0;
1223
1224 error_free:
1225 usb_set_intfdata(intf, NULL);
1226 kfree(e1d);
1227 error:
1228 usb_put_dev(usb_dev);
1229 return ret;
1230 }
1231
1232 static void ice1usb_gpsdo_disconnect(struct usb_interface *intf)
1233 {
1234 struct ice1usb_gpsdo *e1d = usb_get_intfdata(intf);
1235
1236 dev_dbg(&intf->dev, "entering %s", __FUNCTION__);
1237
1238 if (!e1d)
1239 return;
1240
1241 /* avoid any shutdown code from submitting further I/O */
1242 e1d->present = false;
1243
1244 remove_sysfs_files(e1d);
1245
1246 usb_set_intfdata(intf, NULL);
1247
1248 ice1usb_gpsdo_free(e1d);
1249 }
1250
1251
1252 static struct usb_driver ice1usb_gpsdo_driver = {
1253 .name = "icE1usb-GPSDO",
1254 .id_table = ice1usb_products,
1255 .probe = ice1usb_gpsdo_probe,
1256 .disconnect = ice1usb_gpsdo_disconnect,
1257 };
1258
1259 /***********************************************************************
1260 * kernel USB integration / probing
1261 ***********************************************************************/
1262
1263 /* does the given altsetting contain an EP with wMaxPacketSize == 0 ? */
1264 static bool has_ep_packetsize_zero(const struct usb_host_interface *alt)
1265 {
1266 unsigned int j;
1267
1268 /* compute the sum of all wMaxPacketSize */
1269 for (j = 0; j < alt->desc.bNumEndpoints; j++) {
1270 const struct usb_endpoint_descriptor *epd = &alt->endpoint[j].desc;
1271 if (usb_endpoint_xfer_isoc(epd)) {
1272 if (epd->wMaxPacketSize == 0)
1273 return true;
1274 }
1275 }
1276 return false;
1277 }
1278
1279 /* find the 'on' altsetting (all ISOC EP wMaxPacketSize != 0) */
1280 static const struct usb_host_interface *find_altsetting_on(struct usb_interface *intf)
1281 {
1282 unsigned int i;
1283
1284 for (i = 0; i < intf->num_altsetting; i++) {
1285 const struct usb_host_interface *alt = &intf->altsetting[i];
1286 if (!has_ep_packetsize_zero(alt) && alt->desc.bNumEndpoints >= 3)
1287 return alt;
1288 }
1289 return NULL;
1290 }
1291
1292 /* find the 'off' altsetting (all ISOC EP wMaxPacketSize == 0) */
1293 static const struct usb_host_interface *find_altsetting_off(struct usb_interface *intf)
1294 {
1295 unsigned int i;
1296
1297 for (i = 0; i < intf->num_altsetting; i++) {
1298 const struct usb_host_interface *alt = &intf->altsetting[i];
1299 unsigned int iso_ep_size = 0;
1300 unsigned int j;
1301
1302 /* compute the sum of all wMaxPacketSize */
1303 for (j = 0; j < alt->desc.bNumEndpoints; j++) {
1304 const struct usb_endpoint_descriptor *epd = &alt->endpoint[j].desc;
1305 if (usb_endpoint_xfer_isoc(epd))
1306 iso_ep_size += le16_to_cpu(epd->wMaxPacketSize);
1307 }
1308
1309 if (iso_ep_size == 0)
1310 return alt;
1311 }
1312 return NULL;
1313 }
1314
1315 /* find the endpoint numbers within the interface */
1316 static int find_endpoints(struct ice1usb *ieu, const struct usb_host_interface *alt)
1317 {
1318 unsigned int i;
1319 int found = 0;
1320
1321 for (i = 0; i < alt->desc.bNumEndpoints; i++) {
1322 const struct usb_endpoint_descriptor *epd = &alt->endpoint[i].desc;
1323 switch (usb_endpoint_type(epd)) {
1324 case USB_ENDPOINT_XFER_INT:
1325 if (!ieu->ep.irq && usb_endpoint_dir_in(epd)) {
1326 ieu->ep.irq = epd;
1327 found++;
1328 }
1329 break;
1330 case USB_ENDPOINT_XFER_ISOC:
1331 if (usb_endpoint_dir_in(epd)) {
1332 if ((epd->bmAttributes & USB_ENDPOINT_USAGE_MASK) ==
1333 USB_ENDPOINT_USAGE_FEEDBACK) {
1334 if (!ieu->ep.iso_fb) {
1335 ieu->ep.iso_fb = epd;
1336 found++;
1337 }
1338 } else {
1339 if (!ieu->ep.iso_in) {
1340 ieu->ep.iso_in = epd;
1341 found++;
1342 }
1343 }
1344 } else {
1345 if (!ieu->ep.iso_out) {
1346 ieu->ep.iso_out = epd;
1347 found++;
1348 }
1349 }
1350 break;
1351 default:
1352 break;
1353 }
1354 }
1355 return found;
1356 }
1357
1358
1359 /* select interface altsetting on / off */
1360 static int ice1usb_set_altif(struct ice1usb *ieu, bool on)
1361 {
1362 const struct usb_interface_descriptor *desc;
1363 int rc;
1364
1365 ieu_dbg(ieu, "entering %s(%d)", __FUNCTION__, on);
1366
1367 if (on)
1368 desc = &ieu->alt_on->desc;
1369 else
1370 desc = &ieu->alt_off->desc;
1371
1372 rc = usb_set_interface(ieu->usb_dev, desc->bInterfaceNumber, desc->bAlternateSetting);
1373 if (rc) {
1374 ieu_err(ieu, "error setting %s-altif %u (%d)",
1375 on ? "ON" : "OFF", desc->bInterfaceNumber, rc);
1376 }
1377
1378 return rc;
1379 }
1380
1381 static int ice1usb_probe(struct usb_interface *intf, const struct usb_device_id *prod)
1382 {
1383 struct usb_device *usb_dev = usb_get_dev(interface_to_usbdev(intf));
1384 const struct usb_interface_descriptor *ifdesc = &intf->altsetting->desc;
1385 unsigned int max_pack_size;
1386 struct dahdi_device *ddev;
1387 struct dahdi_span *dspan;
1388 struct ice1usb *ieu;
1389 int ret = -ENODEV;
1390 int rc;
1391
1392 dev_dbg(&intf->dev, "entering %s", __FUNCTION__);
1393
1394 if (ifdesc->bInterfaceClass != 0xff || ifdesc->bInterfaceSubClass != 0xE1) {
1395 dev_dbg(&intf->dev, "Unsupported Interface Class/SubClass %02x/%02x",
1396 ifdesc->bInterfaceClass, ifdesc->bInterfaceSubClass);
1397 return -ENODEV;
1398 }
1399
1400 /* we only support protocol 0 so far */
1401 if (ifdesc->bInterfaceProtocol != 0x00)
1402 return -ENODEV;
1403
1404 ieu = kzalloc(sizeof(*ieu), GFP_KERNEL);
1405 if (!ieu) {
1406 dev_err(&intf->dev, "Out of memory\n");
1407 return -ENOMEM;
1408 }
1409
1410 ieu->usb_dev = usb_dev;
1411 ieu->usb_intf = intf;
1412 ieu->fc.r_acc = 0;
1413 ieu->fc.r_sw = 8192;
1414 ieu->present = true;
1415 spin_lock_init(&ieu->lock);
1416
1417 /* locate ON / OFF altsettings */
1418 ieu->alt_off = find_altsetting_off(ieu->usb_intf);
1419 if (!ieu->alt_off) {
1420 dev_err(&intf->dev, "Cannot find OFF altsetting");
1421 goto error;
1422 }
1423 ieu->alt_on = find_altsetting_on(ieu->usb_intf);
1424 if (!ieu->alt_on) {
1425 dev_err(&intf->dev, "Cannot find ON altsetting");
1426 goto error;
1427 }
1428 dev_info(&intf->dev, "altsetting off=%u, on=%u\n",
1429 ieu->alt_off->desc.bAlternateSetting, ieu->alt_on->desc.bAlternateSetting);
1430
1431 /* locate ON / OFF altsettings */
1432 if (find_endpoints(ieu, ieu->alt_on) < 4) {
1433 dev_err(&intf->dev, "Cannot find all endpoints");
1434 goto error;
1435 }
1436
1437 /* compute the maximum number of E1 frames to send in one ISO OUT packet */
1438 max_pack_size = le16_to_cpu(ieu->ep.iso_out->wMaxPacketSize);
1439 ieu->max_fts = (max_pack_size - 4) / 32;
1440 dev_dbg(&intf->dev, "Maximum FTS: %u", ieu->max_fts);
1441
1442 /* TODO: only one dahdi_device even for multiple USB interfaces? */
1443 ddev = ieu->dahdi.dev = dahdi_create_device();
1444 if (!ddev)
1445 goto error;
1446 ddev->manufacturer = usb_dev->manufacturer;
1447 ddev->devicetype = usb_dev->product;
1448 ddev->hardware_id = usb_dev->serial;
1449 //ddev->location = USB_BUS_PATH;
1450
1451 dspan = &ieu->dahdi.span;
1452 /* TODO: include USB path info? */
1453 snprintf(dspan->name, sizeof(dspan->name), "icE1usb/1/%d",
1454 ieu->alt_on->desc.bInterfaceNumber); //TDOO: device != 1
1455 snprintf(dspan->desc, sizeof(dspan->desc), "Osmocom icE1USB Card 1 Span %d",
1456 ieu->alt_on->desc.bInterfaceNumber); //TODO: device != 1
1457 dspan->channels = 31;
1458 dspan->spantype = SPANTYPE_DIGITAL_E1;
1459 dspan->deflaw = DAHDI_LAW_ALAW;
1460 dspan->linecompat = DAHDI_CONFIG_HDB3 | DAHDI_CONFIG_CCS | DAHDI_CONFIG_CRC4;
1461 dspan->chans = ieu->dahdi.chans;
1462 dspan->flags = 0;
1463 dspan->offset = 0;
1464 dspan->ops = &ice1usb_span_ops;
1465
1466 rc = e1u_alloc_channels(ieu);
1467 if (rc) {
1468 ret = -ENOMEM;
1469 goto err_free_ddev;
1470 }
1471
1472 list_add_tail(&dspan->device_node, &ddev->spans);
1473
1474 rc = dahdi_register_device(ieu->dahdi.dev, &intf->dev);
1475 if (rc) {
1476 dev_err(&intf->dev, "Failed to reigster with DAHDI\n");
1477 goto err_free_chans;
1478 }
1479
1480 init_usb_anchor(&ieu->anchor.iso_in);
1481 init_usb_anchor(&ieu->anchor.iso_out);
1482 init_usb_anchor(&ieu->anchor.iso_fb);
1483 init_usb_anchor(&ieu->anchor.irq);
1484
1485 usb_set_intfdata(intf, ieu);
1486
1487 return 0;
1488
1489 err_free_chans:
1490 e1u_free_channels(ieu);
1491 err_free_ddev:
1492 dahdi_free_device(ieu->dahdi.dev);
1493 error:
1494 kfree(ieu);
1495 return ret;
1496 }
1497
1498 static struct usb_driver ice1usb_driver;
1499
1500 static void ice1usb_disconnect(struct usb_interface *intf)
1501 {
1502 struct ice1usb *ieu = usb_get_intfdata(intf);
1503
1504 dev_dbg(&intf->dev, "entering %s", __FUNCTION__);
1505
1506 if (!ieu)
1507 return;
1508
1509 /* avoid any shutdown code from submitting further I/O */
1510 ieu->present = false;
1511
1512 usb_set_intfdata(intf, NULL);
1513
1514 /* will in turn call e1u_d_shutdown() which stops all transfers */
1515 dahdi_unregister_device(ieu->dahdi.dev);
1516
1517 ice1usb_free(ieu);
1518 }
1519
1520 #if 0
1521 static int ice1usb_suspend(struct usb_interface *intf, pm_message_t message)
1522 {
1523 struct ice1usb *ieu = usb_get_intfdata(intf);
1524 /* FIXME */
1525 }
1526
1527 static int ice1usb_resume(struct usb_interface *intf)
1528 {
1529 struct ice1usb *ieu = usb_get_intfdata(intf);
1530 /* FIXME */
1531 }
1532 #endif
1533
1534 static struct usb_driver ice1usb_driver = {
1535 .name = "icE1usb",
1536 .id_table = ice1usb_products,
1537 .probe = ice1usb_probe,
1538 .disconnect = ice1usb_disconnect,
1539 //.suspend = ice1usb_suspend,
1540 //.resume = ice1usb_resume,
1541 };
1542
1543
1544 /* cannot use module_usb_driver() as we have both E1 and GPS-DO drivers */
1545 static int __init __ice1usb_init(void)
1546 {
1547 int rc;
1548
1549 rc = usb_register_driver(&ice1usb_gpsdo_driver, THIS_MODULE, KBUILD_MODNAME);
1550 if (rc < 0)
1551 return rc;
1552
1553 rc = usb_register_driver(&ice1usb_driver, THIS_MODULE, KBUILD_MODNAME);
1554 if (rc < 0) {
1555 usb_deregister(&ice1usb_gpsdo_driver);
1556 return rc;
1557 }
1558
1559 return rc;
1560 }
1561
1562 static void __exit __ice1usb_exit(void)
1563 {
1564 usb_deregister(&ice1usb_gpsdo_driver);
1565 usb_deregister(&ice1usb_driver);
1566 }
1567
1568 module_init(__ice1usb_init);
1569 module_exit(__ice1usb_exit);
1570
1571 MODULE_AUTHOR("Harald Welte <[email protected]>");
1572 MODULE_DESCRIPTION("Osmocom icE1usb USB E1 interface");
1573 MODULE_LICENSE("GPL");
1574 MODULE_VERSION(VERSION);