~ubuntu-branches/ubuntu/saucy/dahdi-tools/saucy-proposed

« back to all changes in this revision

Viewing changes to xpp/xpp_fxloader

  • Committer: Package Import Robot
  • Author(s): Jackson Doak
  • Date: 2013-08-25 12:48:37 UTC
  • mfrom: (2.1.7 sid)
  • Revision ID: package-import@ubuntu.com-20130825124837-wtefi7f9dsihg8is
Tags: 1:2.7.0-1ubuntu1
* Merge from debian. Remaining changes:
  - debian/control: Added gawk as dependency for dkms build
  - debian/control: Package dahdi Depends on dahdi-dkms | dahdi-source
  - debian/control: Set ubuntu maintainer    
  - added debian/dahdi.postinst
  - debian/control: Removed Uploaders field.
  - added debian/dahdi.postinst
  - added --error-handler=init_failed to debian/rules
  

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
#!/bin/bash
2
2
 
3
3
# xpp_fxloader: load Xorcom Astribank (XPP) firmware
4
 
# $Id: xpp_fxloader 9740 2011-02-08 14:04:50Z tzafrir $
 
4
# $Id$
5
5
#
6
6
# Written by Tzafrir Cohen <tzafrir.cohen@xorcom.com>
7
7
# Copyright (C) 2006-2009, Xorcom
56
56
fi
57
57
 
58
58
debug() {
59
 
        [ "$DEBUG" != "" ] && $LOGGER "$@"
 
59
        [ "$DEBUG" != "" ] && echo >&2 "$@"
60
60
        return 0
61
61
}
62
62
 
67
67
FIRMWARE_DIR="${FIRMWARE_DIR:-/usr/share/dahdi}"
68
68
ASTRIBANK_HEXLOAD=${ASTRIBANK_HEXLOAD:-/usr/sbin/astribank_hexload}
69
69
ASTRIBANK_TOOL=${ASTRIBANK_TOOL:-/usr/sbin/astribank_tool}
 
70
XPP_CONFIG="${XPP_CONFIG:-/etc/dahdi/xpp.conf}"
70
71
XPP_UDEV_SLEEP_TIME="${XPP_UDEV_SLEEP_TIME:-15}"
71
72
 
72
 
USB_FW="${USB_FW:-USB_FW.hex}"
 
73
USB_RECOV="${USB_RECOV:-USB_RECOV.hex}"
73
74
 
74
75
if [ -r "$DEFAULTS" ]; then
75
76
        . "$DEFAULTS"
103
104
 
104
105
run_fxload() {
105
106
  sleep_if_race
106
 
  fxload -t fx2 $* 2>&1 1>/dev/null | $LOGGER
 
107
  fxload -t fx2 $* 2>&1 1>/dev/null
107
108
  status=$PIPESTATUS
108
109
  if [ $status != 0 ]; then
109
 
    $LOGGER "fxload failed with status $status"
 
110
    echo >&2 "fxload failed with status $status"
110
111
    exit 55
111
112
  fi
112
113
}
113
114
 
 
115
list_via_proc() {
 
116
        cat /proc/bus/usb/devices | egrep '^P:|^T:' | sed \
 
117
                -e '/^T:/s/ *Spd.*//' \
 
118
                -e '/^T:/s, *Lev.*Dev#= *,\t,' \
 
119
                -e '/^T:/s,Bus=,,' \
 
120
                -e '/^P:/s,[A-Za-z]\+=,,g' \
 
121
                -e '/^P:/s,\.,,g' | awk -vusb_prefix="$USB_PREFIX" '
 
122
                        /^T:/   {
 
123
                                bus=$2
 
124
                                dev=$3
 
125
                        }
 
126
                        /^P:/   {
 
127
                                vendor=$2
 
128
                                sub("0x", "", vendor);
 
129
                                prod=$3
 
130
                                sub("0x", "", product);
 
131
                                bcd=$4
 
132
                                printf("%4s/%4s/%d\t%s/%03d/%03d\n",
 
133
                                        vendor, prod, bcd, usb_prefix, bus, dev);
 
134
                        }
 
135
                        '
 
136
}
 
