merge of '49118a4c6035353c0f8cf1aa30297dd36e43241f'
[vuplus_openembedded] / packages / bluez / bluez-utils-3.33 / hciattach-ti-bts.patch
1 --- bluez-utils-3.1/tools/hciattach.c.orig      2006-07-23 14:02:14.000000000 +0200
2 +++ bluez-utils-3.1/tools/hciattach.c   2006-07-23 14:06:29.000000000 +0200
3 @@ -60,6 +60,8 @@
4  #define HCI_UART_3WIRE 2
5  #define HCI_UART_H4DS  3
6  
7 +#include "ti_bts.h"
8 +
9  struct uart_t {
10         char *type;
11         int  m_id;
12 @@ -70,6 +72,7 @@
13         int  flags;
14         char *bdaddr;
15         int  (*init) (int fd, struct uart_t *u, struct termios *ti);
16 +       char *bts;      /* bluetooth script */
17  };
18  
19  #define FLOW_CTL       0x0001
20 @@ -279,6 +282,114 @@
21         return 0;
22  }
23  
24 +static int brf6150(int fd, struct uart_t *u, struct termios *ti)
25 +{
26 +       bts_t *bfp;
27 +       int i;
28 +       unsigned long vers;
29 +       unsigned char actionbuf[256];
30 +       unsigned char resp[128];                /* Response */
31 +       unsigned long count;
32 +       unsigned short atype;
33 +
34 +       if (u->bts == NULL)     /* no script, ignore */
35 +               return 0;
36 +
37 +       bfp = bts_load_script( u->bts, &vers );
38 +       if (bfp == NULL)
39 +               return -1;
40 +
41 +       fprintf( stderr, "Loading BTS script version %lu\n", vers );
42 +
43 +       while ((count = bts_next_action( bfp, actionbuf,
44 +                       sizeof actionbuf - 1, &atype )) != 0) {
45 +               if (atype == ACTION_REMARKS) {
46 +                       if (actionbuf[0] != 0)
47 +                               fprintf( stderr, "%s\n", actionbuf );
48 +               }
49 +               else if (atype == ACTION_SEND_COMMAND) {
50 +#if 0
51 +                       fprintf( stderr, "ACTION_SEND_COMMAND: ", (int)atype );
52 +                       for (i=0; i<count; i++) {
53 +                               fprintf( stderr, "0x%02x ", actionbuf[i] );
54 +                       }
55 +                       fprintf( stderr, "\n" );
56 +#endif
57 +                       int n;
58 +                       n = write(fd, actionbuf, count);
59 +                       if (n < 0 || n < count) {
60 +                               perror("Failed to write TI action command");
61 +                               return -1;
62 +                       }
63 +               }
64 +               else if (atype == ACTION_WAIT_EVENT) {
65 +                       action_wait_t *wait = (action_wait_t *)actionbuf;
66 +#if 0
67 +                       fprintf( stderr, "ACTION_WAIT_EVENT: %u msec, %u size, data = ", wait->msec, wait->size );
68 +                       for (i=0; i<wait->size; i++) {
69 +                               fprintf( stderr, "0x%02x ", wait->data[i] );
70 +                       }
71 +                       fprintf( stderr, "\n" );
72 +#endif
73 +                       usleep(wait->msec);     /* seems they give usec, not msec */
74 +                       /* Read reply. */
75 +                       if ((count = read_hci_event(fd, resp, sizeof resp)) < 0) {
76 +                               perror("Failed to read TI command response");
77 +                               return -1;
78 +                       }
79 +                       if (count < wait->size) {
80 +                               fprintf( stderr, "TI command response is short.");
81 +                       }
82 +                       for (i=0; i<wait->size; i++) {
83 +                               if (i == 3) continue;   /* ignore */
84 +                               if (resp[i] != wait->data[i]) {
85 +                                       fprintf( stderr, "TI command response does not match expected result.\n" );
86 +                               }
87 +                       }
88 +               }
89 +               else if (atype == ACTION_SERIAL_PORT_PARAMETERS) {
90 +                       action_serial_t *sercmd = (action_serial_t *)actionbuf;
91 +
92 +                       /* Set actual baudrate */
93 +                       fprintf( stderr,
94 +                               "BTS changing baud rate to %u, flow control to %u\n",
95 +                               sercmd->baud, sercmd->flow_control );
96 +
97 +                       tcflush(fd, TCIOFLUSH);
98 +
99 +                       if (sercmd->flow_control)
100 +                               ti->c_cflag |= CRTSCTS;
101 +                       else
102 +                               ti->c_cflag &= ~CRTSCTS;
103 +                       if (tcsetattr(fd, TCSANOW, ti) < 0) {
104 +                               perror("Can't set port settings");
105 +                               return -1;
106 +                       }
107 +
108 +                       u->speed = sercmd->baud;
109 +
110 +                       tcflush(fd, TCIOFLUSH);
111 +                       if (set_speed(fd, ti, sercmd->baud) < 0) {
112 +                               perror("Can't set baud rate");
113 +                               return -1;
114 +                       }
115 +               }
116 +               else if (atype == ACTION_DELAY) {
117 +                       action_delay_t *delay = (action_delay_t *)actionbuf;
118 +                       usleep(delay->msec);    /* seems they give usec, not msec */
119 +               }
120 +               else {
121 +                       fprintf( stderr, "BTS action type = %d: ", (int)atype );
122 +                       for (i=0; i<count; i++) {
123 +                               fprintf( stderr, "0x%02x ", actionbuf[i] );
124 +                       }
125 +                       fprintf( stderr, "\n" );
126 +               }
127 +       }
128 +       bts_unload_script( bfp );
129 +       return 0;
130 +}
131 +
132  static int texas(int fd, struct uart_t *u, struct termios *ti)
133  {
134         struct timespec tm = {0, 50000};
135 @@ -328,6 +439,17 @@
136         /* Print LMP subversion */
137         fprintf(stderr, "Texas module LMP sub-version : 0x%02x%02x\n", resp[14] & 0xFF, resp[13] & 0xFF);
138  
139 +       if ((resp[14] >> 2) == 3) {
140 +               /* BRF6150 */
141 +               int err;
142 +
143 +               nanosleep(&tm, NULL);
144 +               if ((err = brf6150(fd, u, ti)) != 0) {
145 +                       fprintf(stderr, "Texas module script failed (err=%d)\n", err);
146 +                       return -1;
147 +               }
148 +       }
149 +       
150         nanosleep(&tm, NULL);
151         return 0;
152  }
153 @@ -1204,7 +1326,7 @@
154  {
155         printf("hciattach - HCI UART driver initialization utility\n");
156         printf("Usage:\n");
157 -       printf("\thciattach [-n] [-p] [-b] [-t timeout] [-s initial_speed] <tty> <type | id> [speed] [flow|noflow] [bdaddr]\n");
158 +       printf("\thciattach [-n] [-p] [-b] [-t timeout] [-s initial_speed] [-S bts-script] <tty> <type | id> [speed] [flow|noflow] [bdaddr]\n");
159         printf("\thciattach -l\n");
160  }
161  
162 @@ -1219,11 +1341,12 @@
163         struct sigaction sa;
164         struct pollfd p;
165         char dev[PATH_MAX];
166 +       char *bts = NULL;
167  
168         detach = 1;
169         printpid = 0;
170  
171 -       while ((opt=getopt(argc, argv, "bnpt:s:l")) != EOF) {
172 +       while ((opt=getopt(argc, argv, "bnpt:s:S:l")) != EOF) {
173                 switch(opt) {
174                 case 'b':
175                         send_break = 1;
176 @@ -1245,6 +1368,10 @@
177                         init_speed = atoi(optarg);
178                         break;
179  
180 +               case 'S':
181 +                       bts = optarg;
182 +                       break;
183 +
184                 case 'l':
185                         for (i = 0; uart[i].type; i++) {
186                                 printf("%-10s0x%04x,0x%04x\n", uart[i].type,
187 @@ -1320,6 +1447,8 @@
188         if (init_speed)
189                 u->init_speed = init_speed;
190  
191 +       u->bts = bts;
192 +
193         memset(&sa, 0, sizeof(sa));
194         sa.sa_flags   = SA_NOCLDSTOP;
195         sa.sa_handler = sig_alarm;
196 --- bluez-utils-3.1/tools/ti_bts.h.orig 2006-07-23 14:07:26.000000000 +0200
197 +++ bluez-utils-3.1/tools/ti_bts.h      2006-07-23 14:07:46.000000000 +0200
198 @@ -0,0 +1,116 @@
199 +/*
200 + * Copyright (c) 2005 Texas Instruments, Inc.
201 + *    Ported by SDG Systems, LLC
202 + *
203 + *  This program is free software; you can redistribute it and/or modify
204 + *  it under the terms of the GNU General Public License version 2 as
205 + *  published by the Free Software Foundation;
206 + *
207 + *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
208 + *  OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
209 + *  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS.
210 + *  IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) AND AUTHOR(S) BE LIABLE FOR ANY
211 + *  CLAIM, OR ANY SPECIAL INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES 
212 + *  WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 
213 + *  ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 
214 + *  OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
215 + *
216 + *  ALL LIABILITY, INCLUDING LIABILITY FOR INFRINGEMENT OF ANY PATENTS, 
217 + *  COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS, RELATING TO USE OF THIS 
218 + *  SOFTWARE IS DISCLAIMED.
219 + *
220 + */
221 +
222 +#ifndef BT_SCRIPT_H
223 +#define BT_SCRIPT_H
224 +
225 +#ifdef __cplusplus
226 +extern "C" {
227 +#endif
228 +
229 +/*
230 + * Define the interface of Bluetooth Script
231 + */
232 +
233 +typedef void bts_t;
234 +
235 +
236 +#define ACTION_SEND_COMMAND             1   /* Send out raw data (as is) */
237 +#define ACTION_WAIT_EVENT               2   /* Wait for data */
238 +#define ACTION_SERIAL_PORT_PARAMETERS   3   
239 +#define ACTION_DELAY                    4   
240 +#define ACTION_RUN_SCRIPT               5   
241 +#define ACTION_REMARKS                  6
242 +
243 +/*
244 + * Structure for ACTION_SEND_COMMAND
245 + */
246 +typedef struct tagCActionCommand
247 +{
248 +    unsigned char data[1]; /* Data to send */
249 +} action_command_t;
250 +
251 +/*
252 + * Structure for ACTION_WAIT_EVENT
253 + */
254 +typedef struct tagCActionWaitEvent
255 +{
256 +    unsigned long msec; /* in milliseconds */
257 +    unsigned long size;
258 +    unsigned char data[1]; /* Data to wait for */
259 +} action_wait_t;
260 +
261 +
262 +/*
263 + * Structure for ACTION_SERIAL_PORT_PARAMETERS
264 + */
265 +typedef struct tagCActionSerialPortParameters
266 +{
267 +    unsigned long baud;
268 +    unsigned long flow_control;
269 +} action_serial_t;
270 +
271 +/* Flow Control Type */
272 +#define FCT_NONE        0
273 +#define FCT_HARDWARE    1
274 +
275 +#define DONT_CHANGE     0xFFFFFFFF  /* For both baud rate and flow control */
276 +
277 +
278 +/*
279 + * Structure for ACTION_DELAY
280 + */
281 +typedef struct tagCActionDelay
282 +{
283 +    unsigned long msec; /* in milliseconds */
284 +} action_delay_t;
285 +
286 +/*
287 + * Structure for ACTION_RUN_SCRIPT
288 + */
289 +typedef struct tagCActionRunScript
290 +{
291 +    char filename[1];
292 +} action_run_t;
293 +
294 +/*
295 + * Structure for ACTION_REMARKS
296 + */
297 +typedef struct tagCActionRemarks
298 +{
299 +    char m_szRemarks[1];
300 +} action_remarks_t;
301 +
302 +
303 +const char *cis_create_filename(const unsigned char* cmdparms);
304 +bts_t * bts_load_script(const char* fname, unsigned long* version);
305 +unsigned long bts_next_action(const bts_t* bts_fp, unsigned char* action_buf,
306 +       unsigned long nMaxSize, unsigned short* ptype);
307 +void bts_unload_script(bts_t* bts_fp);
308 +
309 +#ifdef __cplusplus
310 +};
311 +#endif
312 +
313 +#endif /* BT_SCRIPT_H */
314 +
315 --- bluez-utils-3.1/tools/ti_bts.c.orig 2006-07-23 14:07:28.000000000 +0200
316 +++ bluez-utils-3.1/tools/ti_bts.c      2006-07-23 14:07:46.000000000 +0200
317 @@ -0,0 +1,149 @@
318 +/*
319 + * Copyright (c) 2005 Texas Instruments, Inc.
320 + *    Ported by SDG Systems, LLC
321 + *
322 + *  This program is free software; you can redistribute it and/or modify
323 + *  it under the terms of the GNU General Public License version 2 as
324 + *  published by the Free Software Foundation;
325 + *
326 + *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
327 + *  OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
328 + *  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS.
329 + *  IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) AND AUTHOR(S) BE LIABLE FOR ANY
330 + *  CLAIM, OR ANY SPECIAL INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES 
331 + *  WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 
332 + *  ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 
333 + *  OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
334 + *
335 + *  ALL LIABILITY, INCLUDING LIABILITY FOR INFRINGEMENT OF ANY PATENTS, 
336 + *  COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS, RELATING TO USE OF THIS 
337 + *  SOFTWARE IS DISCLAIMED.
338 + *
339 + */
340 +
341 +
342 +#include <stdio.h>
343 +#include <stdlib.h>
344 +#include "ti_bts.h"
345 +
346 +#ifndef MAKEWORD
347 +#define MAKEWORD(a, b)      ((unsigned short)(((unsigned char)(a)) | ((unsigned short)((unsigned char)(b))) << 8))
348 +#endif
349 +
350 +#define TI_MANUFACTURER_ID  13
351 +
352 +/*
353 + * Common Init Script specific
354 + */
355 +const char *
356 +cis_create_filename(const unsigned char* cmdparms)
357 +{
358 +    static char bts_file[50];
359 +
360 +    /* Check for TI's id */
361 +    unsigned short manfid = MAKEWORD(cmdparms[8], cmdparms[9]);
362 +
363 +    if (TI_MANUFACTURER_ID == manfid) {
364 +        unsigned short version = MAKEWORD(cmdparms[10], cmdparms[11]);
365 +        
366 +        unsigned short chip =  (version & 0x7C00) >> 10;
367 +        unsigned short min_ver = (version & 0x007F);
368 +        unsigned short maj_ver = (version & 0x0380) >> 7;
369 +
370 +        if (0 != (version & 0x8000)) {
371 +            maj_ver |= 0x0008;
372 +        }
373 +        
374 +        sprintf( bts_file, "TIInit_%d.%d.%d.bts", 
375 +            (int)chip, (int)maj_ver, (int)min_ver);
376 +
377 +        return &bts_file[0];
378 +    }
379 +    return NULL;
380 +}
381 +
382 +typedef struct tagCHeader 
383 +{
384 +    unsigned long magic;
385 +    unsigned long version;
386 +    unsigned char future[24];
387 +} cheader_t;
388 +
389 +
390 +/* The value 0x42535442 stands for (in ASCII) BTSB */
391 +/* which is Bluetooth Script Binary */
392 +#define FILE_HEADER_MAGIC   0x42535442
393 +
394 +
395 +bts_t *
396 +bts_load_script(const char* fname, unsigned long* version)
397 +{
398 +    bts_t* bts = NULL;
399 +    FILE* fp = fopen(fname, "rb");
400 +
401 +    if (NULL != fp) {
402 +        /* Read header */
403 +        cheader_t header;
404 +
405 +        /* Read header */
406 +        if (1 == fread(&header, sizeof(header), 1, fp)) {
407 +            /* Check magic number for correctness */
408 +            if (header.magic == FILE_HEADER_MAGIC) {
409 +                /* If user wants the version number */
410 +                if (NULL != version) {
411 +                    *version = header.version;
412 +                }
413 +                bts = (bts_t*)fp;
414 +            }
415 +        }
416 +        /* If failed reading the file, close it */
417 +        if (NULL == bts) {
418 +            fclose(fp);
419 +        }
420 +    }
421 +    return bts;
422 +}
423 +
424 +unsigned long
425 +bts_next_action(const bts_t* bts_fp, unsigned char* action_buf,
426 +    unsigned long nMaxSize, unsigned short* ptype)
427 +{
428 +    unsigned long bytes = 0;
429 +    FILE* fp = (FILE*)bts_fp;
430 +    unsigned char action_hdr[4];
431 +
432 +    if (bts_fp == NULL)
433 +        return 0;
434 +
435 +    /* Each Action has the following: */
436 +    /* UINT16 type of this action */
437 +    /* UINT16 size of rest */
438 +    /* BYTE[] action buffer (for HCI, includes the type byte e.g. 1 for hci command) */
439 +
440 +    if (1 == fread(&action_hdr[0], sizeof(action_hdr), 1, fp)) {
441 +        unsigned short type = *(unsigned short*)&action_hdr[0];
442 +        unsigned short size = *(unsigned short*)&action_hdr[2];
443 +
444 +        if (size <= nMaxSize) {
445 +            int nread = fread(action_buf, sizeof(action_buf[0]), size, fp);
446 +
447 +            if (nread == size) {
448 +                *ptype = type;
449 +                bytes = (unsigned long)size;
450 +            }
451 +        }
452 +    }
453 +
454 +    return bytes;
455 +}
456 +
457 +void
458 +bts_unload_script(bts_t* bts_fp)
459 +{
460 +    FILE* fp = (FILE*)bts_fp;
461 +
462 +    if (NULL != fp) {
463 +        fclose(fp);
464 +    }
465 +}
466 +
467 --- bluez-utils-3.1/tools/Makefile.am.orig      2006-07-23 14:06:59.000000000 +0200
468 +++ bluez-utils-3.1/tools/Makefile.am   2006-07-23 14:07:18.000000000 +0200
469 @@ -45,7 +45,7 @@
470  
471  noinst_PROGRAMS = hcisecfilter ppporc
472  
473 -hciattach_SOURCES = hciattach.c hciattach_st.c
474 +hciattach_SOURCES = hciattach.c hciattach_st.c ti_bts.h ti_bts.c
475  hciattach_LDADD = @BLUEZ_LIBS@
476  
477  hciconfig_SOURCES = hciconfig.c csr.h csr.c