137
 
 
138
list_via_sysfs() {
 
139
        find /sys/bus/usb/devices -maxdepth 1 -mindepth 1 | \
 
140
                egrep -v '/usb[0-9]|:' | while read dev; do
 
141
                        (
 
142
                                cat "$dev/idVendor"
 
143
                                cat "$dev/idProduct"
 
144
                                cat "$dev/bcdDevice"
 
145
                                echo "$dev" | sed \
 
146
                                        -e 's,/sys/bus/usb/devices/,,' \
 
147
                                        -e 's,-.*,,'
 
148
                                cat "$dev/devnum"
 
149
                        ) | tr -s '\n' '\t'
 
150
                        echo ''
 
151
                done | awk -vusb_prefix="$USB_PREFIX" '{
 
152
                        printf("%4s/%4s/%d\t%s/%03d/%03d\n",
 
153
                                $1, $2, $3, usb_prefix, $4, $5);
 
154
                        }'
 
155
}
 
156
 
 
157
list_via_lsusb() {
 
158
        lsusb -v | awk -vusb_prefix="$USB_PREFIX" '
 
159
                /^Bus/ {
 
160
                        sub(":", "", $4);
 
161
                        dev = sprintf("%s/%s/%s ", usb_prefix, $2, $4);
 
162
                }
 
163
                /idVendor/  {
 
164
                        id_vendor = $2
 
165
                        sub("0x", "", id_vendor);
 
166
                }
 
167
                /idProduct/ {
 
168
                        id_product = $2
 
169
                        sub("0x", "", id_product);
 
170
                }
 
171
                /bcdDevice/ {
 
172
                        bcd_device = $2
 
173
                        sub("^0*", "", bcd_device);
 
174
                        sub("[.]", "", bcd_device);
 
175
                        printf("%s/%s/%s\t%s\n",
 
176
                                id_vendor, id_product, bcd_device, dev);
 
177
                }
 
178
                '
 
179
}
 
180
 
 
181
list_devs() {
 
182
        #echo >&2 "list_devs"
 
183
        if [ "$#" -eq 0 ]; then
 
184
                if [ -f /proc/bus/usb/devices ]; then
 
185
                        method='via_proc'
 
186
                elif [ -d /sys/bus/usb/devices ]; then
 
187
                        method='via_sysfs'
 
188
                else
 
189
                        method='via_lsusb'
 
190
                fi
 
191
        elif [ "$#" -eq 1 ]; then
 
192
                method="$1"
 
193
        else
 
194
                echo >&2 "$0: unknown list_devs method='$method'"
 
195
                exit 1
 
196
        fi
 
197
 
 
198
        case "$method" in
 
199
        via_proc|via_sysfs|via_lsusb)
 
200
                ;;
 
201
        *)
 
202
                echo >&2 "$0: unknown list_devs method='$method'"
 
203
                exit 1
 
204
                ;;
 
205
        esac
 
206
        list_$method | grep -v '^0000/0000/' | sort
 
207
}
 
208
 
 
209
filter_devs() {
 
210
        id_str="$1"
 
211
 
 
212
        #echo >&2 "filter_devs($id_str)"
 
213
        list_devs | awk -vid_str="$id_str" '{ if ($1 ~ id_str) { print } }'
 
214
}
 
215
 
 
216
usb_firmware_device() {
 
217
        id_str="$1"
 
218
        devpath="$2"
 
219
 
 
220
        case "$id_str" in
 
221
        e4e4/11[3456]0/101|e4e4/1163/101)
 
222
                fw="USB_FW.hex"
 
223
                ;;
 
224
        e4e4/116[03]/201)
 
225
                fw="USB_FW.201.hex"
 
226
                ;;
 
227
        e4e4/*)
 
228
                debug "No USB firmware for device $devpath ($id_str)"
 
229
                return
 
230
                ;;
 
231
        *)
 
232
                return
 
233
                ;;
 
234
        esac
 
235
        fw_file="$FIRMWARE_DIR/$fw"
 
236
        ver=$(awk '/\$Id:/ { print $4 }' $fw_file)
 
237
        debug "USB Firmware $fw_file (Version=$ver) into $devpath"
 
238
        run_fxload -D "$devpath" -I "$fw_file" || exit 1
 
239
}
 
240
 
114
241
run_astribank_hexload() {
115
242
        debug "Running: $ASTRIBANK_HEXLOAD $*"
116
 
        $ASTRIBANK_HEXLOAD "$@" | $LOGGER
 
243
        $ASTRIBANK_HEXLOAD "$@"
117
244
        status=$PIPESTATUS
118
245
        if [ $status != 0 ]; then
119
 
                $LOGGER "$ASTRIBANK_HEXLOAD failed with status $status"
 
246
                echo >&2 "$ASTRIBANK_HEXLOAD failed with status $status"
120
247
                exit 77
121
248
        fi
122
249
}
123
250
 
124
251
run_astribank_tool() {
125
252
        debug "Running: $ASTRIBANK_TOOL $*"
126
 
        $ASTRIBANK_TOOL "$@" | $LOGGER
 
253
        $ASTRIBANK_TOOL "$@"
127
254
        status=$PIPESTATUS
128
255
        if [ $status != 0 ]; then
129
 
                $LOGGER "$ASTRIBANK_TOOL failed with status $status"
 
256
                echo >&2 "$ASTRIBANK_TOOL failed with status $status"
130
257
                exit 77
131
258
        fi
132
259
}
133
260
 
134
 
load_usb_fw() {
135
 
  v_id=$1
136
 
  p_id=$2
137
 
  fw=$3
138
 
  
139
 
  devices=`find_dev $v_id $p_id`
140
 
  for dev in $devices
141
 
  do
142
 
    ver=$(awk '/\$Id:/ { print $4 }' $FIRMWARE_DIR/$fw)
143
 
    debug "USB Firmware $FIRMWARE_DIR/$fw (Version=$ver) into $dev"
144
 
    run_fxload -D $dev -I $FIRMWARE_DIR/$fw || exit 1
145
 
  done
 
261
usb_firmware_all_devices() {
 
262
        devs=`list_devs`
 
263
        echo "USB firmware"
 
264
        echo "$devs" | while read id_str devpath
 
265
        do
 
266
                usb_firmware_device "$id_str" "$devpath"
 
267
        done
 
268
        wait_renumeration $numdevs 'e4e4/11[3456]1/*' "usb_firmware_all_devices"
146
269
}
147
270
 
148
271
load_fw_device() {
149
 
        dev=$1
150
 
        fw=$2
 
272
        dev="$1"
 
273
        fw="$2"
151
274
        debug "FPGA loading $fw into $dev"
152
275
        run_astribank_hexload -D "$dev" -F "$FIRMWARE_DIR/$fw"
153
 
        pic_files=`echo "$FIRMWARE_DIR"/PIC_TYPE_[1-4].hex`
154
 
        debug "PIC burning into $dev: $pic_files"
155
 
        run_astribank_hexload -D "$dev" -p $pic_files
156
 
        run_astribank_tool -D "$dev" -n         # Do renumeration!
157
 
        debug "PIC burning finished $pic_files"
 
276
        case "$fw" in
 
277
        FPGA_1161*.hex)
 
278
                echo_file="$FIRMWARE_DIR/OCT6104E-256D.ima"
 
279
                law=''
 
280
                law_str='uLaw'
 
281
                abtool_output=`$ASTRIBANK_TOOL -D "$dev" -Q 2>&1`
 
282
                ec_card_type=`echo "$abtool_output" | grep 'CARD 4' | sed -e 's/.*type=//' -e 's/\..*//'`
 
283
                caps_num=`echo "$abtool_output" | grep 'ECHO ports' | sed -e 's/.*: *//'`
 
284
                if [ "$ec_card_type" = '5' ]; then
 
285
                        debug "ECHO burning into $dev: $echo_file"
 
286
                        card_type=`echo "$abtool_output" | grep 'CARD 0' | sed -e 's/.*type=//' -e 's/\..*//'`
 
287
                        case "$card_type" in
 
288
                        3)      law="-A";;
 
289
                        4)
 
290
                                pri_protocol=''
 
291
                                if [ -r "$XPP_CONFIG" ]; then
 
292
                                        pri_protocol=`awk '/^pri_protocol/ {print $2}' $XPP_CONFIG`
 
293
                                fi
 
294
                                # "E1" or empty (implied E1) means aLaw
 
295
                                if [ "$pri_protocol" != 'T1' ]; then
 
296
                                        law='-A'
 
297
                                fi
 
298
                                ;;
 
299
                        esac
 
300
                        if [ "$law" = '-A' ]; then
 
301
                                law_str="aLaw"
 
302
                        fi
 
303
                        caps_num=`echo "$abtool_output" | grep 'ECHO ports' | sed -e 's/.*: *//'`
 
304
                        debug "ECHO: 1st module is $law_str, $caps_num channels allowed."
 
305
                        if [ "$caps_num" != '0' ]; then
 
306
                                run_astribank_hexload -D "$dev" -O $law "$echo_file"
 
307
                        else
 
308
                                echo "WARNING: ECHO burning was skipped (no capabilities)"
 
309
                        fi
 
310
                fi
 
311
                pic_files=`echo "$FIRMWARE_DIR"/PIC_TYPE_[1-4].hex`
 
312
                debug "PIC burning into $dev: begin $pic_files"
 
313
                run_astribank_hexload -D "$dev" -p $pic_files
 
314
                debug "PIC burning into $dev: end $pic_files"
 
315
                ;;
 
316
        esac
 
317
        # Do renumeration!
 
318
        run_astribank_tool -D "$dev" -n > /dev/null 2>&1
 
319
        debug "Renumeration of $dev done."
158
320
}
159
321
 
160
 
#
161
 
# Use in manual loading. Parallelize loading
162
 
# firmwares to all of our devices
163
 
#
164
 
firmware_by_id() {
165
 
  v_id=$1
166
 
  p_id=$2
167
 
  fw=$3
168
 
  
169
 
  devices=`find_dev $v_id $p_id`
170
 
  childs=""
171
 
  for dev in $devices
172
 
  do
173
 
        (
174
 
        set -e
 
322
fpga_firmware_device() {
 
323
        id_str="$1"
 
324
        devpath="$2"
175
325
 
176
 
        load_fw_device "$dev" "$fw"
 
326
        id_product=`echo "$id_str" | cut -d/ -f2`
 
327
        bcd_device=`echo "$id_str" | cut -d/ -f3`
 
328
        case "$id_str" in
 
329
        e4e4/1131/101)
 
330
                fw="FPGA_FXS.hex"
 
331
                ;;
 
332
        e4e4/11[456]1/101)
 
333
                fw="FPGA_${id_product}.hex"
 
334
                ;;
 
335
        e4e4/1161/201)
 
336
                fw="FPGA_${id_product}.${bcd_device}.hex"
 
337
                ;;
 
338
        e4e4/*)
 
339
                debug "No FPGA firmware for device $devpath ($id_str)"
 
340
                return
 
341
                ;;
 
342
        *)
 
343
                return
 
344
                ;;
 
345
        esac
 
346
        debug "Loading $fw into $devpath"
 
347
        load_fw_device "$devpath" "$fw"
177
348
        sleep_if_race
178
 
        ) &
179
 
        childs="$childs $!"
180
 
        sleep 0.4
181
 
  done
182
 
  # Wait for specific childs to get their exit status
183
 
  wait $childs
184
349
}
185
350
 
186
351
numdevs() {
187
 
  v_ids="$1"
188
 
  p_ids="$2"
 
352
        id_str="$1"
189
353
 
190
 
  for v in $v_ids
191
 
  do
192
 
    (
193
 
      for p in $p_ids
194
 
      do
195
 
        find_dev $v $p
196
 
      done
197
 
    )
198
 
  done | wc -w
 
354
        #echo >&2 "numdevs($id_str)"
 
355
        filter_devs "$id_str" | wc -l
199
356
}
200
357
 
201
358
wait_renumeration() {
202
 
  num="$1"
203
 
  v_ids="$2"
204
 
  p_ids="$3"
205
 
 
206
 
  while
207
 
    n=`numdevs "$v_ids" "$p_ids"`
208
 
    [ "$num" -gt "$n" ]
209
 
  do
210
 
    echo -n "."
211
 
    sleep 1
212
 
  done
213
 
  echo "Got all $num devices"
 
359
        num="$1"
 
360
        id_str="$2"
 
361
        caller="$3"
 
362
        iter=10
 
363
 
 
364
        prev=0
 
365
        echo "Waiting renumeration ($caller)"
 
366
        while
 
367
                n=`numdevs "$id_str"`
 
368
                [ "$num" -gt "$n" ]
 
369
        do
 
370
                if [ "$prev" -lt "$n" ]; then
 
371
                        echo -n "+"
 
372
                else
 
373
                        echo -n "."
 
374
                fi
 
375
                sleep 1
 
376
                prev="$n"
 
377
                debug "wait($iter) (found $n from $num devices) ($caller)"
 
378
                if ! iter=`expr $iter - 1`; then
 
379
                        echo "Timeout (found $n from $num devices) ($caller)"
 
380
                        break;
 
381
                fi
 
382
        done
 
383
        echo "Got all $num devices ($caller)"
 
384
        sleep 1 # Let everything settle
 
385
}
 
386
 
 
387
fpga_firmware_all_devices() {
 
388
        echo "Loading FPGA firmwares"
 
389
        devs=`filter_devs 'e4e4/11[3456]1/*'`
 
390
        n=`echo "$devs" | wc -l`
 
391
        echo "$devs" | (
 
392
                while read id_str devpath; do
 
393
                        fpga_firmware_device "$id_str" "$devpath" &
 
394
                done
 
395
                sleep 1
 
396
                echo "Wait for FPGA loading processes"
 
397
                wait
 
398
                )
 
399
        wait_renumeration $numdevs 'e4e4/11[3456]2/*' "fpga_firmware_device"
214
400
}
215
401
 
216
402
reset_fpga() {
217
 
  totaldevs=`numdevs e4e4 '11[3456][0123]'`
218
 
  devices=`find_dev e4e4 '11[3456][12]'`
219
 
  debug "Reseting devices [$totaldevs devices]"
220
 
  for dev in $devices
221
 
  do
222
 
        debug "Resetting FPGA Firmware on $dev"
223
 
        sleep_if_race
224
 
        run_astribank_tool -D "$dev" -r full 2>&1 >/dev/null
225
 
  done
226
 
  if [ "$1" = 'wait' ]; then
227
 
          wait_renumeration $totaldevs e4e4 '11[3456][03]'
228
 
  fi
 
403
        devices=`filter_devs 'e4e4/11[3456][124]/*'`
 
404
        totaldevs=`numdevs 'e4e4/11[3456][124]/*'`
 
405
        echo >&2 -- "Reseting devices [$totaldevs devices]"
 
406
        echo "$devices" | grep -v '^$' | while read id_str dev
 
407
        do
 
408
                (
 
409
                        debug "Resetting FPGA Firmware on $dev"
 
410
                        sleep_if_race
 
411
                        run_astribank_tool -D "$dev" -r full >/dev/null 2>&1
 
412
                ) &
 
413
        done
 
414
        wait
 
415
        if [ "$1" = 'wait' ]; then
 
416
                wait_renumeration $totaldevs 'e4e4/11[3456][03]/*' "reset_fpga"
 
417
        fi
229
418
}
230
419
 
231
420
usage() {
266
455
reset)
267
456
        reset_fpga
268
457
        ;;
 
458
list)
 
459
        filter_devs 'e4e4/*/*'
 
460
        exit 0
 
461
        ;;
269
462
xppdetect|load|usb)
270
 
        numdevs=`numdevs e4e4 '11[3456][013]'`
271
 
        $LOGGER -- "--------- FIRMWARE LOADING: ($1) [$numdevs devices]"
 
463
        numdevs=`numdevs 'e4e4/11[3456][0134]/*'`
 
464
        echo >&2 -- "--------- FIRMWARE LOADING: ($1) [$numdevs devices]"
272
465
 
273
 
        load_usb_fw e4e4 1130 $USB_FW
274
 
        load_usb_fw e4e4 1140 $USB_FW
275
 
        load_usb_fw e4e4 1150 $USB_FW
276
 
        load_usb_fw e4e4 1160 $USB_FW
277
 
        load_usb_fw e4e4 1163 $USB_FW
278
 
        wait_renumeration $numdevs e4e4 '11[3456]1'
 
466
        usb_firmware_all_devices
279
467
        if [ "$1" != 'usb' ]
280
468
        then
281
 
                firmware_by_id e4e4 1131 FPGA_FXS.hex
282
 
                firmware_by_id e4e4 1141 FPGA_1141.hex
283
 
                firmware_by_id e4e4 1151 FPGA_1151.hex
284
 
                firmware_by_id e4e4 1161 FPGA_1161.hex
285
 
                wait_renumeration $numdevs e4e4 '11[3456]2'
 
469
                fpga_firmware_all_devices
286
470
        fi
287
471
 
288
 
        sleep 3         # Let it stabilize
289
 
        $LOGGER -- "--------- FIRMWARE IS LOADED"
 
472
        echo >&2 -- "--------- FIRMWARE IS LOADED"
290
473
        exit 0
291
474
        ;;
 
475
recover-sb)
 
476
        # Load a firmware that fixes a but which makes the Source Byte in the
 
477
        # EEPROM reset and make the device appear like a Cypress dev kit:
 
478
        load_usb_fw 04b4 8613 $USB_RECOV
 
479
        ;;
292
480
help)
293
481
        usage
294
482
        exit 0
336
524
                sleep 1
337
525
        done
338
526
        if [ $usb_dev_writable != 1 ]; then
339
 
                $LOGGER "Device $DEVICE not writable. Can't load firmware."
 
527
                echo >&2 "Device $DEVICE not writable. Can't load firmware."
340
528
                return;
341
529
        fi
342
530
 
343
 
        $LOGGER "Trying to find what to do for product $PRODUCT, device $DEVICE"
344
 
        prod_id=`echo "$PRODUCT" | cut -d/ -f2`
 
531
        echo >&2 "Trying to find what to do for product $PRODUCT, device $DEVICE"
345
532
        case "$PRODUCT" in
 
533
        4b4/8613/*)
 
534
                # This case is for a potentially-broken Astribank.
 
535
                # In most systems you should not set udev rules for those to
 
536
                # get here, as this is actually the ID of a Cypress dev-kit:
 
537
                FIRM_USB="$FIRMWARE_DIR/$USB_RECOV"
 
538
                echo >&2 "Loading recovery firmware '$FIRM_USB' into '$DEVICE'"
 
539
                run_fxload -D "$DEVICE" -I "$FIRM_USB"
 
540
                ;;
346
541
        e4e4/11[3456]0/*|e4e4/1163/*)
347
 
                FIRM_USB="$FIRMWARE_DIR/$USB_FW"
348
 
                $LOGGER "Loading firmware '$FIRM_USB' into '$DEVICE'"
349
 
                run_fxload -D "$DEVICE" -I "$FIRM_USB"
 
542
                usb_firmware_device "$PRODUCT" "$DEVICE"
350
543
                ;;
351
544
        e4e4/11[3456]1/*)
352
545
                # There are potentially two separate udev events, for
353
546
                # each of the two endpoints. Ignore the first interface:
354
547
                case "$DEVPATH" in *.0) exit 0;; esac
355
 
                if [ "$prod_id" = 1131 ]; then
356
 
                        FIRM_FPGA="FPGA_FXS.hex"        # Legacy
357
 
                else
358
 
                        FIRM_FPGA="FPGA_$prod_id.hex"
359
 
                fi
360
548
                sleep_if_race
361
 
                load_fw_device "$DEVICE" "$FIRM_FPGA"
 
549
                fpga_firmware_device "$PRODUCT" "$DEVICE" &
 
550
                wait    # parallel firmware loading
362
551
                ;;
363
552
        esac    
364
553
}
365
554
 
366
 
udev_delayed_load &
 
555
udev_delayed_load 2>&1 | $LOGGER &
367
556