]> pilppa.org Git - familiar-h63xx-build.git/blob - org.handhelds.familiar/packages/linux/openzaurus-pxa-2.4.18-rmk7-pxa3-embedix20031107/bluetooth-2.4.18-mh15.patch
OE tree imported from monotone branch org.openembedded.oz354fam083 at revision 8b12e3...
[familiar-h63xx-build.git] / org.handhelds.familiar / packages / linux / openzaurus-pxa-2.4.18-rmk7-pxa3-embedix20031107 / bluetooth-2.4.18-mh15.patch
1 diff -urN linux-2.4.18/arch/alpha/config.in linux-2.4.18-mh15/arch/alpha/config.in
2 --- linux-2.4.18/arch/alpha/config.in   2001-11-21 00:49:31.000000000 +0100
3 +++ linux-2.4.18-mh15/arch/alpha/config.in      2004-08-01 16:26:22.000000000 +0200
4 @@ -371,9 +371,7 @@
5  source drivers/usb/Config.in
6  source drivers/input/Config.in
7  
8 -if [ "$CONFIG_EXPERIMENTAL" = "y" ]; then
9 -   source net/bluetooth/Config.in
10 -fi
11 +source net/bluetooth/Config.in
12  
13  mainmenu_option next_comment
14  comment 'Kernel hacking'
15 diff -urN linux-2.4.18/arch/arm/config.in linux-2.4.18-mh15/arch/arm/config.in
16 --- linux-2.4.18/arch/arm/config.in     2001-11-09 22:58:02.000000000 +0100
17 +++ linux-2.4.18-mh15/arch/arm/config.in        2004-08-01 16:26:22.000000000 +0200
18 @@ -584,9 +584,7 @@
19  
20  source drivers/usb/Config.in
21  
22 -if [ "$CONFIG_EXPERIMENTAL" = "y" ]; then
23 -   source net/bluetooth/Config.in
24 -fi
25 +source net/bluetooth/Config.in
26  
27  mainmenu_option next_comment
28  comment 'Kernel hacking'
29 diff -urN linux-2.4.18/arch/i386/config.in linux-2.4.18-mh15/arch/i386/config.in
30 --- linux-2.4.18/arch/i386/config.in    2002-02-25 20:37:52.000000000 +0100
31 +++ linux-2.4.18-mh15/arch/i386/config.in       2004-08-01 16:26:22.000000000 +0200
32 @@ -407,9 +407,7 @@
33  
34  source drivers/usb/Config.in
35  
36 -if [ "$CONFIG_EXPERIMENTAL" = "y" ]; then
37 -   source net/bluetooth/Config.in
38 -fi
39 +source net/bluetooth/Config.in
40  
41  mainmenu_option next_comment
42  comment 'Kernel hacking'
43 diff -urN linux-2.4.18/arch/ppc/config.in linux-2.4.18-mh15/arch/ppc/config.in
44 --- linux-2.4.18/arch/ppc/config.in     2002-02-25 20:37:55.000000000 +0100
45 +++ linux-2.4.18-mh15/arch/ppc/config.in        2004-08-01 16:26:22.000000000 +0200
46 @@ -389,9 +389,7 @@
47  
48  source drivers/usb/Config.in
49  
50 -if [ "$CONFIG_EXPERIMENTAL" = "y" ]; then
51 -   source net/bluetooth/Config.in
52 -fi
53 +source net/bluetooth/Config.in
54  
55  mainmenu_option next_comment
56  comment 'Kernel hacking'
57 diff -urN linux-2.4.18/arch/sparc/config.in linux-2.4.18-mh15/arch/sparc/config.in
58 --- linux-2.4.18/arch/sparc/config.in   2001-06-12 04:15:27.000000000 +0200
59 +++ linux-2.4.18-mh15/arch/sparc/config.in      2004-08-01 16:26:22.000000000 +0200
60 @@ -251,9 +251,7 @@
61  
62  source fs/Config.in
63  
64 -if [ "$CONFIG_EXPERIMENTAL" = "y" ]; then
65 -   source net/bluetooth/Config.in
66 -fi
67 +source net/bluetooth/Config.in
68  
69  mainmenu_option next_comment
70  comment 'Watchdog'
71 diff -urN linux-2.4.18/arch/sparc64/config.in linux-2.4.18-mh15/arch/sparc64/config.in
72 --- linux-2.4.18/arch/sparc64/config.in 2001-12-21 18:41:53.000000000 +0100
73 +++ linux-2.4.18-mh15/arch/sparc64/config.in    2004-08-01 16:26:22.000000000 +0200
74 @@ -283,9 +283,7 @@
75  
76  source drivers/usb/Config.in
77  
78 -if [ "$CONFIG_EXPERIMENTAL" = "y" ]; then
79 -   source net/bluetooth/Config.in
80 -fi
81 +source net/bluetooth/Config.in
82  
83  mainmenu_option next_comment
84  comment 'Watchdog'
85 diff -urN linux-2.4.18/arch/sparc64/kernel/ioctl32.c linux-2.4.18-mh15/arch/sparc64/kernel/ioctl32.c
86 --- linux-2.4.18/arch/sparc64/kernel/ioctl32.c  2002-02-25 20:37:56.000000000 +0100
87 +++ linux-2.4.18-mh15/arch/sparc64/kernel/ioctl32.c     2004-08-01 16:26:23.000000000 +0200
88 @@ -92,6 +92,7 @@
89  
90  #include <net/bluetooth/bluetooth.h>
91  #include <net/bluetooth/hci.h>
92 +#include <net/bluetooth/rfcomm.h>
93  
94  #include <linux/usb.h>
95  #include <linux/usbdevice_fs.h>
96 @@ -3822,6 +3823,15 @@
97         return err;
98  }
99  
100 +/* Bluetooth ioctls */
101 +#define HCIUARTSETPROTO        _IOW('U', 200, int)
102 +#define HCIUARTGETPROTO        _IOR('U', 201, int)
103 +
104 +#define BNEPCONNADD    _IOW('B', 200, int)
105 +#define BNEPCONNDEL    _IOW('B', 201, int)
106 +#define BNEPGETCONNLIST        _IOR('B', 210, int)
107 +#define BNEPGETCONNINFO        _IOR('B', 211, int)
108 +
109  struct mtd_oob_buf32 {
110         u32 start;
111         u32 length;
112 @@ -3878,6 +3888,16 @@
113         return ((0 == ret) ? 0 : -EFAULT);
114  }      
115  
116 +#define CMTPCONNADD    _IOW('C', 200, int)
117 +#define CMTPCONNDEL    _IOW('C', 201, int)
118 +#define CMTPGETCONNLIST        _IOR('C', 210, int)
119 +#define CMTPGETCONNINFO        _IOR('C', 211, int)
120 +
121 +#define HIDPCONNADD    _IOW('H', 200, int)
122 +#define HIDPCONNDEL    _IOW('H', 201, int)
123 +#define HIDPGETCONNLIST        _IOR('H', 210, int)
124 +#define HIDPGETCONNINFO        _IOR('H', 211, int)
125 +
126  struct ioctl_trans {
127         unsigned int cmd;
128         unsigned int handler;
129 @@ -4540,6 +4560,25 @@
130  COMPATIBLE_IOCTL(HCISETSCAN)
131  COMPATIBLE_IOCTL(HCISETAUTH)
132  COMPATIBLE_IOCTL(HCIINQUIRY)
133 +COMPATIBLE_IOCTL(HCIUARTSETPROTO)
134 +COMPATIBLE_IOCTL(HCIUARTGETPROTO)
135 +COMPATIBLE_IOCTL(RFCOMMCREATEDEV)
136 +COMPATIBLE_IOCTL(RFCOMMRELEASEDEV)
137 +COMPATIBLE_IOCTL(RFCOMMGETDEVLIST)
138 +COMPATIBLE_IOCTL(RFCOMMGETDEVINFO)
139 +COMPATIBLE_IOCTL(RFCOMMSTEALDLC)
140 +COMPATIBLE_IOCTL(BNEPCONNADD)
141 +COMPATIBLE_IOCTL(BNEPCONNDEL)
142 +COMPATIBLE_IOCTL(BNEPGETCONNLIST)
143 +COMPATIBLE_IOCTL(BNEPGETCONNINFO)
144 +COMPATIBLE_IOCTL(CMTPCONNADD)
145 +COMPATIBLE_IOCTL(CMTPCONNDEL)
146 +COMPATIBLE_IOCTL(CMTPGETCONNLIST)
147 +COMPATIBLE_IOCTL(CMTPGETCONNINFO)
148 +COMPATIBLE_IOCTL(HIDPCONNADD)
149 +COMPATIBLE_IOCTL(HIDPCONNDEL)
150 +COMPATIBLE_IOCTL(HIDPGETCONNLIST)
151 +COMPATIBLE_IOCTL(HIDPGETCONNINFO)
152  /* Misc. */
153  COMPATIBLE_IOCTL(0x41545900)           /* ATYIO_CLKR */
154  COMPATIBLE_IOCTL(0x41545901)           /* ATYIO_CLKW */
155 diff -urN linux-2.4.18/CREDITS linux-2.4.18-mh15/CREDITS
156 --- linux-2.4.18/CREDITS        2002-02-25 20:37:50.000000000 +0100
157 +++ linux-2.4.18-mh15/CREDITS   2004-08-01 16:26:23.000000000 +0200
158 @@ -1317,6 +1317,16 @@
159  S: Provo, Utah 84606-5607
160  S: USA
161  
162 +N: Marcel Holtmann
163 +E: marcel@holtmann.org
164 +W: http://www.holtmann.org
165 +D: Maintainer of the Linux Bluetooth Subsystem
166 +D: Author and maintainer of the various Bluetooth HCI drivers
167 +D: Author and maintainer of the CAPI message transport protocol driver
168 +D: Author and maintainer of the Bluetooth HID protocol driver
169 +D: Various other Bluetooth related patches, cleanups and fixes
170 +S: Germany
171 +
172  N: Rob W. W. Hooft
173  E: hooft@EMBL-Heidelberg.DE
174  D: Shared libs for graphics-tools and for the f2c compiler
175 @@ -2546,6 +2556,7 @@
176  N: Aristeu Sergio Rozanski Filho
177  E: aris@conectiva.com.br
178  D: Support for EtherExpress 10 ISA (i82595) in eepro driver
179 +D: User level driver support for input
180  S: Conectiva S.A.
181  S: R. Tocantins, 89 - Cristo Rei
182  S: 80050-430 - Curitiba - Paraná
183 diff -urN linux-2.4.18/Documentation/Configure.help linux-2.4.18-mh15/Documentation/Configure.help
184 --- linux-2.4.18/Documentation/Configure.help   2002-02-25 20:37:51.000000000 +0100
185 +++ linux-2.4.18-mh15/Documentation/Configure.help      2004-08-01 16:26:23.000000000 +0200
186 @@ -2824,14 +2824,6 @@
187  
188    If unsure, say N.
189  
190 -HCI EMU (virtual device) driver
191 -CONFIG_BLUEZ_HCIEMU
192 -  Bluetooth Virtual HCI device driver.
193 -  This driver is required if you want to use HCI Emulation software.
194 -
195 -  Say Y here to compile support for Virtual HCI devices into the
196 -  kernel or say M to compile it as module (hci_usb.o).
197 -
198  # Choice: alphatype
199  Alpha system type
200  CONFIG_ALPHA_GENERIC
201 @@ -11037,6 +11029,12 @@
202  
203    If unsure, say N.
204  
205 +Hotplug firmware loading support (EXPERIMENTAL)
206 +CONFIG_FW_LOADER
207 +  This option is provided for the case where no in-kernel-tree modules require
208 +  hotplug firmware loading support, but a module built outside the kernel tree
209 +  does.
210 +
211  Use PCI shared memory for NIC registers
212  CONFIG_TULIP_MMIO
213    Use PCI shared memory for the NIC registers, rather than going through 
214 @@ -12896,6 +12894,15 @@
215    accessible under char device 13:64+ - /dev/input/eventX in a generic
216    way.  This is the future ...
217  
218 +CONFIG_INPUT_UINPUT
219 +  Say Y here if you want to support user level drivers for input
220 +  subsystem accessible under char device 10:223 - /dev/input/uinput.
221 +
222 +  This driver is also available as a module ( = code which can be
223 +  inserted in and removed from the running kernel whenever you want).
224 +  The module will be called uinput.o.  If you want to compile it as a
225 +  module, say M here and read <file:Documentation/modules.txt>.            
226 +
227  USB Scanner support
228  CONFIG_USB_SCANNER
229    Say Y here if you want to connect a USB scanner to your computer's
230 @@ -19870,19 +19877,22 @@
231    Bluetooth can be found at <http://www.bluetooth.com/>.
232  
233    Linux Bluetooth subsystem consist of several layers:
234 -               HCI Core (device and connection manager, scheduler)
235 -               HCI Device drivers (interface to the hardware)
236 -               L2CAP Module (L2CAP protocol)
237 +               BlueZ Core (HCI device and connection manager, scheduler)
238 +               HCI Device drivers (Interface to the hardware)
239 +               SCO Module (SCO audio links)
240 +               L2CAP Module (Logical Link Control and Adaptation Protocol)
241 +               RFCOMM Module (RFCOMM Protocol)
242 +               BNEP Module (Bluetooth Network Encapsulation Protocol)
243 +               CMTP Module (CAPI Message Transport Protocol)
244 +               HIDP Module (Human Interface Device Protocol)
245  
246 -  Say Y here to enable Linux Bluetooth support and to build HCI Core
247 -  layer.
248 +  Say Y here to compile Bluetooth support into the kernel or say M to
249 +  compile it as module (bluez.o).
250  
251    To use Linux Bluetooth subsystem, you will need several user-space
252    utilities like hciconfig and hcid.  These utilities and updates to
253    Bluetooth kernel modules are provided in the BlueZ package.
254 -  For more information, see <http://bluez.sourceforge.net/>.
255 -
256 -  If you want to compile HCI Core as module (hci.o) say M here.
257 +  For more information, see <http://www.bluez.org/>.
258  
259  L2CAP protocol support
260  CONFIG_BLUEZ_L2CAP
261 @@ -19893,15 +19903,96 @@
262    Say Y here to compile L2CAP support into the kernel or say M to
263    compile it as module (l2cap.o).
264  
265 +SCO links support
266 +CONFIG_BLUEZ_SCO
267 +  SCO link provides voice transport over Bluetooth.  SCO support is
268 +  required for voice applications like Headset and Audio.
269 +
270 +  Say Y here to compile SCO support into the kernel or say M to
271 +  compile it as module (sco.o).
272 +
273 +RFCOMM protocol support
274 +CONFIG_BLUEZ_RFCOMM
275 +  RFCOMM provides connection oriented stream transport.  RFCOMM
276 +  support is required for Dialup Networking, OBEX and other Bluetooth
277 +  applications.
278 +
279 +  Say Y here to compile RFCOMM support into the kernel or say M to
280 +  compile it as module (rfcomm.o).
281 +
282 +RFCOMM TTY emulation support
283 +CONFIG_BLUEZ_RFCOMM_TTY
284 +  This option enables TTY emulation support for RFCOMM channels.
285 +
286 +BNEP protocol support
287 +CONFIG_BLUEZ_BNEP
288 +  BNEP (Bluetooth Network Encapsulation Protocol) is Ethernet
289 +  emulation layer on top of Bluetooth.  BNEP is required for
290 +  Bluetooth PAN (Personal Area Network).
291 +
292 +  Say Y here to compile BNEP support into the kernel or say M to
293 +  compile it as module (bnep.o).
294 +
295 +BNEP multicast filter support
296 +CONFIG_BLUEZ_BNEP_MC_FILTER
297 +  This option enables the multicast filter support for BNEP.
298 +
299 +BNEP protocol filter support
300 +CONFIG_BLUEZ_BNEP_PROTO_FILTER
301 +  This option enables the protocol filter support for BNEP.
302 +
303 +CMTP protocol support
304 +CONFIG_BLUEZ_CMTP
305 +  CMTP (CAPI Message Transport Protocol) is a transport layer
306 +  for CAPI messages.  CMTP is required for the Bluetooth Common
307 +  ISDN Access Profile.
308 +
309 +  Say Y here to compile CMTP support into the kernel or say M to
310 +  compile it as module (cmtp.o).
311 +
312 +HIDP protocol support
313 +CONFIG_BLUEZ_HIDP
314 +  HIDP (Human Interface Device Protocol) is a transport layer
315 +  for HID reports.  HIDP is required for the Bluetooth Human
316 +  Interface Device Profile.
317 +
318 +  Say Y here to compile HIDP support into the kernel or say M to
319 +  compile it as module (hidp.o).
320 +
321  HCI UART driver
322  CONFIG_BLUEZ_HCIUART
323    Bluetooth HCI UART driver.
324    This driver is required if you want to use Bluetooth devices with
325 -  serial port interface.
326 +  serial port interface. You will also need this driver if you have 
327 +  UART based Bluetooth PCMCIA and CF devices like Xircom Credit Card 
328 +  adapter and BrainBoxes Bluetooth PC Card.
329  
330    Say Y here to compile support for Bluetooth UART devices into the
331    kernel or say M to compile it as module (hci_uart.o).
332  
333 +HCI UART (H4) protocol support 
334 +CONFIG_BLUEZ_HCIUART_H4
335 +  UART (H4) is serial protocol for communication between Bluetooth 
336 +  device and host. This protocol is required for most Bluetooth devices 
337 +  with UART interface, including PCMCIA and CF cards. 
338 +
339 +  Say Y here to compile support for HCI UART (H4) protocol.
340 +
341 +HCI BCSP protocol support 
342 +CONFIG_BLUEZ_HCIUART_BCSP
343 +  BCSP (BlueCore Serial Protocol) is serial protocol for communication 
344 +  between Bluetooth device and host. This protocol is required for non
345 +  USB Bluetooth devices based on CSR BlueCore chip, including PCMCIA and 
346 +  CF cards.
347 +
348 +  Say Y here to compile support for HCI BCSP protocol.
349 +
350 +HCI BCSP transmit CRC with every BCSP packet
351 +CONFIG_BLUEZ_HCIUART_BCSP_TXCRC
352 +  If you say Y here, a 16-bit CRC checksum will be transmitted along with
353 +  every BCSP (BlueCore Serial Protocol) packet sent to the Bluetooth chip.
354 +  This increases reliability, but slightly reduces efficiency.
355 +
356  HCI USB driver
357  CONFIG_BLUEZ_HCIUSB
358    Bluetooth HCI USB driver.
359 @@ -19911,7 +20002,16 @@
360    Say Y here to compile support for Bluetooth USB devices into the
361    kernel or say M to compile it as module (hci_usb.o).
362  
363 -HCI VHCI virtual HCI device driver
364 +HCI USB SCO (voice) support
365 +CONFIG_BLUEZ_HCIUSB_SCO
366 +  This option enables the SCO support in the HCI USB driver. You need this
367 +  to transmit voice data with your Bluetooth USB device. And your device
368 +  must also support sending SCO data over the HCI layer, because some of
369 +  them sends the SCO data to an internal PCM adapter.
370
371 +  Say Y here to compile support for HCI SCO data.
372
373 +HCI VHCI Virtual HCI device driver
374  CONFIG_BLUEZ_HCIVHCI
375    Bluetooth Virtual HCI device driver.
376    This driver is required if you want to use HCI Emulation software.
377 @@ -19919,6 +20019,63 @@
378    Say Y here to compile support for virtual HCI devices into the
379    kernel or say M to compile it as module (hci_vhci.o).
380  
381 +HCI BFUSB device driver
382 +CONFIG_BLUEZ_HCIBFUSB
383 +  Bluetooth HCI BlueFRITZ! USB driver.
384 +  This driver provides support for Bluetooth USB devices with AVM
385 +  interface:
386 +     AVM BlueFRITZ! USB
387 +
388 +  Say Y here to compile support for HCI BFUSB devices into the
389 +  kernel or say M to compile it as module (bfusb.o).
390 +
391 +HCI DTL1 (PC Card) device driver
392 +CONFIG_BLUEZ_HCIDTL1
393 +  Bluetooth HCI DTL1 (PC Card) driver.
394 +  This driver provides support for Bluetooth PCMCIA devices with
395 +  Nokia DTL1 interface:
396 +     Nokia Bluetooth Card
397 +     Socket Bluetooth CF Card
398 +
399 +  Say Y here to compile support for HCI DTL1 devices into the
400 +  kernel or say M to compile it as module (dtl1_cs.o).
401 +
402 +HCI BT3C (PC Card) device driver
403 +CONFIG_BLUEZ_HCIBT3C
404 +  Bluetooth HCI BT3C (PC Card) driver.
405 +  This driver provides support for Bluetooth PCMCIA devices with
406 +  3Com BT3C interface:
407 +     3Com Bluetooth Card (3CRWB6096)
408 +     HP Bluetooth Card
409 +
410 +  Say Y here to compile support for HCI BT3C devices into the
411 +  kernel or say M to compile it as module (bt3c_cs.o).
412 +
413 +HCI BlueCard (PC Card) device driver
414 +CONFIG_BLUEZ_HCIBLUECARD
415 +  Bluetooth HCI BlueCard (PC Card) driver.
416 +  This driver provides support for Bluetooth PCMCIA devices with
417 +  Anycom BlueCard interface:
418 +     Anycom Bluetooth PC Card
419 +     Anycom Bluetooth CF Card
420 +
421 +  Say Y here to compile support for HCI BlueCard devices into the
422 +  kernel or say M to compile it as module (bluecard_cs.o).
423 +
424 +HCI UART (PC Card) device driver
425 +CONFIG_BLUEZ_HCIBTUART
426 +  Bluetooth HCI UART (PC Card) driver.
427 +  This driver provides support for Bluetooth PCMCIA devices with
428 +  an UART interface:
429 +     Xircom CreditCard Bluetooth Adapter
430 +     Xircom RealPort2 Bluetooth Adapter
431 +     Sphinx PICO Card
432 +     H-Soft blue+Card
433 +     Cyber-blue Compact Flash Card
434 +
435 +  Say Y here to compile support for HCI UART devices into the
436 +  kernel or say M to compile it as module (btuart_cs.o).
437 +
438  # The following options are for Linux when running on the Hitachi
439  # SuperH family of RISC microprocessors.
440  
441 diff -urN linux-2.4.18/Documentation/devices.txt linux-2.4.18-mh15/Documentation/devices.txt
442 --- linux-2.4.18/Documentation/devices.txt      2001-11-07 23:46:01.000000000 +0100
443 +++ linux-2.4.18-mh15/Documentation/devices.txt 2004-08-01 16:26:23.000000000 +0200
444 @@ -419,6 +419,7 @@
445                 220 = /dev/mptctl       Message passing technology (MPT) control
446                 221 = /dev/mvista/hssdsi        Montavista PICMG hot swap system driver
447                 222 = /dev/mvista/hasi          Montavista PICMG high availability
448 +               223 = /dev/input/uinput         User level driver support for input
449                 240-255                 Reserved for local use
450  
451   11 char       Raw keyboard device
452 diff -urN linux-2.4.18/Documentation/firmware_class/firmware_sample_driver.c linux-2.4.18-mh15/Documentation/firmware_class/firmware_sample_driver.c
453 --- linux-2.4.18/Documentation/firmware_class/firmware_sample_driver.c  1970-01-01 01:00:00.000000000 +0100
454 +++ linux-2.4.18-mh15/Documentation/firmware_class/firmware_sample_driver.c     2004-08-01 16:26:23.000000000 +0200
455 @@ -0,0 +1,121 @@
456 +/*
457 + * firmware_sample_driver.c -
458 + *
459 + * Copyright (c) 2003 Manuel Estrada Sainz <ranty@debian.org>
460 + *
461 + * Sample code on how to use request_firmware() from drivers.
462 + *
463 + * Note that register_firmware() is currently useless.
464 + *
465 + */
466 +
467 +#include <linux/module.h>
468 +#include <linux/kernel.h>
469 +#include <linux/init.h>
470 +#include <linux/string.h>
471 +
472 +#include "linux/firmware.h"
473 +
474 +#define WE_CAN_NEED_FIRMWARE_BEFORE_USERSPACE_IS_AVAILABLE
475 +#ifdef WE_CAN_NEED_FIRMWARE_BEFORE_USERSPACE_IS_AVAILABLE
476 +char __init inkernel_firmware[] = "let's say that this is firmware\n";
477 +#endif
478 +
479 +static char ghost_device[] = "ghost0";
480 +
481 +static void sample_firmware_load(char *firmware, int size)
482 +{
483 +       u8 buf[size+1];
484 +       memcpy(buf, firmware, size);
485 +       buf[size] = '\0';
486 +       printk("firmware_sample_driver: firmware: %s\n", buf);
487 +}
488 +
489 +static void sample_probe_default(void)
490 +{
491 +       /* uses the default method to get the firmware */
492 +        const struct firmware *fw_entry;
493 +       printk("firmware_sample_driver: a ghost device got inserted :)\n");
494 +
495 +        if(request_firmware(&fw_entry, "sample_driver_fw", ghost_device)!=0)
496 +       {
497 +               printk(KERN_ERR
498 +                      "firmware_sample_driver: Firmware not available\n");
499 +               return;
500 +       }
501 +       
502 +       sample_firmware_load(fw_entry->data, fw_entry->size);
503 +
504 +       release_firmware(fw_entry);
505 +
506 +       /* finish setting up the device */
507 +}
508 +static void sample_probe_specific(void)
509 +{
510 +       /* Uses some specific hotplug support to get the firmware from
511 +        * userspace  directly into the hardware, or via some sysfs file */
512 +
513 +       /* NOTE: This currently doesn't work */
514 +
515 +       printk("firmware_sample_driver: a ghost device got inserted :)\n");
516 +
517 +        if(request_firmware(NULL, "sample_driver_fw", ghost_device)!=0)
518 +       {
519 +               printk(KERN_ERR
520 +                      "firmware_sample_driver: Firmware load failed\n");
521 +               return;
522 +       }
523 +       
524 +       /* request_firmware blocks until userspace finished, so at
525 +        * this point the firmware should be already in the device */
526 +
527 +       /* finish setting up the device */
528 +}
529 +static void sample_probe_async_cont(const struct firmware *fw, void *context)
530 +{
531 +       if(!fw){
532 +               printk(KERN_ERR
533 +                      "firmware_sample_driver: firmware load failed\n");
534 +               return;
535 +       }
536 +
537 +       printk("firmware_sample_driver: device pointer \"%s\"\n",
538 +              (char *)context);
539 +       sample_firmware_load(fw->data, fw->size);
540 +}
541 +static void sample_probe_async(void)
542 +{
543 +       /* Let's say that I can't sleep */
544 +       int error;
545 +       error = request_firmware_nowait (THIS_MODULE,
546 +                                        "sample_driver_fw", ghost_device,
547 +                                        "my device pointer",
548 +                                        sample_probe_async_cont);
549 +       if(error){
550 +               printk(KERN_ERR 
551 +                      "firmware_sample_driver:"
552 +                      " request_firmware_nowait failed\n");
553 +       }
554 +}
555 +
556 +static int sample_init(void)
557 +{
558 +#ifdef WE_CAN_NEED_FIRMWARE_BEFORE_USERSPACE_IS_AVAILABLE
559 +       register_firmware("sample_driver_fw", inkernel_firmware,
560 +                         sizeof(inkernel_firmware));
561 +#endif
562 +       /* since there is no real hardware insertion I just call the
563 +        * sample probe functions here */
564 +       sample_probe_specific();
565 +       sample_probe_default();
566 +       sample_probe_async();
567 +       return 0;
568 +}
569 +static void __exit sample_exit(void)
570 +{
571 +}
572 +
573 +module_init (sample_init);
574 +module_exit (sample_exit);
575 +
576 +MODULE_LICENSE("GPL");
577 diff -urN linux-2.4.18/Documentation/firmware_class/hotplug-script linux-2.4.18-mh15/Documentation/firmware_class/hotplug-script
578 --- linux-2.4.18/Documentation/firmware_class/hotplug-script    1970-01-01 01:00:00.000000000 +0100
579 +++ linux-2.4.18-mh15/Documentation/firmware_class/hotplug-script       2004-08-01 16:26:23.000000000 +0200
580 @@ -0,0 +1,16 @@
581 +#!/bin/sh
582 +
583 +# Simple hotplug script sample:
584 +# 
585 +# Both $DEVPATH and $FIRMWARE are already provided in the environment.
586 +
587 +HOTPLUG_FW_DIR=/usr/lib/hotplug/firmware/
588 +
589 +echo 1 > /sysfs/$DEVPATH/loading
590 +cat $HOTPLUG_FW_DIR/$FIRMWARE > /sysfs/$DEVPATH/data
591 +echo 0 > /sysfs/$DEVPATH/loading
592 +
593 +# To cancel the load in case of error:
594 +#
595 +#      echo -1 > /sysfs/$DEVPATH/loading
596 +#
597 diff -urN linux-2.4.18/Documentation/firmware_class/README linux-2.4.18-mh15/Documentation/firmware_class/README
598 --- linux-2.4.18/Documentation/firmware_class/README    1970-01-01 01:00:00.000000000 +0100
599 +++ linux-2.4.18-mh15/Documentation/firmware_class/README       2004-08-01 16:26:23.000000000 +0200
600 @@ -0,0 +1,58 @@
601 +
602 + request_firmware() hotplug interface:
603 + ------------------------------------
604 +       Copyright (C) 2003 Manuel Estrada Sainz <ranty@debian.org>
605 +
606 + Why:
607 + ---
608 +
609 + Today, the most extended way to use firmware in the Linux kernel is linking
610 + it statically in a header file. Which has political and technical issues:
611 +
612 +  1) Some firmware is not legal to redistribute.
613 +  2) The firmware occupies memory permanently, even though it often is just
614 +     used once.
615 +  3) Some people, like the Debian crowd, don't consider some firmware free
616 +     enough and remove entire drivers (e.g.: keyspan).
617 +
618 + about in-kernel persistence:
619 + ---------------------------
620 + Under some circumstances, as explained below, it would be interesting to keep
621 + firmware images in non-swappable kernel memory or even in the kernel image
622 + (probably within initramfs).
623 +
624 + Note that this functionality has not been implemented.
625 +
626 + - Why OPTIONAL in-kernel persistence may be a good idea sometimes:
627
628 +       - If the device that needs the firmware is needed to access the
629 +         filesystem. When upon some error the device has to be reset and the
630 +         firmware reloaded, it won't be possible to get it from userspace.
631 +         e.g.:
632 +               - A diskless client with a network card that needs firmware.
633 +               - The filesystem is stored in a disk behind an scsi device
634 +                 that needs firmware.
635 +       - Replacing buggy DSDT/SSDT ACPI tables on boot.
636 +         Note: this would require the persistent objects to be included
637 +         within the kernel image, probably within initramfs.
638 +         
639 +   And the same device can be needed to access the filesystem or not depending
640 +   on the setup, so I think that the choice on what firmware to make
641 +   persistent should be left to userspace.
642 +
643 + - Why register_firmware()+__init can be useful:
644 +       - For boot devices needing firmware.
645 +       - To make the transition easier:
646 +               The firmware can be declared __init and register_firmware()
647 +               called on module_init. Then the firmware is warranted to be
648 +               there even if "firmware hotplug userspace" is not there yet or
649 +               it doesn't yet provide the needed firmware.
650 +               Once the firmware is widely available in userspace, it can be
651 +               removed from the kernel. Or made optional (CONFIG_.*_FIRMWARE).
652 +
653 +       In either case, if firmware hotplug support is there, it can move the
654 +       firmware out of kernel memory into the real filesystem for later
655 +       usage.
656 +
657 +       Note: If persistence is implemented on top of initramfs,
658 +       register_firmware() may not be appropriate.
659 diff -urN linux-2.4.18/drivers/bluetooth/bfusb.c linux-2.4.18-mh15/drivers/bluetooth/bfusb.c
660 --- linux-2.4.18/drivers/bluetooth/bfusb.c      1970-01-01 01:00:00.000000000 +0100
661 +++ linux-2.4.18-mh15/drivers/bluetooth/bfusb.c 2004-08-01 16:26:23.000000000 +0200
662 @@ -0,0 +1,782 @@
663 +/*
664 + *
665 + *  AVM BlueFRITZ! USB driver
666 + *
667 + *  Copyright (C) 2003  Marcel Holtmann <marcel@holtmann.org>
668 + *
669 + *
670 + *  This program is free software; you can redistribute it and/or modify
671 + *  it under the terms of the GNU General Public License as published by
672 + *  the Free Software Foundation; either version 2 of the License, or
673 + *  (at your option) any later version.
674 + *
675 + *  This program is distributed in the hope that it will be useful,
676 + *  but WITHOUT ANY WARRANTY; without even the implied warranty of
677 + *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
678 + *  GNU General Public License for more details.
679 + *
680 + *  You should have received a copy of the GNU General Public License
681 + *  along with this program; if not, write to the Free Software
682 + *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
683 + *
684 + */
685 +
686 +#include <linux/config.h>
687 +#include <linux/module.h>
688 +
689 +#include <linux/kernel.h>
690 +#include <linux/init.h>
691 +#include <linux/slab.h>
692 +#include <linux/types.h>
693 +#include <linux/sched.h>
694 +#include <linux/errno.h>
695 +#include <linux/skbuff.h>
696 +
697 +#include <linux/firmware.h>
698 +#include <linux/usb.h>
699 +
700 +#include <net/bluetooth/bluetooth.h>
701 +#include <net/bluetooth/hci_core.h>
702 +
703 +#ifndef CONFIG_BLUEZ_HCIBFUSB_DEBUG
704 +#undef  BT_DBG
705 +#define BT_DBG(D...)
706 +#endif
707 +
708 +#define VERSION "1.1"
709 +
710 +static struct usb_device_id bfusb_table[] = {
711 +       /* AVM BlueFRITZ! USB */
712 +       { USB_DEVICE(0x057c, 0x2200) },
713 +
714 +       { }     /* Terminating entry */
715 +};
716 +
717 +MODULE_DEVICE_TABLE(usb, bfusb_table);
718 +
719 +
720 +#define BFUSB_MAX_BLOCK_SIZE   256
721 +
722 +#define BFUSB_BLOCK_TIMEOUT    (HZ * 3)
723 +
724 +#define BFUSB_TX_PROCESS       1
725 +#define BFUSB_TX_WAKEUP                2
726 +
727 +#define BFUSB_MAX_BULK_TX      1
728 +#define BFUSB_MAX_BULK_RX      1
729 +
730 +struct bfusb {
731 +       struct hci_dev          hdev;
732 +
733 +       unsigned long           state;
734 +
735 +       struct usb_device       *udev;
736 +
737 +       unsigned int            bulk_in_ep;
738 +       unsigned int            bulk_out_ep;
739 +       unsigned int            bulk_pkt_size;
740 +
741 +       rwlock_t                lock;
742 +
743 +       struct sk_buff_head     transmit_q;
744 +
745 +       struct sk_buff          *reassembly;
746 +
747 +       atomic_t                pending_tx;
748 +       struct sk_buff_head     pending_q;
749 +       struct sk_buff_head     completed_q;
750 +};
751 +
752 +struct bfusb_scb {
753 +       struct urb *urb;
754 +};
755 +
756 +static void bfusb_tx_complete(struct urb *urb);
757 +static void bfusb_rx_complete(struct urb *urb);
758 +
759 +static struct urb *bfusb_get_completed(struct bfusb *bfusb)
760 +{
761 +       struct sk_buff *skb;
762 +       struct urb *urb = NULL;
763 +
764 +       BT_DBG("bfusb %p", bfusb);
765 +
766 +       skb = skb_dequeue(&bfusb->completed_q);
767 +       if (skb) {
768 +               urb = ((struct bfusb_scb *) skb->cb)->urb;
769 +               kfree_skb(skb);
770 +       }
771 +
772 +       return urb;
773 +}
774 +
775 +static inline void bfusb_unlink_urbs(struct bfusb *bfusb)
776 +{
777 +       struct sk_buff *skb;
778 +       struct urb *urb;
779 +
780 +       BT_DBG("bfusb %p", bfusb);
781 +
782 +       while ((skb = skb_dequeue(&bfusb->pending_q))) {
783 +               urb = ((struct bfusb_scb *) skb->cb)->urb;
784 +               usb_unlink_urb(urb);
785 +               skb_queue_tail(&bfusb->completed_q, skb);
786 +       }
787 +
788 +       while ((urb = bfusb_get_completed(bfusb)))
789 +               usb_free_urb(urb);
790 +}
791 +
792 +
793 +static int bfusb_send_bulk(struct bfusb *bfusb, struct sk_buff *skb)
794 +{
795 +       struct bfusb_scb *scb = (void *) skb->cb;
796 +       struct urb *urb = bfusb_get_completed(bfusb);
797 +       int err, pipe;
798 +
799 +       BT_DBG("bfusb %p skb %p len %d", bfusb, skb, skb->len);
800 +
801 +       if (!urb && !(urb = usb_alloc_urb(0)))
802 +               return -ENOMEM;
803 +
804 +       pipe = usb_sndbulkpipe(bfusb->udev, bfusb->bulk_out_ep);
805 +
806 +       FILL_BULK_URB(urb, bfusb->udev, pipe, skb->data, skb->len,
807 +                       bfusb_tx_complete, skb);
808 +
809 +       urb->transfer_flags = USB_QUEUE_BULK;
810 +
811 +       scb->urb = urb;
812 +
813 +       skb_queue_tail(&bfusb->pending_q, skb);
814 +
815 +       err = usb_submit_urb(urb);
816 +       if (err) {
817 +               BT_ERR("%s bulk tx submit failed urb %p err %d", 
818 +                                       bfusb->hdev.name, urb, err);
819 +               skb_unlink(skb);
820 +               usb_free_urb(urb);
821 +       } else
822 +               atomic_inc(&bfusb->pending_tx);
823 +
824 +       return err;
825 +}
826 +
827 +static void bfusb_tx_wakeup(struct bfusb *bfusb)
828 +{
829 +       struct sk_buff *skb;
830 +
831 +       BT_DBG("bfusb %p", bfusb);
832 +
833 +       if (test_and_set_bit(BFUSB_TX_PROCESS, &bfusb->state)) {
834 +               set_bit(BFUSB_TX_WAKEUP, &bfusb->state);
835 +               return;
836 +       }
837 +
838 +       do {
839 +               clear_bit(BFUSB_TX_WAKEUP, &bfusb->state);
840 +
841 +               while ((atomic_read(&bfusb->pending_tx) < BFUSB_MAX_BULK_TX) &&
842 +                               (skb = skb_dequeue(&bfusb->transmit_q))) {
843 +                       if (bfusb_send_bulk(bfusb, skb) < 0) {
844 +                               skb_queue_head(&bfusb->transmit_q, skb);
845 +                               break;
846 +                       }
847 +               }
848 +
849 +       } while (test_bit(BFUSB_TX_WAKEUP, &bfusb->state));
850 +
851 +       clear_bit(BFUSB_TX_PROCESS, &bfusb->state);
852 +}
853 +
854 +static void bfusb_tx_complete(struct urb *urb)
855 +{
856 +       struct sk_buff *skb = (struct sk_buff *) urb->context;
857 +       struct bfusb *bfusb = (struct bfusb *) skb->dev;
858 +
859 +       BT_DBG("bfusb %p urb %p skb %p len %d", bfusb, urb, skb, skb->len);
860 +
861 +       atomic_dec(&bfusb->pending_tx);
862 +
863 +       if (!test_bit(HCI_RUNNING, &bfusb->hdev.flags))
864 +               return;
865 +
866 +       if (!urb->status)
867 +               bfusb->hdev.stat.byte_tx += skb->len;
868 +       else
869 +               bfusb->hdev.stat.err_tx++;
870 +
871 +       read_lock(&bfusb->lock);
872 +
873 +       skb_unlink(skb);
874 +       skb_queue_tail(&bfusb->completed_q, skb);
875 +
876 +       bfusb_tx_wakeup(bfusb);
877 +
878 +       read_unlock(&bfusb->lock);
879 +}
880 +
881 +
882 +static int bfusb_rx_submit(struct bfusb *bfusb, struct urb *urb)
883 +{
884 +       struct bfusb_scb *scb;
885 +       struct sk_buff *skb;
886 +       int err, pipe, size = HCI_MAX_FRAME_SIZE + 32;
887 +
888 +       BT_DBG("bfusb %p urb %p", bfusb, urb);
889 +
890 +       if (!urb && !(urb = usb_alloc_urb(0)))
891 +               return -ENOMEM;
892 +
893 +       if (!(skb = bluez_skb_alloc(size, GFP_ATOMIC))) {
894 +               usb_free_urb(urb);
895 +               return -ENOMEM;
896 +       }
897 +
898 +       skb->dev = (void *) bfusb;
899 +
900 +       scb = (struct bfusb_scb *) skb->cb;
901 +       scb->urb = urb;
902 +
903 +       pipe = usb_rcvbulkpipe(bfusb->udev, bfusb->bulk_in_ep);
904 +
905 +       FILL_BULK_URB(urb, bfusb->udev, pipe, skb->data, size,
906 +                       bfusb_rx_complete, skb);
907 +
908 +       urb->transfer_flags = USB_QUEUE_BULK;
909 +
910 +       skb_queue_tail(&bfusb->pending_q, skb);
911 +
912 +       err = usb_submit_urb(urb);
913 +       if (err) {
914 +               BT_ERR("%s bulk rx submit failed urb %p err %d",
915 +                                       bfusb->hdev.name, urb, err);
916 +               skb_unlink(skb);
917 +               kfree_skb(skb);
918 +               usb_free_urb(urb);
919 +       }
920 +
921 +       return err;
922 +}
923 +
924 +static inline int bfusb_recv_block(struct bfusb *bfusb, int hdr, unsigned char *data, int len)
925 +{
926 +       BT_DBG("bfusb %p hdr 0x%02x data %p len %d", bfusb, hdr, data, len);
927 +
928 +       if (hdr & 0x10) {
929 +               BT_ERR("%s error in block", bfusb->hdev.name);
930 +               if (bfusb->reassembly)
931 +                       kfree_skb(bfusb->reassembly);
932 +               bfusb->reassembly = NULL;
933 +               return -EIO;
934 +       }
935 +
936 +       if (hdr & 0x04) {
937 +               struct sk_buff *skb;
938 +               unsigned char pkt_type;
939 +               int pkt_len = 0;
940 +
941 +               if (bfusb->reassembly) {
942 +                       BT_ERR("%s unexpected start block", bfusb->hdev.name);
943 +                       kfree_skb(bfusb->reassembly);
944 +                       bfusb->reassembly = NULL;
945 +               }
946 +
947 +               if (len < 1) {
948 +                       BT_ERR("%s no packet type found", bfusb->hdev.name);
949 +                       return -EPROTO;
950 +               }
951 +
952 +               pkt_type = *data++; len--;
953 +
954 +               switch (pkt_type) {
955 +               case HCI_EVENT_PKT:
956 +                       if (len >= HCI_EVENT_HDR_SIZE) {
957 +                               hci_event_hdr *hdr = (hci_event_hdr *) data;
958 +                               pkt_len = HCI_EVENT_HDR_SIZE + hdr->plen;
959 +                       } else {
960 +                               BT_ERR("%s event block is too short", bfusb->hdev.name);
961 +                               return -EILSEQ;
962 +                       }
963 +                       break;
964 +
965 +               case HCI_ACLDATA_PKT:
966 +                       if (len >= HCI_ACL_HDR_SIZE) {
967 +                               hci_acl_hdr *hdr = (hci_acl_hdr *) data;
968 +                               pkt_len = HCI_ACL_HDR_SIZE + __le16_to_cpu(hdr->dlen);
969 +                       } else {
970 +                               BT_ERR("%s data block is too short", bfusb->hdev.name);
971 +                               return -EILSEQ;
972 +                       }
973 +                       break;
974 +
975 +               case HCI_SCODATA_PKT:
976 +                       if (len >= HCI_SCO_HDR_SIZE) {
977 +                               hci_sco_hdr *hdr = (hci_sco_hdr *) data;
978 +                               pkt_len = HCI_SCO_HDR_SIZE + hdr->dlen;
979 +                       } else {
980 +                               BT_ERR("%s audio block is too short", bfusb->hdev.name);
981 +                               return -EILSEQ;
982 +                       }
983 +                       break;
984 +               }
985 +
986 +               skb = bluez_skb_alloc(pkt_len, GFP_ATOMIC);
987 +               if (!skb) {
988 +                       BT_ERR("%s no memory for the packet", bfusb->hdev.name);
989 +                       return -ENOMEM;
990 +               }
991 +
992 +               skb->dev = (void *) &bfusb->hdev;
993 +               skb->pkt_type = pkt_type;
994 +
995 +               bfusb->reassembly = skb;
996 +       } else {
997 +               if (!bfusb->reassembly) {
998 +                       BT_ERR("%s unexpected continuation block", bfusb->hdev.name);
999 +                       return -EIO;
1000 +               }
1001 +       }
1002 +
1003 +       if (len > 0)
1004 +               memcpy(skb_put(bfusb->reassembly, len), data, len);
1005 +
1006 +       if (hdr & 0x08) {
1007 +               hci_recv_frame(bfusb->reassembly);
1008 +               bfusb->reassembly = NULL;
1009 +       }
1010 +
1011 +       return 0;
1012 +}
1013 +
1014 +static void bfusb_rx_complete(struct urb *urb)
1015 +{
1016 +       struct sk_buff *skb = (struct sk_buff *) urb->context;
1017 +       struct bfusb *bfusb = (struct bfusb *) skb->dev;
1018 +       unsigned char *buf = urb->transfer_buffer;
1019 +       int count = urb->actual_length;
1020 +       int err, hdr, len;
1021 +
1022 +       BT_DBG("bfusb %p urb %p skb %p len %d", bfusb, urb, skb, skb->len);
1023 +
1024 +       read_lock(&bfusb->lock);
1025 +
1026 +       if (!test_bit(HCI_RUNNING, &bfusb->hdev.flags))
1027 +               goto unlock;
1028 +
1029 +       if (urb->status || !count)
1030 +               goto resubmit;
1031 +
1032 +       bfusb->hdev.stat.byte_rx += count;
1033 +
1034 +       skb_put(skb, count);
1035 +
1036 +       while (count) {
1037 +               hdr = buf[0] | (buf[1] << 8);
1038 +
1039 +               if (hdr & 0x4000) {
1040 +                       len = 0;
1041 +                       count -= 2;
1042 +                       buf   += 2;
1043 +               } else {
1044 +                       len = (buf[2] == 0) ? 256 : buf[2];
1045 +                       count -= 3;
1046 +                       buf   += 3;
1047 +               }
1048 +
1049 +               if (count < len) {
1050 +                       BT_ERR("%s block extends over URB buffer ranges",
1051 +                                       bfusb->hdev.name);
1052 +               }
1053 +
1054 +               if ((hdr & 0xe1) == 0xc1)
1055 +                       bfusb_recv_block(bfusb, hdr, buf, len);
1056 +
1057 +               count -= len;
1058 +               buf   += len;
1059 +       }
1060 +
1061 +       skb_unlink(skb);
1062 +       kfree_skb(skb);
1063 +
1064 +       bfusb_rx_submit(bfusb, urb);
1065 +
1066 +       read_unlock(&bfusb->lock);
1067 +
1068 +       return;
1069 +
1070 +resubmit:
1071 +       urb->dev = bfusb->udev;
1072 +
1073 +       err = usb_submit_urb(urb);
1074 +       if (err) {
1075 +               BT_ERR("%s bulk resubmit failed urb %p err %d",
1076 +                                       bfusb->hdev.name, urb, err);
1077 +       }
1078 +
1079 +unlock:
1080 +       read_unlock(&bfusb->lock);
1081 +}
1082 +
1083 +
1084 +static int bfusb_open(struct hci_dev *hdev)
1085 +{
1086 +       struct bfusb *bfusb = (struct bfusb *) hdev->driver_data;
1087 +       unsigned long flags;
1088 +       int i, err;
1089 +
1090 +       BT_DBG("hdev %p bfusb %p", hdev, bfusb);
1091 +
1092 +       if (test_and_set_bit(HCI_RUNNING, &hdev->flags))
1093 +               return 0;
1094 +
1095 +       MOD_INC_USE_COUNT;
1096 +
1097 +       write_lock_irqsave(&bfusb->lock, flags);
1098 +
1099 +       err = bfusb_rx_submit(bfusb, NULL);
1100 +       if (!err) {
1101 +               for (i = 1; i < BFUSB_MAX_BULK_RX; i++)
1102 +                       bfusb_rx_submit(bfusb, NULL);
1103 +       } else {
1104 +               clear_bit(HCI_RUNNING, &hdev->flags);
1105 +               MOD_DEC_USE_COUNT;
1106 +       }
1107 +
1108 +       write_unlock_irqrestore(&bfusb->lock, flags);
1109 +
1110 +       return err;
1111 +}
1112 +
1113 +static int bfusb_flush(struct hci_dev *hdev)
1114 +{
1115 +       struct bfusb *bfusb = (struct bfusb *) hdev->driver_data;
1116 +
1117 +       BT_DBG("hdev %p bfusb %p", hdev, bfusb);
1118 +
1119 +       skb_queue_purge(&bfusb->transmit_q);
1120 +
1121 +       return 0;
1122 +}
1123 +
1124 +static int bfusb_close(struct hci_dev *hdev)
1125 +{
1126 +       struct bfusb *bfusb = (struct bfusb *) hdev->driver_data;
1127 +       unsigned long flags;
1128 +
1129 +       BT_DBG("hdev %p bfusb %p", hdev, bfusb);
1130 +
1131 +       if (!test_and_clear_bit(HCI_RUNNING, &hdev->flags))
1132 +               return 0;
1133 +
1134 +       write_lock_irqsave(&bfusb->lock, flags);
1135 +
1136 +       bfusb_unlink_urbs(bfusb);
1137 +       bfusb_flush(hdev);
1138 +
1139 +       write_unlock_irqrestore(&bfusb->lock, flags);
1140 +
1141 +       MOD_DEC_USE_COUNT;
1142 +
1143 +       return 0;
1144 +}
1145 +
1146 +static int bfusb_send_frame(struct sk_buff *skb)
1147 +{
1148 +       struct hci_dev *hdev = (struct hci_dev *) skb->dev;
1149 +       struct bfusb *bfusb;
1150 +       struct sk_buff *nskb;
1151 +       unsigned char buf[3];
1152 +       int sent = 0, size, count;
1153 +
1154 +       BT_DBG("hdev %p skb %p type %d len %d", hdev, skb, skb->pkt_type, skb->len);
1155 +
1156 +       if (!hdev) {
1157 +               BT_ERR("Frame for unknown HCI device (hdev=NULL)");
1158 +               return -ENODEV;
1159 +       }
1160 +
1161 +       if (!test_bit(HCI_RUNNING, &hdev->flags))
1162 +               return -EBUSY;
1163 +
1164 +       bfusb = (struct bfusb *) hdev->driver_data;
1165 +
1166 +       switch (skb->pkt_type) {
1167 +       case HCI_COMMAND_PKT:
1168 +               hdev->stat.cmd_tx++;
1169 +               break;
1170 +       case HCI_ACLDATA_PKT:
1171 +               hdev->stat.acl_tx++;
1172 +               break;
1173 +       case HCI_SCODATA_PKT:
1174 +               hdev->stat.sco_tx++;
1175 +               break;
1176 +       };
1177 +
1178 +       /* Prepend skb with frame type */
1179 +       memcpy(skb_push(skb, 1), &(skb->pkt_type), 1);
1180 +
1181 +       count = skb->len;
1182 +
1183 +       /* Max HCI frame size seems to be 1511 + 1 */
1184 +       if (!(nskb = bluez_skb_alloc(count + 32, GFP_ATOMIC))) {
1185 +               BT_ERR("Can't allocate memory for new packet");
1186 +               return -ENOMEM;
1187 +       }
1188 +
1189 +       nskb->dev = (void *) bfusb;
1190 +
1191 +       while (count) {
1192 +               size = min_t(uint, count, BFUSB_MAX_BLOCK_SIZE);
1193 +
1194 +               buf[0] = 0xc1 | ((sent == 0) ? 0x04 : 0) | ((count == size) ? 0x08 : 0);
1195 +               buf[1] = 0x00;
1196 +               buf[2] = (size == BFUSB_MAX_BLOCK_SIZE) ? 0 : size;
1197 +
1198 +               memcpy(skb_put(nskb, 3), buf, 3);
1199 +               memcpy(skb_put(nskb, size), skb->data + sent, size);
1200 +
1201 +               sent  += size;
1202 +               count -= size;
1203 +       }
1204 +
1205 +       /* Don't send frame with multiple size of bulk max packet */
1206 +       if ((nskb->len % bfusb->bulk_pkt_size) == 0) {
1207 +               buf[0] = 0xdd;
1208 +               buf[1] = 0x00;
1209 +               memcpy(skb_put(nskb, 2), buf, 2);
1210 +       }
1211 +
1212 +       read_lock(&bfusb->lock);
1213 +
1214 +       skb_queue_tail(&bfusb->transmit_q, nskb);
1215 +       bfusb_tx_wakeup(bfusb);
1216 +
1217 +       read_unlock(&bfusb->lock);
1218 +
1219 +       kfree_skb(skb);
1220 +
1221 +       return 0;
1222 +}
1223 +
1224 +static void bfusb_destruct(struct hci_dev *hdev)
1225 +{
1226 +       struct bfusb *bfusb = (struct bfusb *) hdev->driver_data;
1227 +
1228 +       BT_DBG("hdev %p bfusb %p", hdev, bfusb);
1229 +
1230 +       kfree(bfusb);
1231 +}
1232 +
1233 +static int bfusb_ioctl(struct hci_dev *hdev, unsigned int cmd, unsigned long arg)
1234 +{
1235 +       return -ENOIOCTLCMD;
1236 +}
1237 +
1238 +
1239 +static int bfusb_load_firmware(struct bfusb *bfusb, unsigned char *firmware, int count)
1240 +{
1241 +       unsigned char *buf;
1242 +       int err, pipe, len, size, sent = 0;
1243 +
1244 +       BT_DBG("bfusb %p udev %p firmware %p count %d", bfusb, bfusb->udev, firmware, count);
1245 +
1246 +       BT_INFO("BlueFRITZ! USB loading firmware");
1247 +
1248 +       if (usb_set_configuration(bfusb->udev, 1) < 0) {
1249 +               BT_ERR("Can't change to loading configuration");
1250 +               return -EBUSY;
1251 +       }
1252 +
1253 +       buf = kmalloc(BFUSB_MAX_BLOCK_SIZE + 3, GFP_ATOMIC);
1254 +       if (!buf) {
1255 +               BT_ERR("Can't allocate memory chunk for firmware");
1256 +               return -ENOMEM;
1257 +       }
1258 +
1259 +       pipe = usb_sndbulkpipe(bfusb->udev, bfusb->bulk_out_ep);
1260 +
1261 +       while (count) {
1262 +               size = min_t(uint, count, BFUSB_MAX_BLOCK_SIZE + 3);
1263 +
1264 +               memcpy(buf, firmware + sent, size);
1265 +
1266 +               err = usb_bulk_msg(bfusb->udev, pipe, buf, size,
1267 +                                       &len, BFUSB_BLOCK_TIMEOUT);
1268 +
1269 +               if (err || (len != size)) {
1270 +                       BT_ERR("Error in firmware loading");
1271 +                       goto error;
1272 +               }
1273 +
1274 +               sent  += size;
1275 +               count -= size;
1276 +       }
1277 +
1278 +       if ((err = usb_bulk_msg(bfusb->udev, pipe, NULL, 0,
1279 +                               &len, BFUSB_BLOCK_TIMEOUT)) < 0) {
1280 +               BT_ERR("Error in null packet request");
1281 +               goto error;
1282 +       }
1283 +
1284 +       if ((err = usb_set_configuration(bfusb->udev, 2)) < 0) {
1285 +               BT_ERR("Can't change to running configuration");
1286 +               goto error;
1287 +       }
1288 +
1289 +       BT_INFO("BlueFRITZ! USB device ready");
1290 +
1291 +       kfree(buf);
1292 +       return 0;
1293 +
1294 +error:
1295 +       kfree(buf);
1296 +
1297 +       pipe = usb_sndctrlpipe(bfusb->udev, 0);
1298 +
1299 +       usb_control_msg(bfusb->udev, pipe, USB_REQ_SET_CONFIGURATION,
1300 +                               0, 0, 0, NULL, 0, BFUSB_BLOCK_TIMEOUT);
1301 +
1302 +       return err;
1303 +}
1304 +
1305 +static void *bfusb_probe(struct usb_device *udev, unsigned int ifnum, const struct usb_device_id *id)
1306 +{
1307 +       const struct firmware *firmware;
1308 +       char device[16];
1309 +       struct usb_interface *iface;
1310 +       struct usb_interface_descriptor *iface_desc;
1311 +       struct usb_endpoint_descriptor *bulk_out_ep;
1312 +       struct usb_endpoint_descriptor *bulk_in_ep;
1313 +       struct hci_dev *hdev;
1314 +       struct bfusb *bfusb;
1315 +
1316 +       BT_DBG("udev %p ifnum %d id %p", udev, ifnum, id);
1317 +
1318 +       /* Check number of endpoints */
1319 +       iface = &udev->actconfig->interface[0];
1320 +       iface_desc = &iface->altsetting[0];
1321 +
1322 +       if (iface_desc->bNumEndpoints < 2)
1323 +               return NULL;
1324 +
1325 +       bulk_out_ep = &iface_desc->endpoint[0];
1326 +       bulk_in_ep  = &iface_desc->endpoint[1];
1327 +
1328 +       if (!bulk_out_ep || !bulk_in_ep) {
1329 +               BT_ERR("Bulk endpoints not found");
1330 +               goto done;
1331 +       }
1332 +
1333 +       /* Initialize control structure and load firmware */
1334 +       if (!(bfusb = kmalloc(sizeof(struct bfusb), GFP_KERNEL))) {
1335 +               BT_ERR("Can't allocate memory for control structure");
1336 +               goto done;
1337 +       }
1338 +
1339 +       memset(bfusb, 0, sizeof(struct bfusb));
1340 +
1341 +       bfusb->udev = udev;
1342 +       bfusb->bulk_in_ep    = bulk_in_ep->bEndpointAddress;
1343 +       bfusb->bulk_out_ep   = bulk_out_ep->bEndpointAddress;
1344 +       bfusb->bulk_pkt_size = bulk_out_ep->wMaxPacketSize;
1345 +
1346 +       bfusb->lock = RW_LOCK_UNLOCKED;
1347 +
1348 +       bfusb->reassembly = NULL;
1349 +
1350 +       skb_queue_head_init(&bfusb->transmit_q);
1351 +       skb_queue_head_init(&bfusb->pending_q);
1352 +       skb_queue_head_init(&bfusb->completed_q);
1353 +
1354 +       snprintf(device, sizeof(device), "bfusb%3.3d%3.3d", udev->bus->busnum, udev->devnum);
1355 +
1356 +       if (request_firmware(&firmware, "bfubase.frm", device) < 0) {
1357 +               BT_ERR("Firmware request failed");
1358 +               goto error;
1359 +       }
1360 +
1361 +       if (bfusb_load_firmware(bfusb, firmware->data, firmware->size) < 0) {
1362 +               BT_ERR("Firmware loading failed");
1363 +               goto release;
1364 +       }
1365 +
1366 +       release_firmware(firmware);
1367 +
1368 +       /* Initialize and register HCI device */
1369 +       hdev = &bfusb->hdev;
1370 +
1371 +       hdev->type = HCI_USB;
1372 +       hdev->driver_data = bfusb;
1373 +
1374 +       hdev->open     = bfusb_open;
1375 +       hdev->close    = bfusb_close;
1376 +       hdev->flush    = bfusb_flush;
1377 +       hdev->send     = bfusb_send_frame;
1378 +       hdev->destruct = bfusb_destruct;
1379 +       hdev->ioctl    = bfusb_ioctl;
1380 +
1381 +       if (hci_register_dev(hdev) < 0) {
1382 +               BT_ERR("Can't register HCI device");
1383 +               goto error;
1384 +       }
1385 +
1386 +       return bfusb;
1387 +
1388 +release:
1389 +       release_firmware(firmware);
1390 +
1391 +error:
1392 +       kfree(bfusb);
1393 +
1394 +done:
1395 +       return NULL;
1396 +}
1397 +
1398 +static void bfusb_disconnect(struct usb_device *udev, void *ptr)
1399 +{
1400 +       struct bfusb *bfusb = (struct bfusb *) ptr;
1401 +       struct hci_dev *hdev = &bfusb->hdev;
1402 +
1403 +       BT_DBG("udev %p ptr %p", udev, ptr);
1404 +
1405 +       if (!hdev)
1406 +               return;
1407 +
1408 +       bfusb_close(hdev);
1409 +
1410 +       if (hci_unregister_dev(hdev) < 0)
1411 +               BT_ERR("Can't unregister HCI device %s", hdev->name);
1412 +}
1413 +
1414 +static struct usb_driver bfusb_driver = {
1415 +       name:           "bfusb",
1416 +       probe:          bfusb_probe,
1417 +       disconnect:     bfusb_disconnect,
1418 +       id_table:       bfusb_table,
1419 +};
1420 +
1421 +static int __init bfusb_init(void)
1422 +{
1423 +       int err;
1424 +
1425 +       BT_INFO("BlueFRITZ! USB driver ver %s", VERSION);
1426 +       BT_INFO("Copyright (C) 2003 Marcel Holtmann <marcel@holtmann.org>");
1427 +
1428 +       if ((err = usb_register(&bfusb_driver)) < 0)
1429 +               BT_ERR("Failed to register BlueFRITZ! USB driver");
1430 +
1431 +       return err;
1432 +}
1433 +
1434 +static void __exit bfusb_cleanup(void)
1435 +{
1436 +       usb_deregister(&bfusb_driver);
1437 +}
1438 +
1439 +module_init(bfusb_init);
1440 +module_exit(bfusb_cleanup);
1441 +
1442 +MODULE_AUTHOR("Marcel Holtmann <marcel@holtmann.org>");
1443 +MODULE_DESCRIPTION("BlueFRITZ! USB driver ver " VERSION);
1444 +MODULE_LICENSE("GPL");
1445 diff -urN linux-2.4.18/drivers/bluetooth/bluecard_cs.c linux-2.4.18-mh15/drivers/bluetooth/bluecard_cs.c
1446 --- linux-2.4.18/drivers/bluetooth/bluecard_cs.c        1970-01-01 01:00:00.000000000 +0100
1447 +++ linux-2.4.18-mh15/drivers/bluetooth/bluecard_cs.c   2004-08-01 16:26:23.000000000 +0200
1448 @@ -0,0 +1,1116 @@
1449 +/*
1450 + *
1451 + *  Bluetooth driver for the Anycom BlueCard (LSE039/LSE041)
1452 + *
1453 + *  Copyright (C) 2001-2002  Marcel Holtmann <marcel@holtmann.org>
1454 + *
1455 + *
1456 + *  This program is free software; you can redistribute it and/or modify
1457 + *  it under the terms of the GNU General Public License version 2 as
1458 + *  published by the Free Software Foundation;
1459 + *
1460 + *  Software distributed under the License is distributed on an "AS
1461 + *  IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
1462 + *  implied. See the License for the specific language governing
1463 + *  rights and limitations under the License.
1464 + *
1465 + *  The initial developer of the original code is David A. Hinds
1466 + *  <dahinds@users.sourceforge.net>.  Portions created by David A. Hinds
1467 + *  are Copyright (C) 1999 David A. Hinds.  All Rights Reserved.
1468 + *
1469 + */
1470 +
1471 +#include <linux/config.h>
1472 +#include <linux/module.h>
1473 +
1474 +#include <linux/kernel.h>
1475 +#include <linux/init.h>
1476 +#include <linux/slab.h>
1477 +#include <linux/types.h>
1478 +#include <linux/sched.h>
1479 +#include <linux/timer.h>
1480 +#include <linux/errno.h>
1481 +#include <linux/ptrace.h>
1482 +#include <linux/ioport.h>
1483 +#include <linux/spinlock.h>
1484 +#include <linux/skbuff.h>
1485 +#include <asm/io.h>
1486 +
1487 +#include <pcmcia/version.h>
1488 +#include <pcmcia/cs_types.h>
1489 +#include <pcmcia/cs.h>
1490 +#include <pcmcia/cistpl.h>
1491 +#include <pcmcia/ciscode.h>
1492 +#include <pcmcia/ds.h>
1493 +#include <pcmcia/cisreg.h>
1494 +
1495 +#include <net/bluetooth/bluetooth.h>
1496 +#include <net/bluetooth/hci_core.h>
1497 +
1498 +
1499 +
1500 +/* ======================== Module parameters ======================== */
1501 +
1502 +
1503 +/* Bit map of interrupts to choose from */
1504 +static u_int irq_mask = 0x86bc;
1505 +static int irq_list[4] = { -1 };
1506 +
1507 +MODULE_PARM(irq_mask, "i");
1508 +MODULE_PARM(irq_list, "1-4i");
1509 +
1510 +MODULE_AUTHOR("Marcel Holtmann <marcel@holtmann.org>");
1511 +MODULE_DESCRIPTION("BlueZ driver for the Anycom BlueCard (LSE039/LSE041)");
1512 +MODULE_LICENSE("GPL");
1513 +
1514 +
1515 +
1516 +/* ======================== Local structures ======================== */
1517 +
1518 +
1519 +typedef struct bluecard_info_t {
1520 +       dev_link_t link;
1521 +       dev_node_t node;
1522 +
1523 +       struct hci_dev hdev;
1524 +
1525 +       spinlock_t lock;                /* For serializing operations */
1526 +       struct timer_list timer;        /* For LED control */
1527 +
1528 +       struct sk_buff_head txq;
1529 +       unsigned long tx_state;
1530 +
1531 +       unsigned long rx_state;
1532 +       unsigned long rx_count;
1533 +       struct sk_buff *rx_skb;
1534 +
1535 +       unsigned char ctrl_reg;
1536 +       unsigned long hw_state;         /* Status of the hardware and LED control */
1537 +} bluecard_info_t;
1538 +
1539 +
1540 +void bluecard_config(dev_link_t *link);
1541 +void bluecard_release(u_long arg);
1542 +int bluecard_event(event_t event, int priority, event_callback_args_t *args);
1543 +
1544 +static dev_info_t dev_info = "bluecard_cs";
1545 +
1546 +dev_link_t *bluecard_attach(void);
1547 +void bluecard_detach(dev_link_t *);
1548 +
1549 +static dev_link_t *dev_list = NULL;
1550 +
1551 +
1552 +/* Default baud rate: 57600, 115200, 230400 or 460800 */
1553 +#define DEFAULT_BAUD_RATE  230400
1554 +
1555 +
1556 +/* Hardware states */
1557 +#define CARD_READY             1
1558 +#define CARD_HAS_PCCARD_ID     4
1559 +#define CARD_HAS_POWER_LED     5
1560 +#define CARD_HAS_ACTIVITY_LED  6
1561 +
1562 +/* Transmit states  */
1563 +#define XMIT_SENDING         1
1564 +#define XMIT_WAKEUP          2
1565 +#define XMIT_BUFFER_NUMBER   5 /* unset = buffer one, set = buffer two */
1566 +#define XMIT_BUF_ONE_READY   6
1567 +#define XMIT_BUF_TWO_READY   7
1568 +#define XMIT_SENDING_READY   8
1569 +
1570 +/* Receiver states */
1571 +#define RECV_WAIT_PACKET_TYPE   0
1572 +#define RECV_WAIT_EVENT_HEADER  1
1573 +#define RECV_WAIT_ACL_HEADER    2
1574 +#define RECV_WAIT_SCO_HEADER    3
1575 +#define RECV_WAIT_DATA          4
1576 +
1577 +/* Special packet types */
1578 +#define PKT_BAUD_RATE_57600   0x80
1579 +#define PKT_BAUD_RATE_115200  0x81
1580 +#define PKT_BAUD_RATE_230400  0x82
1581 +#define PKT_BAUD_RATE_460800  0x83
1582 +
1583 +
1584 +/* These are the register offsets */
1585 +#define REG_COMMAND     0x20
1586 +#define REG_INTERRUPT   0x21
1587 +#define REG_CONTROL     0x22
1588 +#define REG_RX_CONTROL  0x24
1589 +#define REG_CARD_RESET  0x30
1590 +#define REG_LED_CTRL    0x30
1591 +
1592 +/* REG_COMMAND */
1593 +#define REG_COMMAND_TX_BUF_ONE  0x01
1594 +#define REG_COMMAND_TX_BUF_TWO  0x02
1595 +#define REG_COMMAND_RX_BUF_ONE  0x04
1596 +#define REG_COMMAND_RX_BUF_TWO  0x08
1597 +#define REG_COMMAND_RX_WIN_ONE  0x00
1598 +#define REG_COMMAND_RX_WIN_TWO  0x10
1599 +
1600 +/* REG_CONTROL */
1601 +#define REG_CONTROL_BAUD_RATE_57600   0x00
1602 +#define REG_CONTROL_BAUD_RATE_115200  0x01
1603 +#define REG_CONTROL_BAUD_RATE_230400  0x02
1604 +#define REG_CONTROL_BAUD_RATE_460800  0x03
1605 +#define REG_CONTROL_RTS               0x04
1606 +#define REG_CONTROL_BT_ON             0x08
1607 +#define REG_CONTROL_BT_RESET          0x10
1608 +#define REG_CONTROL_BT_RES_PU         0x20
1609 +#define REG_CONTROL_INTERRUPT         0x40
1610 +#define REG_CONTROL_CARD_RESET        0x80
1611 +
1612 +/* REG_RX_CONTROL */
1613 +#define RTS_LEVEL_SHIFT_BITS  0x02
1614 +
1615 +
1616 +
1617 +/* ======================== LED handling routines ======================== */
1618 +
1619 +
1620 +void bluecard_activity_led_timeout(u_long arg)
1621 +{
1622 +       bluecard_info_t *info = (bluecard_info_t *)arg;
1623 +       unsigned int iobase = info->link.io.BasePort1;
1624 +
1625 +       if (test_bit(CARD_HAS_ACTIVITY_LED, &(info->hw_state))) {
1626 +               /* Disable activity LED */
1627 +               outb(0x08 | 0x20, iobase + 0x30);
1628 +       } else {
1629 +               /* Disable power LED */
1630 +               outb(0x00, iobase + 0x30);
1631 +       }
1632 +}
1633 +
1634 +
1635 +static void bluecard_enable_activity_led(bluecard_info_t *info)
1636 +{
1637 +       unsigned int iobase = info->link.io.BasePort1;
1638 +
1639 +       if (test_bit(CARD_HAS_ACTIVITY_LED, &(info->hw_state))) {
1640 +               /* Enable activity LED */
1641 +               outb(0x10 | 0x40, iobase + 0x30);
1642 +
1643 +               /* Stop the LED after HZ/4 */
1644 +               mod_timer(&(info->timer), jiffies + HZ / 4);
1645 +       } else {
1646 +               /* Enable power LED */
1647 +               outb(0x08 | 0x20, iobase + 0x30);
1648 +
1649 +               /* Stop the LED after HZ/2 */
1650 +               mod_timer(&(info->timer), jiffies + HZ / 2);
1651 +       }
1652 +}
1653 +
1654 +
1655 +
1656 +/* ======================== Interrupt handling ======================== */
1657 +
1658 +
1659 +static int bluecard_write(unsigned int iobase, unsigned int offset, __u8 *buf, int len)
1660 +{
1661 +       int i, actual;
1662 +
1663 +       actual = (len > 15) ? 15 : len;
1664 +
1665 +       outb_p(actual, iobase + offset);
1666 +
1667 +       for (i = 0; i < actual; i++)
1668 +               outb_p(buf[i], iobase + offset + i + 1);
1669 +
1670 +       return actual;
1671 +}
1672 +
1673 +
1674 +static void bluecard_write_wakeup(bluecard_info_t *info)
1675 +{
1676 +       if (!info) {
1677 +               printk(KERN_WARNING "bluecard_cs: Call of write_wakeup for unknown device.\n");
1678 +               return;
1679 +       }
1680 +
1681 +       if (!test_bit(XMIT_SENDING_READY, &(info->tx_state)))
1682 +               return;
1683 +
1684 +       if (test_and_set_bit(XMIT_SENDING, &(info->tx_state))) {
1685 +               set_bit(XMIT_WAKEUP, &(info->tx_state));
1686 +               return;
1687 +       }
1688 +
1689 +       do {
1690 +               register unsigned int iobase = info->link.io.BasePort1;
1691 +               register unsigned int offset;
1692 +               register unsigned char command;
1693 +               register unsigned long ready_bit;
1694 +               register struct sk_buff *skb;
1695 +               register int len;
1696 +
1697 +               clear_bit(XMIT_WAKEUP, &(info->tx_state));
1698 +
1699 +               if (!(info->link.state & DEV_PRESENT))
1700 +                       return;
1701 +
1702 +               if (test_bit(XMIT_BUFFER_NUMBER, &(info->tx_state))) {
1703 +                       if (!test_bit(XMIT_BUF_TWO_READY, &(info->tx_state)))
1704 +                               break;
1705 +                       offset = 0x10;
1706 +                       command = REG_COMMAND_TX_BUF_TWO;
1707 +                       ready_bit = XMIT_BUF_TWO_READY;
1708 +               } else {
1709 +                       if (!test_bit(XMIT_BUF_ONE_READY, &(info->tx_state)))
1710 +                               break;
1711 +                       offset = 0x00;
1712 +                       command = REG_COMMAND_TX_BUF_ONE;
1713 +                       ready_bit = XMIT_BUF_ONE_READY;
1714 +               }
1715 +
1716 +               if (!(skb = skb_dequeue(&(info->txq))))
1717 +                       break;
1718 +
1719 +               if (skb->pkt_type & 0x80) {
1720 +                       /* Disable RTS */
1721 +                       info->ctrl_reg |= REG_CONTROL_RTS;
1722 +                       outb(info->ctrl_reg, iobase + REG_CONTROL);
1723 +               }
1724 +
1725 +               /* Activate LED */
1726 +               bluecard_enable_activity_led(info);
1727 +
1728 +               /* Send frame */
1729 +               len = bluecard_write(iobase, offset, skb->data, skb->len);
1730 +
1731 +               /* Tell the FPGA to send the data */
1732 +               outb_p(command, iobase + REG_COMMAND);
1733 +
1734 +               /* Mark the buffer as dirty */
1735 +               clear_bit(ready_bit, &(info->tx_state));
1736 +
1737 +               if (skb->pkt_type & 0x80) {
1738 +
1739 +                       wait_queue_head_t wait;
1740 +                       unsigned char baud_reg;
1741 +
1742 +                       switch (skb->pkt_type) {
1743 +                       case PKT_BAUD_RATE_460800:
1744 +                               baud_reg = REG_CONTROL_BAUD_RATE_460800;
1745 +                               break;
1746 +                       case PKT_BAUD_RATE_230400:
1747 +                               baud_reg = REG_CONTROL_BAUD_RATE_230400;
1748 +                               break;
1749 +                       case PKT_BAUD_RATE_115200:
1750 +                               baud_reg = REG_CONTROL_BAUD_RATE_115200;
1751 +                               break;
1752 +                       case PKT_BAUD_RATE_57600:
1753 +                               /* Fall through... */
1754 +                       default:
1755 +                               baud_reg = REG_CONTROL_BAUD_RATE_57600;
1756 +                               break;
1757 +                       }
1758 +
1759 +                       /* Wait until the command reaches the baseband */
1760 +                       init_waitqueue_head(&wait);
1761 +                       interruptible_sleep_on_timeout(&wait, HZ / 10);
1762 +
1763 +                       /* Set baud on baseband */
1764 +                       info->ctrl_reg &= ~0x03;
1765 +                       info->ctrl_reg |= baud_reg;
1766 +                       outb(info->ctrl_reg, iobase + REG_CONTROL);
1767 +
1768 +                       /* Enable RTS */
1769 +                       info->ctrl_reg &= ~REG_CONTROL_RTS;
1770 +                       outb(info->ctrl_reg, iobase + REG_CONTROL);
1771 +
1772 +                       /* Wait before the next HCI packet can be send */
1773 +                       interruptible_sleep_on_timeout(&wait, HZ);
1774 +
1775 +               }
1776 +
1777 +               if (len == skb->len) {
1778 +                       kfree_skb(skb);
1779 +               } else {
1780 +                       skb_pull(skb, len);
1781 +                       skb_queue_head(&(info->txq), skb);
1782 +               }
1783 +
1784 +               info->hdev.stat.byte_tx += len;
1785 +
1786 +               /* Change buffer */
1787 +               change_bit(XMIT_BUFFER_NUMBER, &(info->tx_state));
1788 +
1789 +       } while (test_bit(XMIT_WAKEUP, &(info->tx_state)));
1790 +
1791 +       clear_bit(XMIT_SENDING, &(info->tx_state));
1792 +}
1793 +
1794 +
1795 +static int bluecard_read(unsigned int iobase, unsigned int offset, __u8 *buf, int size)
1796 +{
1797 +       int i, n, len;
1798 +
1799 +       outb(REG_COMMAND_RX_WIN_ONE, iobase + REG_COMMAND);
1800 +
1801 +       len = inb(iobase + offset);
1802 +       n = 0;
1803 +       i = 1;
1804 +
1805 +       while (n < len) {
1806 +
1807 +               if (i == 16) {
1808 +                       outb(REG_COMMAND_RX_WIN_TWO, iobase + REG_COMMAND);
1809 +                       i = 0;
1810 +               }
1811 +
1812 +               buf[n] = inb(iobase + offset + i);
1813 +
1814 +               n++;
1815 +               i++;
1816 +
1817 +       }
1818 +
1819 +       return len;
1820 +}
1821 +
1822 +
1823 +static void bluecard_receive(bluecard_info_t *info, unsigned int offset)
1824 +{
1825 +       unsigned int iobase;
1826 +       unsigned char buf[31];
1827 +       int i, len;
1828 +
1829 +       if (!info) {
1830 +               printk(KERN_WARNING "bluecard_cs: Call of receive for unknown device.\n");
1831 +               return;
1832 +       }
1833 +
1834 +       iobase = info->link.io.BasePort1;
1835 +
1836 +       if (test_bit(XMIT_SENDING_READY, &(info->tx_state)))
1837 +               bluecard_enable_activity_led(info);
1838 +
1839 +       len = bluecard_read(iobase, offset, buf, sizeof(buf));
1840 +
1841 +       for (i = 0; i < len; i++) {
1842 +
1843 +               /* Allocate packet */
1844 +               if (info->rx_skb == NULL) {
1845 +                       info->rx_state = RECV_WAIT_PACKET_TYPE;
1846 +                       info->rx_count = 0;
1847 +                       if (!(info->rx_skb = bluez_skb_alloc(HCI_MAX_FRAME_SIZE, GFP_ATOMIC))) {
1848 +                               printk(KERN_WARNING "bluecard_cs: Can't allocate mem for new packet.\n");
1849 +                               return;
1850 +                       }
1851 +               }
1852 +
1853 +               if (info->rx_state == RECV_WAIT_PACKET_TYPE) {
1854 +
1855 +                       info->rx_skb->dev = (void *)&(info->hdev);
1856 +                       info->rx_skb->pkt_type = buf[i];
1857 +
1858 +                       switch (info->rx_skb->pkt_type) {
1859 +
1860 +                       case 0x00:
1861 +                               /* init packet */
1862 +                               if (offset != 0x00) {
1863 +                                       set_bit(XMIT_BUF_ONE_READY, &(info->tx_state));
1864 +                                       set_bit(XMIT_BUF_TWO_READY, &(info->tx_state));
1865 +                                       set_bit(XMIT_SENDING_READY, &(info->tx_state));
1866 +                                       bluecard_write_wakeup(info);
1867 +                               }
1868 +
1869 +                               kfree_skb(info->rx_skb);
1870 +                               info->rx_skb = NULL;
1871 +                               break;
1872 +
1873 +                       case HCI_EVENT_PKT:
1874 +                               info->rx_state = RECV_WAIT_EVENT_HEADER;
1875 +                               info->rx_count = HCI_EVENT_HDR_SIZE;
1876 +                               break;
1877 +
1878 +                       case HCI_ACLDATA_PKT:
1879 +                               info->rx_state = RECV_WAIT_ACL_HEADER;
1880 +                               info->rx_count = HCI_ACL_HDR_SIZE;
1881 +                               break;
1882 +
1883 +                       case HCI_SCODATA_PKT:
1884 +                               info->rx_state = RECV_WAIT_SCO_HEADER;
1885 +                               info->rx_count = HCI_SCO_HDR_SIZE;
1886 +                               break;
1887 +
1888 +                       default:
1889 +                               /* unknown packet */
1890 +                               printk(KERN_WARNING "bluecard_cs: Unknown HCI packet with type 0x%02x received.\n", info->rx_skb->pkt_type);
1891 +                               info->hdev.stat.err_rx++;
1892 +
1893 +                               kfree_skb(info->rx_skb);
1894 +                               info->rx_skb = NULL;
1895 +                               break;
1896 +
1897 +                       }
1898 +
1899 +               } else {
1900 +
1901 +                       *skb_put(info->rx_skb, 1) = buf[i];
1902 +                       info->rx_count--;
1903 +
1904 +                       if (info->rx_count == 0) {
1905 +
1906 +                               int dlen;
1907 +                               hci_event_hdr *eh;
1908 +                               hci_acl_hdr *ah;
1909 +                               hci_sco_hdr *sh;
1910 +
1911 +                               switch (info->rx_state) {
1912 +
1913 +                               case RECV_WAIT_EVENT_HEADER:
1914 +                                       eh = (hci_event_hdr *)(info->rx_skb->data);
1915 +                                       info->rx_state = RECV_WAIT_DATA;
1916 +                                       info->rx_count = eh->plen;
1917 +                                       break;
1918 +
1919 +                               case RECV_WAIT_ACL_HEADER:
1920 +                                       ah = (hci_acl_hdr *)(info->rx_skb->data);
1921 +                                       dlen = __le16_to_cpu(ah->dlen);
1922 +                                       info->rx_state = RECV_WAIT_DATA;
1923 +                                       info->rx_count = dlen;
1924 +                                       break;
1925 +
1926 +                               case RECV_WAIT_SCO_HEADER:
1927 +                                       sh = (hci_sco_hdr *)(info->rx_skb->data);
1928 +                                       info->rx_state = RECV_WAIT_DATA;
1929 +                                       info->rx_count = sh->dlen;
1930 +                                       break;
1931 +
1932 +                               case RECV_WAIT_DATA:
1933 +                                       hci_recv_frame(info->rx_skb);
1934 +                                       info->rx_skb = NULL;
1935 +                                       break;
1936 +
1937 +                               }
1938 +
1939 +                       }
1940 +
1941 +               }
1942 +
1943 +
1944 +       }
1945 +
1946 +       info->hdev.stat.byte_rx += len;
1947 +}
1948 +
1949 +
1950 +void bluecard_interrupt(int irq, void *dev_inst, struct pt_regs *regs)
1951 +{
1952 +       bluecard_info_t *info = dev_inst;
1953 +       unsigned int iobase;
1954 +       unsigned char reg;
1955 +
1956 +       if (!info) {
1957 +               printk(KERN_WARNING "bluecard_cs: Call of irq %d for unknown device.\n", irq);
1958 +               return;
1959 +       }
1960 +
1961 +       if (!test_bit(CARD_READY, &(info->hw_state)))
1962 +               return;
1963 +
1964 +       iobase = info->link.io.BasePort1;
1965 +
1966 +       spin_lock(&(info->lock));
1967 +
1968 +       /* Disable interrupt */
1969 +       info->ctrl_reg &= ~REG_CONTROL_INTERRUPT;
1970 +       outb(info->ctrl_reg, iobase + REG_CONTROL);
1971 +
1972 +       reg = inb(iobase + REG_INTERRUPT);
1973 +
1974 +       if ((reg != 0x00) && (reg != 0xff)) {
1975 +
1976 +               if (reg & 0x04) {
1977 +                       bluecard_receive(info, 0x00);
1978 +                       outb(0x04, iobase + REG_INTERRUPT);
1979 +                       outb(REG_COMMAND_RX_BUF_ONE, iobase + REG_COMMAND);
1980 +               }
1981 +
1982 +               if (reg & 0x08) {
1983 +                       bluecard_receive(info, 0x10);
1984 +                       outb(0x08, iobase + REG_INTERRUPT);
1985 +                       outb(REG_COMMAND_RX_BUF_TWO, iobase + REG_COMMAND);
1986 +               }
1987 +
1988 +               if (reg & 0x01) {
1989 +                       set_bit(XMIT_BUF_ONE_READY, &(info->tx_state));
1990 +                       outb(0x01, iobase + REG_INTERRUPT);
1991 +                       bluecard_write_wakeup(info);
1992 +               }
1993 +
1994 +               if (reg & 0x02) {
1995 +                       set_bit(XMIT_BUF_TWO_READY, &(info->tx_state));
1996 +                       outb(0x02, iobase + REG_INTERRUPT);
1997 +                       bluecard_write_wakeup(info);
1998 +               }
1999 +
2000 +       }
2001 +
2002 +       /* Enable interrupt */
2003 +       info->ctrl_reg |= REG_CONTROL_INTERRUPT;
2004 +       outb(info->ctrl_reg, iobase + REG_CONTROL);
2005 +
2006 +       spin_unlock(&(info->lock));
2007 +}
2008 +
2009 +
2010 +
2011 +/* ======================== Device specific HCI commands ======================== */
2012 +
2013 +
2014 +static int bluecard_hci_set_baud_rate(struct hci_dev *hdev, int baud)
2015 +{
2016 +       bluecard_info_t *info = (bluecard_info_t *)(hdev->driver_data);
2017 +       struct sk_buff *skb;
2018 +
2019 +       /* Ericsson baud rate command */
2020 +       unsigned char cmd[] = { HCI_COMMAND_PKT, 0x09, 0xfc, 0x01, 0x03 };
2021 +
2022 +       if (!(skb = bluez_skb_alloc(HCI_MAX_FRAME_SIZE, GFP_ATOMIC))) {
2023 +               printk(KERN_WARNING "bluecard_cs: Can't allocate mem for new packet.\n");
2024 +               return -1;
2025 +       }
2026 +
2027 +       switch (baud) {
2028 +       case 460800:
2029 +               cmd[4] = 0x00;
2030 +               skb->pkt_type = PKT_BAUD_RATE_460800;
2031 +               break;
2032 +       case 230400:
2033 +               cmd[4] = 0x01;
2034 +               skb->pkt_type = PKT_BAUD_RATE_230400;
2035 +               break;
2036 +       case 115200:
2037 +               cmd[4] = 0x02;
2038 +               skb->pkt_type = PKT_BAUD_RATE_115200;
2039 +               break;
2040 +       case 57600:
2041 +               /* Fall through... */
2042 +       default:
2043 +               cmd[4] = 0x03;
2044 +               skb->pkt_type = PKT_BAUD_RATE_57600;
2045 +               break;
2046 +       }
2047 +
2048 +       memcpy(skb_put(skb, sizeof(cmd)), cmd, sizeof(cmd));
2049 +
2050 +       skb_queue_tail(&(info->txq), skb);
2051 +
2052 +       bluecard_write_wakeup(info);
2053 +
2054 +       return 0;
2055 +}
2056 +
2057 +
2058 +
2059 +/* ======================== HCI interface ======================== */
2060 +
2061 +
2062 +static int bluecard_hci_flush(struct hci_dev *hdev)
2063 +{
2064 +       bluecard_info_t *info = (bluecard_info_t *)(hdev->driver_data);
2065 +
2066 +       /* Drop TX queue */
2067 +       skb_queue_purge(&(info->txq));
2068 +
2069 +       return 0;
2070 +}
2071 +
2072 +
2073 +static int bluecard_hci_open(struct hci_dev *hdev)
2074 +{
2075 +       bluecard_info_t *info = (bluecard_info_t *)(hdev->driver_data);
2076 +       unsigned int iobase = info->link.io.BasePort1;
2077 +
2078 +       bluecard_hci_set_baud_rate(hdev, DEFAULT_BAUD_RATE);
2079 +
2080 +       if (test_and_set_bit(HCI_RUNNING, &(hdev->flags)))
2081 +               return 0;
2082 +
2083 +       /* Enable LED */
2084 +       outb(0x08 | 0x20, iobase + 0x30);
2085 +
2086 +       return 0;
2087 +}
2088 +
2089 +
2090 +static int bluecard_hci_close(struct hci_dev *hdev)
2091 +{
2092 +       bluecard_info_t *info = (bluecard_info_t *)(hdev->driver_data);
2093 +       unsigned int iobase = info->link.io.BasePort1;
2094 +
2095 +       if (!test_and_clear_bit(HCI_RUNNING, &(hdev->flags)))
2096 +               return 0;
2097 +
2098 +       bluecard_hci_flush(hdev);
2099 +
2100 +       /* Disable LED */
2101 +       outb(0x00, iobase + 0x30);
2102 +
2103 +       return 0;
2104 +}
2105 +
2106 +
2107 +static int bluecard_hci_send_frame(struct sk_buff *skb)
2108 +{
2109 +       bluecard_info_t *info;
2110 +       struct hci_dev *hdev = (struct hci_dev *)(skb->dev);
2111 +
2112 +       if (!hdev) {
2113 +               printk(KERN_WARNING "bluecard_cs: Frame for unknown HCI device (hdev=NULL).");
2114 +               return -ENODEV;
2115 +       }
2116 +
2117 +       info = (bluecard_info_t *)(hdev->driver_data);
2118 +
2119 +       switch (skb->pkt_type) {
2120 +       case HCI_COMMAND_PKT:
2121 +               hdev->stat.cmd_tx++;
2122 +               break;
2123 +       case HCI_ACLDATA_PKT:
2124 +               hdev->stat.acl_tx++;
2125 +               break;
2126 +       case HCI_SCODATA_PKT:
2127 +               hdev->stat.sco_tx++;
2128 +               break;
2129 +       };
2130 +
2131 +       /* Prepend skb with frame type */
2132 +       memcpy(skb_push(skb, 1), &(skb->pkt_type), 1);
2133 +       skb_queue_tail(&(info->txq), skb);
2134 +
2135 +       bluecard_write_wakeup(info);
2136 +
2137 +       return 0;
2138 +}
2139 +
2140 +
2141 +static void bluecard_hci_destruct(struct hci_dev *hdev)
2142 +{
2143 +}
2144 +
2145 +
2146 +static int bluecard_hci_ioctl(struct hci_dev *hdev, unsigned int cmd, unsigned long arg)
2147 +{
2148 +       return -ENOIOCTLCMD;
2149 +}
2150 +
2151 +
2152 +
2153 +/* ======================== Card services HCI interaction ======================== */
2154 +
2155 +
2156 +int bluecard_open(bluecard_info_t *info)
2157 +{
2158 +       unsigned int iobase = info->link.io.BasePort1;
2159 +       struct hci_dev *hdev;
2160 +       unsigned char id;
2161 +
2162 +       spin_lock_init(&(info->lock));
2163 +
2164 +       init_timer(&(info->timer));
2165 +       info->timer.function = &bluecard_activity_led_timeout;
2166 +       info->timer.data = (u_long)info;
2167 +
2168 +       skb_queue_head_init(&(info->txq));
2169 +
2170 +       info->rx_state = RECV_WAIT_PACKET_TYPE;
2171 +       info->rx_count = 0;
2172 +       info->rx_skb = NULL;
2173 +
2174 +       id = inb(iobase + 0x30);
2175 +
2176 +       if ((id & 0x0f) == 0x02)
2177 +               set_bit(CARD_HAS_PCCARD_ID, &(info->hw_state));
2178 +
2179 +       if (id & 0x10)
2180 +               set_bit(CARD_HAS_POWER_LED, &(info->hw_state));
2181 +
2182 +       if (id & 0x20)
2183 +               set_bit(CARD_HAS_ACTIVITY_LED, &(info->hw_state));
2184 +
2185 +       /* Reset card */
2186 +       info->ctrl_reg = REG_CONTROL_BT_RESET | REG_CONTROL_CARD_RESET;
2187 +       outb(info->ctrl_reg, iobase + REG_CONTROL);
2188 +
2189 +       /* Turn FPGA off */
2190 +       outb(0x80, iobase + 0x30);
2191 +
2192 +       /* Wait some time */
2193 +       set_current_state(TASK_INTERRUPTIBLE);
2194 +       schedule_timeout(HZ / 100);
2195 +
2196 +       /* Turn FPGA on */
2197 +       outb(0x00, iobase + 0x30);
2198 +
2199 +       /* Activate card */
2200 +       info->ctrl_reg = REG_CONTROL_BT_ON | REG_CONTROL_BT_RES_PU;
2201 +       outb(info->ctrl_reg, iobase + REG_CONTROL);
2202 +
2203 +       /* Enable interrupt */
2204 +       outb(0xff, iobase + REG_INTERRUPT);
2205 +       info->ctrl_reg |= REG_CONTROL_INTERRUPT;
2206 +       outb(info->ctrl_reg, iobase + REG_CONTROL);
2207 +
2208 +       /* Start the RX buffers */
2209 +       outb(REG_COMMAND_RX_BUF_ONE, iobase + REG_COMMAND);
2210 +       outb(REG_COMMAND_RX_BUF_TWO, iobase + REG_COMMAND);
2211 +
2212 +       /* Signal that the hardware is ready */
2213 +       set_bit(CARD_READY, &(info->hw_state));
2214 +
2215 +       /* Drop TX queue */
2216 +       skb_queue_purge(&(info->txq));
2217 +
2218 +       /* Control the point at which RTS is enabled */
2219 +       outb((0x0f << RTS_LEVEL_SHIFT_BITS) | 1, iobase + REG_RX_CONTROL);
2220 +
2221 +       /* Timeout before it is safe to send the first HCI packet */
2222 +       set_current_state(TASK_INTERRUPTIBLE);
2223 +       schedule_timeout((HZ * 5) / 4);         // or set it to 3/2
2224 +
2225 +
2226 +       /* Initialize and register HCI device */
2227 +
2228 +       hdev = &(info->hdev);
2229 +
2230 +       hdev->type = HCI_PCCARD;
2231 +       hdev->driver_data = info;
2232 +
2233 +       hdev->open = bluecard_hci_open;
2234 +       hdev->close = bluecard_hci_close;
2235 +       hdev->flush = bluecard_hci_flush;
2236 +       hdev->send = bluecard_hci_send_frame;
2237 +       hdev->destruct = bluecard_hci_destruct;
2238 +       hdev->ioctl = bluecard_hci_ioctl;
2239 +
2240 +       if (hci_register_dev(hdev) < 0) {
2241 +               printk(KERN_WARNING "bluecard_cs: Can't register HCI device %s.\n", hdev->name);
2242 +               return -ENODEV;
2243 +       }
2244 +
2245 +       return 0;
2246 +}
2247 +
2248 +
2249 +int bluecard_close(bluecard_info_t *info)
2250 +{
2251 +       unsigned int iobase = info->link.io.BasePort1;
2252 +       struct hci_dev *hdev = &(info->hdev);
2253 +
2254 +       if (info->link.state & DEV_CONFIG_PENDING)
2255 +               return -ENODEV;
2256 +
2257 +       bluecard_hci_close(hdev);
2258 +
2259 +       clear_bit(CARD_READY, &(info->hw_state));
2260 +
2261 +       /* Reset card */
2262 +       info->ctrl_reg = REG_CONTROL_BT_RESET | REG_CONTROL_CARD_RESET;
2263 +       outb(info->ctrl_reg, iobase + REG_CONTROL);
2264 +
2265 +       /* Turn FPGA off */
2266 +       outb(0x80, iobase + 0x30);
2267 +
2268 +       if (hci_unregister_dev(hdev) < 0)
2269 +               printk(KERN_WARNING "bluecard_cs: Can't unregister HCI device %s.\n", hdev->name);
2270 +
2271 +       return 0;
2272 +}
2273 +
2274 +
2275 +
2276 +/* ======================== Card services ======================== */
2277 +
2278 +
2279 +static void cs_error(client_handle_t handle, int func, int ret)
2280 +{
2281 +       error_info_t err = { func, ret };
2282 +
2283 +       CardServices(ReportError, handle, &err);
2284 +}
2285 +
2286 +
2287 +dev_link_t *bluecard_attach(void)
2288 +{
2289 +       bluecard_info_t *info;
2290 +       client_reg_t client_reg;
2291 +       dev_link_t *link;
2292 +       int i, ret;
2293 +
2294 +       /* Create new info device */
2295 +       info = kmalloc(sizeof(*info), GFP_KERNEL);
2296 +       if (!info)
2297 +               return NULL;
2298 +       memset(info, 0, sizeof(*info));
2299 +
2300 +       link = &info->link;
2301 +       link->priv = info;
2302 +
2303 +       link->release.function = &bluecard_release;
2304 +       link->release.data = (u_long)link;
2305 +       link->io.Attributes1 = IO_DATA_PATH_WIDTH_8;
2306 +       link->io.NumPorts1 = 8;
2307 +       link->irq.Attributes = IRQ_TYPE_EXCLUSIVE | IRQ_HANDLE_PRESENT;
2308 +       link->irq.IRQInfo1 = IRQ_INFO2_VALID | IRQ_LEVEL_ID;
2309 +
2310 +       if (irq_list[0] == -1)
2311 +               link->irq.IRQInfo2 = irq_mask;
2312 +       else
2313 +               for (i = 0; i < 4; i++)
2314 +                       link->irq.IRQInfo2 |= 1 << irq_list[i];
2315 +
2316 +       link->irq.Handler = bluecard_interrupt;
2317 +       link->irq.Instance = info;
2318 +
2319 +       link->conf.Attributes = CONF_ENABLE_IRQ;
2320 +       link->conf.Vcc = 50;
2321 +       link->conf.IntType = INT_MEMORY_AND_IO;
2322 +
2323 +       /* Register with Card Services */
2324 +       link->next = dev_list;
2325 +       dev_list = link;
2326 +       client_reg.dev_info = &dev_info;
2327 +       client_reg.Attributes = INFO_IO_CLIENT | INFO_CARD_SHARE;
2328 +       client_reg.EventMask =
2329 +               CS_EVENT_CARD_INSERTION | CS_EVENT_CARD_REMOVAL |
2330 +               CS_EVENT_RESET_PHYSICAL | CS_EVENT_CARD_RESET |
2331 +               CS_EVENT_PM_SUSPEND | CS_EVENT_PM_RESUME;
2332 +       client_reg.event_handler = &bluecard_event;
2333 +       client_reg.Version = 0x0210;
2334 +       client_reg.event_callback_args.client_data = link;
2335 +
2336 +       ret = CardServices(RegisterClient, &link->handle, &client_reg);
2337 +       if (ret != CS_SUCCESS) {
2338 +               cs_error(link->handle, RegisterClient, ret);
2339 +               bluecard_detach(link);
2340 +               return NULL;
2341 +       }
2342 +
2343 +       return link;
2344 +}
2345 +
2346 +
2347 +void bluecard_detach(dev_link_t *link)
2348 +{
2349 +       bluecard_info_t *info = link->priv;
2350 +       dev_link_t **linkp;
2351 +       int ret;
2352 +
2353 +       /* Locate device structure */
2354 +       for (linkp = &dev_list; *linkp; linkp = &(*linkp)->next)
2355 +               if (*linkp == link)
2356 +                       break;
2357 +
2358 +       if (*linkp == NULL)
2359 +               return;
2360 +
2361 +       del_timer(&link->release);
2362 +       if (link->state & DEV_CONFIG)
2363 +               bluecard_release((u_long)link);
2364 +
2365 +       if (link->handle) {
2366 +               ret = CardServices(DeregisterClient, link->handle);
2367 +               if (ret != CS_SUCCESS)
2368 +                       cs_error(link->handle, DeregisterClient, ret);
2369 +       }
2370 +
2371 +       /* Unlink device structure, free bits */
2372 +       *linkp = link->next;
2373 +
2374 +       kfree(info);
2375 +}
2376 +
2377 +
2378 +static int get_tuple(int fn, client_handle_t handle, tuple_t *tuple, cisparse_t *parse)
2379 +{
2380 +       int i;
2381 +
2382 +       i = CardServices(fn, handle, tuple);
2383 +       if (i != CS_SUCCESS)
2384 +               return CS_NO_MORE_ITEMS;
2385 +
2386 +       i = CardServices(GetTupleData, handle, tuple);
2387 +       if (i != CS_SUCCESS)
2388 +               return i;
2389 +
2390 +       return CardServices(ParseTuple, handle, tuple, parse);
2391 +}
2392 +
2393 +
2394 +#define first_tuple(a, b, c) get_tuple(GetFirstTuple, a, b, c)
2395 +#define next_tuple(a, b, c) get_tuple(GetNextTuple, a, b, c)
2396 +
2397 +void bluecard_config(dev_link_t *link)
2398 +{
2399 +       client_handle_t handle = link->handle;
2400 +       bluecard_info_t *info = link->priv;
2401 +       tuple_t tuple;
2402 +       u_short buf[256];
2403 +       cisparse_t parse;
2404 +       config_info_t config;
2405 +       int i, n, last_ret, last_fn;
2406 +
2407 +       tuple.TupleData = (cisdata_t *)buf;
2408 +       tuple.TupleOffset = 0;
2409 +       tuple.TupleDataMax = 255;
2410 +       tuple.Attributes = 0;
2411 +
2412 +       /* Get configuration register information */
2413 +       tuple.DesiredTuple = CISTPL_CONFIG;
2414 +       last_ret = first_tuple(handle, &tuple, &parse);
2415 +       if (last_ret != CS_SUCCESS) {
2416 +               last_fn = ParseTuple;
2417 +               goto cs_failed;
2418 +       }
2419 +       link->conf.ConfigBase = parse.config.base;
2420 +       link->conf.Present = parse.config.rmask[0];
2421 +
2422 +       /* Configure card */
2423 +       link->state |= DEV_CONFIG;
2424 +       i = CardServices(GetConfigurationInfo, handle, &config);
2425 +       link->conf.Vcc = config.Vcc;
2426 +
2427 +       link->conf.ConfigIndex = 0x20;
2428 +       link->io.NumPorts1 = 64;
2429 +       link->io.IOAddrLines = 6;
2430 +
2431 +       for (n = 0; n < 0x400; n += 0x40) {
2432 +               link->io.BasePort1 = n ^ 0x300;
2433 +               i = CardServices(RequestIO, link->handle, &link->io);
2434 +               if (i == CS_SUCCESS)
2435 +                       break;
2436 +       }
2437 +
2438 +       if (i != CS_SUCCESS) {
2439 +               cs_error(link->handle, RequestIO, i);
2440 +               goto failed;
2441 +       }
2442 +
2443 +       i = CardServices(RequestIRQ, link->handle, &link->irq);
2444 +       if (i != CS_SUCCESS) {
2445 +               cs_error(link->handle, RequestIRQ, i);
2446 +               link->irq.AssignedIRQ = 0;
2447 +       }
2448 +
2449 +       i = CardServices(RequestConfiguration, link->handle, &link->conf);
2450 +       if (i != CS_SUCCESS) {
2451 +               cs_error(link->handle, RequestConfiguration, i);
2452 +               goto failed;
2453 +       }
2454 +
2455 +       MOD_INC_USE_COUNT;
2456 +
2457 +       if (bluecard_open(info) != 0)
2458 +               goto failed;
2459 +
2460 +       strcpy(info->node.dev_name, info->hdev.name);
2461 +       link->dev = &info->node;
2462 +       link->state &= ~DEV_CONFIG_PENDING;
2463 +
2464 +       return;
2465 +
2466 +cs_failed:
2467 +       cs_error(link->handle, last_fn, last_ret);
2468 +
2469 +failed:
2470 +       bluecard_release((u_long)link);
2471 +}
2472 +
2473 +
2474 +void bluecard_release(u_long arg)
2475 +{
2476 +       dev_link_t *link = (dev_link_t *)arg;
2477 +       bluecard_info_t *info = link->priv;
2478 +
2479 +       if (link->state & DEV_PRESENT)
2480 +               bluecard_close(info);
2481 +
2482 +       MOD_DEC_USE_COUNT;
2483 +
2484 +       link->dev = NULL;
2485 +
2486 +       CardServices(ReleaseConfiguration, link->handle);
2487 +       CardServices(ReleaseIO, link->handle, &link->io);
2488 +       CardServices(ReleaseIRQ, link->handle, &link->irq);
2489 +
2490 +       link->state &= ~DEV_CONFIG;
2491 +}
2492 +
2493 +
2494 +int bluecard_event(event_t event, int priority, event_callback_args_t *args)
2495 +{
2496 +       dev_link_t *link = args->client_data;
2497 +       bluecard_info_t *info = link->priv;
2498 +
2499 +       switch (event) {
2500 +       case CS_EVENT_CARD_REMOVAL:
2501 +               link->state &= ~DEV_PRESENT;
2502 +               if (link->state & DEV_CONFIG) {
2503 +                       bluecard_close(info);
2504 +                       mod_timer(&link->release, jiffies + HZ / 20);
2505 +               }
2506 +               break;
2507 +       case CS_EVENT_CARD_INSERTION:
2508 +               link->state |= DEV_PRESENT | DEV_CONFIG_PENDING;
2509 +               bluecard_config(link);
2510 +               break;
2511 +       case CS_EVENT_PM_SUSPEND:
2512 +               link->state |= DEV_SUSPEND;
2513 +               /* Fall through... */
2514 +       case CS_EVENT_RESET_PHYSICAL:
2515 +               if (link->state & DEV_CONFIG)
2516 +                       CardServices(ReleaseConfiguration, link->handle);
2517 +               break;
2518 +       case CS_EVENT_PM_RESUME:
2519 +               link->state &= ~DEV_SUSPEND;
2520 +               /* Fall through... */
2521 +       case CS_EVENT_CARD_RESET:
2522 +               if (DEV_OK(link))
2523 +                       CardServices(RequestConfiguration, link->handle, &link->conf);
2524 +               break;
2525 +       }
2526 +
2527 +       return 0;
2528 +}
2529 +
2530 +
2531 +
2532 +/* ======================== Module initialization ======================== */
2533 +
2534 +
2535 +int __init init_bluecard_cs(void)
2536 +{
2537 +       servinfo_t serv;
2538 +       int err;
2539 +
2540 +       CardServices(GetCardServicesInfo, &serv);
2541 +       if (serv.Revision != CS_RELEASE_CODE) {
2542 +               printk(KERN_NOTICE "bluecard_cs: Card Services release does not match!\n");
2543 +               return -1;
2544 +       }
2545 +
2546 +       err = register_pccard_driver(&dev_info, &bluecard_attach, &bluecard_detach);
2547 +
2548 +       return err;
2549 +}
2550 +
2551 +
2552 +void __exit exit_bluecard_cs(void)
2553 +{
2554 +       unregister_pccard_driver(&dev_info);
2555 +
2556 +       while (dev_list != NULL)
2557 +               bluecard_detach(dev_list);
2558 +}
2559 +
2560 +
2561 +module_init(init_bluecard_cs);
2562 +module_exit(exit_bluecard_cs);
2563 +
2564 +EXPORT_NO_SYMBOLS;
2565 diff -urN linux-2.4.18/drivers/bluetooth/bt3c_cs.c linux-2.4.18-mh15/drivers/bluetooth/bt3c_cs.c
2566 --- linux-2.4.18/drivers/bluetooth/bt3c_cs.c    1970-01-01 01:00:00.000000000 +0100
2567 +++ linux-2.4.18-mh15/drivers/bluetooth/bt3c_cs.c       2004-08-01 16:26:23.000000000 +0200
2568 @@ -0,0 +1,986 @@
2569 +/*
2570 + *
2571 + *  Driver for the 3Com Bluetooth PCMCIA card
2572 + *
2573 + *  Copyright (C) 2001-2002  Marcel Holtmann <marcel@holtmann.org>
2574 + *                           Jose Orlando Pereira <jop@di.uminho.pt>
2575 + *
2576 + *
2577 + *  This program is free software; you can redistribute it and/or modify
2578 + *  it under the terms of the GNU General Public License version 2 as
2579 + *  published by the Free Software Foundation;
2580 + *
2581 + *  Software distributed under the License is distributed on an "AS
2582 + *  IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
2583 + *  implied. See the License for the specific language governing
2584 + *  rights and limitations under the License.
2585 + *
2586 + *  The initial developer of the original code is David A. Hinds
2587 + *  <dahinds@users.sourceforge.net>.  Portions created by David A. Hinds
2588 + *  are Copyright (C) 1999 David A. Hinds.  All Rights Reserved.
2589 + *
2590 + */
2591 +
2592 +#include <linux/config.h>
2593 +#include <linux/module.h>
2594 +
2595 +#include <linux/kernel.h>
2596 +#include <linux/kmod.h>
2597 +#include <linux/init.h>
2598 +#include <linux/slab.h>
2599 +#include <linux/types.h>
2600 +#include <linux/sched.h>
2601 +#include <linux/delay.h>
2602 +#include <linux/timer.h>
2603 +#include <linux/errno.h>
2604 +#include <linux/unistd.h>
2605 +#include <linux/ptrace.h>
2606 +#include <linux/ioport.h>
2607 +#include <linux/spinlock.h>
2608 +
2609 +#include <linux/skbuff.h>
2610 +#include <linux/string.h>
2611 +#include <linux/serial.h>
2612 +#include <linux/serial_reg.h>
2613 +#include <asm/system.h>
2614 +#include <asm/bitops.h>
2615 +#include <asm/io.h>
2616 +
2617 +#include <linux/firmware.h>
2618 +
2619 +#include <pcmcia/version.h>
2620 +#include <pcmcia/cs_types.h>
2621 +#include <pcmcia/cs.h>
2622 +#include <pcmcia/cistpl.h>
2623 +#include <pcmcia/ciscode.h>
2624 +#include <pcmcia/ds.h>
2625 +#include <pcmcia/cisreg.h>
2626 +
2627 +#include <net/bluetooth/bluetooth.h>
2628 +#include <net/bluetooth/hci_core.h>
2629 +
2630 +
2631 +
2632 +/* ======================== Module parameters ======================== */
2633 +
2634 +
2635 +/* Bit map of interrupts to choose from */
2636 +static u_int irq_mask = 0xffff;
2637 +static int irq_list[4] = { -1 };
2638 +
2639 +MODULE_PARM(irq_mask, "i");
2640 +MODULE_PARM(irq_list, "1-4i");
2641 +
2642 +MODULE_AUTHOR("Marcel Holtmann <marcel@holtmann.org>, Jose Orlando Pereira <jop@di.uminho.pt>");
2643 +MODULE_DESCRIPTION("BlueZ driver for the 3Com Bluetooth PCMCIA card");
2644 +MODULE_LICENSE("GPL");
2645 +
2646 +
2647 +
2648 +/* ======================== Local structures ======================== */
2649 +
2650 +
2651 +typedef struct bt3c_info_t {
2652 +       dev_link_t link;
2653 +       dev_node_t node;
2654 +
2655 +       struct hci_dev hdev;
2656 +
2657 +       spinlock_t lock;                /* For serializing operations */
2658 +
2659 +       struct sk_buff_head txq;
2660 +       unsigned long tx_state;
2661 +
2662 +       unsigned long rx_state;
2663 +       unsigned long rx_count;
2664 +       struct sk_buff *rx_skb;
2665 +} bt3c_info_t;
2666 +
2667 +
2668 +void bt3c_config(dev_link_t *link);
2669 +void bt3c_release(u_long arg);
2670 +int bt3c_event(event_t event, int priority, event_callback_args_t *args);
2671 +
2672 +static dev_info_t dev_info = "bt3c_cs";
2673 +
2674 +dev_link_t *bt3c_attach(void);
2675 +void bt3c_detach(dev_link_t *);
2676 +
2677 +static dev_link_t *dev_list = NULL;
2678 +
2679 +
2680 +/* Transmit states  */
2681 +#define XMIT_SENDING  1
2682 +#define XMIT_WAKEUP   2
2683 +#define XMIT_WAITING  8
2684 +
2685 +/* Receiver states */
2686 +#define RECV_WAIT_PACKET_TYPE   0
2687 +#define RECV_WAIT_EVENT_HEADER  1
2688 +#define RECV_WAIT_ACL_HEADER    2
2689 +#define RECV_WAIT_SCO_HEADER    3
2690 +#define RECV_WAIT_DATA          4
2691 +
2692 +
2693 +
2694 +/* ======================== Special I/O functions ======================== */
2695 +
2696 +
2697 +#define DATA_L   0
2698 +#define DATA_H   1
2699 +#define ADDR_L   2
2700 +#define ADDR_H   3
2701 +#define CONTROL  4
2702 +
2703 +
2704 +inline void bt3c_address(unsigned int iobase, unsigned short addr)
2705 +{
2706 +       outb(addr & 0xff, iobase + ADDR_L);
2707 +       outb((addr >> 8) & 0xff, iobase + ADDR_H);
2708 +}
2709 +
2710 +
2711 +inline void bt3c_put(unsigned int iobase, unsigned short value)
2712 +{
2713 +       outb(value & 0xff, iobase + DATA_L);
2714 +       outb((value >> 8) & 0xff, iobase + DATA_H);
2715 +}
2716 +
2717 +
2718 +inline void bt3c_io_write(unsigned int iobase, unsigned short addr, unsigned short value)
2719 +{
2720 +       bt3c_address(iobase, addr);
2721 +       bt3c_put(iobase, value);
2722 +}
2723 +
2724 +
2725 +inline unsigned short bt3c_get(unsigned int iobase)
2726 +{
2727 +       unsigned short value = inb(iobase + DATA_L);
2728 +
2729 +       value |= inb(iobase + DATA_H) << 8;
2730 +
2731 +       return value;
2732 +}
2733 +
2734 +
2735 +inline unsigned short bt3c_read(unsigned int iobase, unsigned short addr)
2736 +{
2737 +       bt3c_address(iobase, addr);
2738 +
2739 +       return bt3c_get(iobase);
2740 +}
2741 +
2742 +
2743 +
2744 +/* ======================== Interrupt handling ======================== */
2745 +
2746 +
2747 +static int bt3c_write(unsigned int iobase, int fifo_size, __u8 *buf, int len)
2748 +{
2749 +       int actual = 0;
2750 +
2751 +       bt3c_address(iobase, 0x7080);
2752 +
2753 +       /* Fill FIFO with current frame */
2754 +       while (actual < len) {
2755 +               /* Transmit next byte */
2756 +               bt3c_put(iobase, buf[actual]);
2757 +               actual++;
2758 +       }
2759 +
2760 +       bt3c_io_write(iobase, 0x7005, actual);
2761 +
2762 +       return actual;
2763 +}
2764 +
2765 +
2766 +static void bt3c_write_wakeup(bt3c_info_t *info, int from)
2767 +{
2768 +       unsigned long flags;
2769 +
2770 +       if (!info) {
2771 +               printk(KERN_WARNING "bt3c_cs: Call of write_wakeup for unknown device.\n");
2772 +               return;
2773 +       }
2774 +
2775 +       if (test_and_set_bit(XMIT_SENDING, &(info->tx_state)))
2776 +               return;
2777 +
2778 +       spin_lock_irqsave(&(info->lock), flags);
2779 +
2780 +       do {
2781 +               register unsigned int iobase = info->link.io.BasePort1;
2782 +               register struct sk_buff *skb;
2783 +               register int len;
2784 +
2785 +               if (!(info->link.state & DEV_PRESENT))
2786 +                       break;
2787 +
2788 +
2789 +               if (!(skb = skb_dequeue(&(info->txq)))) {
2790 +                       clear_bit(XMIT_SENDING, &(info->tx_state));
2791 +                       break;
2792 +               }
2793 +
2794 +               /* Send frame */
2795 +               len = bt3c_write(iobase, 256, skb->data, skb->len);
2796 +
2797 +               if (len != skb->len) {
2798 +                       printk(KERN_WARNING "bt3c_cs: very strange\n");
2799 +               }
2800 +
2801 +               kfree_skb(skb);
2802 +
2803 +               info->hdev.stat.byte_tx += len;
2804 +
2805 +       } while (0);
2806 +
2807 +       spin_unlock_irqrestore(&(info->lock), flags);
2808 +}
2809 +
2810 +
2811 +static void bt3c_receive(bt3c_info_t *info)
2812 +{
2813 +       unsigned int iobase;
2814 +       int size = 0, avail;
2815 +
2816 +       if (!info) {
2817 +               printk(KERN_WARNING "bt3c_cs: Call of receive for unknown device.\n");
2818 +               return;
2819 +       }
2820 +
2821 +       iobase = info->link.io.BasePort1;
2822 +
2823 +       avail = bt3c_read(iobase, 0x7006);
2824 +       //printk("bt3c_cs: receiving %d bytes\n", avail);
2825 +
2826 +       bt3c_address(iobase, 0x7480);
2827 +       while (size < avail) {
2828 +               size++;
2829 +               info->hdev.stat.byte_rx++;
2830 +
2831 +               /* Allocate packet */
2832 +               if (info->rx_skb == NULL) {
2833 +                       info->rx_state = RECV_WAIT_PACKET_TYPE;
2834 +                       info->rx_count = 0;
2835 +                       if (!(info->rx_skb = bluez_skb_alloc(HCI_MAX_FRAME_SIZE, GFP_ATOMIC))) {
2836 +                               printk(KERN_WARNING "bt3c_cs: Can't allocate mem for new packet.\n");
2837 +                               return;
2838 +                       }
2839 +               }
2840 +
2841 +
2842 +               if (info->rx_state == RECV_WAIT_PACKET_TYPE) {
2843 +
2844 +                       info->rx_skb->dev = (void *)&(info->hdev);
2845 +                       info->rx_skb->pkt_type = inb(iobase + DATA_L);
2846 +                       inb(iobase + DATA_H);
2847 +                       //printk("bt3c: PACKET_TYPE=%02x\n", info->rx_skb->pkt_type);
2848 +
2849 +                       switch (info->rx_skb->pkt_type) {
2850 +
2851 +                       case HCI_EVENT_PKT:
2852 +                               info->rx_state = RECV_WAIT_EVENT_HEADER;
2853 +                               info->rx_count = HCI_EVENT_HDR_SIZE;
2854 +                               break;
2855 +
2856 +                       case HCI_ACLDATA_PKT:
2857 +                               info->rx_state = RECV_WAIT_ACL_HEADER;
2858 +                               info->rx_count = HCI_ACL_HDR_SIZE;
2859 +                               break;
2860 +
2861 +                       case HCI_SCODATA_PKT:
2862 +                               info->rx_state = RECV_WAIT_SCO_HEADER;
2863 +                               info->rx_count = HCI_SCO_HDR_SIZE;
2864 +                               break;
2865 +
2866 +                       default:
2867 +                               /* Unknown packet */
2868 +                               printk(KERN_WARNING "bt3c_cs: Unknown HCI packet with type 0x%02x received.\n", info->rx_skb->pkt_type);
2869 +                               info->hdev.stat.err_rx++;
2870 +                               clear_bit(HCI_RUNNING, &(info->hdev.flags));
2871 +
2872 +                               kfree_skb(info->rx_skb);
2873 +                               info->rx_skb = NULL;
2874 +                               break;
2875 +
2876 +                       }
2877 +
2878 +               } else {
2879 +
2880 +                       __u8 x = inb(iobase + DATA_L);
2881 +
2882 +                       *skb_put(info->rx_skb, 1) = x;
2883 +                       inb(iobase + DATA_H);
2884 +                       info->rx_count--;
2885 +
2886 +                       if (info->rx_count == 0) {
2887 +
2888 +                               int dlen;
2889 +                               hci_event_hdr *eh;
2890 +                               hci_acl_hdr *ah;
2891 +                               hci_sco_hdr *sh;
2892 +
2893 +                               switch (info->rx_state) {
2894 +
2895 +                               case RECV_WAIT_EVENT_HEADER:
2896 +                                       eh = (hci_event_hdr *)(info->rx_skb->data);
2897 +                                       info->rx_state = RECV_WAIT_DATA;
2898 +                                       info->rx_count = eh->plen;
2899 +                                       break;
2900 +
2901 +                               case RECV_WAIT_ACL_HEADER:
2902 +                                       ah = (hci_acl_hdr *)(info->rx_skb->data);
2903 +                                       dlen = __le16_to_cpu(ah->dlen);
2904 +                                       info->rx_state = RECV_WAIT_DATA;
2905 +                                       info->rx_count = dlen;
2906 +                                       break;
2907 +
2908 +                               case RECV_WAIT_SCO_HEADER:
2909 +                                       sh = (hci_sco_hdr *)(info->rx_skb->data);
2910 +                                       info->rx_state = RECV_WAIT_DATA;
2911 +                                       info->rx_count = sh->dlen;
2912 +                                       break;
2913 +
2914 +                               case RECV_WAIT_DATA:
2915 +                                       hci_recv_frame(info->rx_skb);
2916 +                                       info->rx_skb = NULL;
2917 +                                       break;
2918 +
2919 +                               }
2920 +
2921 +                       }
2922 +
2923 +               }
2924 +
2925 +       }
2926 +
2927 +       bt3c_io_write(iobase, 0x7006, 0x0000);
2928 +}
2929 +
2930 +
2931 +void bt3c_interrupt(int irq, void *dev_inst, struct pt_regs *regs)
2932 +{
2933 +       bt3c_info_t *info = dev_inst;
2934 +       unsigned int iobase;
2935 +       int iir;
2936 +
2937 +       if (!info) {
2938 +               printk(KERN_WARNING "bt3c_cs: Call of irq %d for unknown device.\n", irq);
2939 +               return;
2940 +       }
2941 +
2942 +       iobase = info->link.io.BasePort1;
2943 +
2944 +       spin_lock(&(info->lock));
2945 +
2946 +       iir = inb(iobase + CONTROL);
2947 +       if (iir & 0x80) {
2948 +               int stat = bt3c_read(iobase, 0x7001);
2949 +
2950 +               if ((stat & 0xff) == 0x7f) {
2951 +                       printk(KERN_WARNING "bt3c_cs: STRANGE stat=%04x\n", stat);
2952 +               } else if ((stat & 0xff) != 0xff) {
2953 +                       if (stat & 0x0020) {
2954 +                               int stat = bt3c_read(iobase, 0x7002) & 0x10;
2955 +                               printk(KERN_WARNING "bt3c_cs: antena %s\n", stat ? "OUT" : "IN");
2956 +                       }
2957 +                       if (stat & 0x0001)
2958 +                               bt3c_receive(info);
2959 +                       if (stat & 0x0002) {
2960 +                               //printk("bt3c_cs: ACK %04x\n", stat);
2961 +                               clear_bit(XMIT_SENDING, &(info->tx_state));
2962 +                               bt3c_write_wakeup(info, 1);
2963 +                       }
2964 +
2965 +                       bt3c_io_write(iobase, 0x7001, 0x0000);
2966 +
2967 +                       outb(iir, iobase + CONTROL);
2968 +               }
2969 +       }
2970 +
2971 +       spin_unlock(&(info->lock));
2972 +}
2973 +
2974 +
2975 +
2976 +
2977 +/* ======================== HCI interface ======================== */
2978 +
2979 +
2980 +static int bt3c_hci_flush(struct hci_dev *hdev)
2981 +{
2982 +       bt3c_info_t *info = (bt3c_info_t *)(hdev->driver_data);
2983 +
2984 +       /* Drop TX queue */
2985 +       skb_queue_purge(&(info->txq));
2986 +
2987 +       return 0;
2988 +}
2989 +
2990 +
2991 +static int bt3c_hci_open(struct hci_dev *hdev)
2992 +{
2993 +       set_bit(HCI_RUNNING, &(hdev->flags));
2994 +
2995 +       return 0;
2996 +}
2997 +
2998 +
2999 +static int bt3c_hci_close(struct hci_dev *hdev)
3000 +{
3001 +       if (!test_and_clear_bit(HCI_RUNNING, &(hdev->flags)))
3002 +               return 0;
3003 +
3004 +       bt3c_hci_flush(hdev);
3005 +
3006 +       return 0;
3007 +}
3008 +
3009 +
3010 +static int bt3c_hci_send_frame(struct sk_buff *skb)
3011 +{
3012 +       bt3c_info_t *info;
3013 +       struct hci_dev *hdev = (struct hci_dev *)(skb->dev);
3014 +
3015 +       if (!hdev) {
3016 +               printk(KERN_WARNING "bt3c_cs: Frame for unknown HCI device (hdev=NULL).");
3017 +               return -ENODEV;
3018 +       }
3019 +
3020 +       info = (bt3c_info_t *) (hdev->driver_data);
3021 +
3022 +       switch (skb->pkt_type) {
3023 +       case HCI_COMMAND_PKT:
3024 +               hdev->stat.cmd_tx++;
3025 +               break;
3026 +       case HCI_ACLDATA_PKT:
3027 +               hdev->stat.acl_tx++;
3028 +               break;
3029 +       case HCI_SCODATA_PKT:
3030 +               hdev->stat.sco_tx++;
3031 +               break;
3032 +       };
3033 +
3034 +       /* Prepend skb with frame type */
3035 +       memcpy(skb_push(skb, 1), &(skb->pkt_type), 1);
3036 +       skb_queue_tail(&(info->txq), skb);
3037 +
3038 +       bt3c_write_wakeup(info, 0);
3039 +
3040 +       return 0;
3041 +}
3042 +
3043 +
3044 +static void bt3c_hci_destruct(struct hci_dev *hdev)
3045 +{
3046 +}
3047 +
3048 +
3049 +static int bt3c_hci_ioctl(struct hci_dev *hdev, unsigned int cmd, unsigned long arg)
3050 +{
3051 +       return -ENOIOCTLCMD;
3052 +}
3053 +
3054 +
3055 +
3056 +/* ======================== Card services HCI interaction ======================== */
3057 +
3058 +
3059 +static int bt3c_load_firmware(bt3c_info_t *info, unsigned char *firmware, int count)
3060 +{
3061 +       char *ptr = (char *) firmware;
3062 +       char b[9];
3063 +       unsigned int iobase, size, addr, fcs, tmp;
3064 +       int i, err = 0;
3065 +
3066 +       iobase = info->link.io.BasePort1;
3067 +
3068 +       /* Reset */
3069 +
3070 +       bt3c_io_write(iobase, 0x8040, 0x0404);
3071 +       bt3c_io_write(iobase, 0x8040, 0x0400);
3072 +
3073 +       udelay(1);
3074 +
3075 +       bt3c_io_write(iobase, 0x8040, 0x0404);
3076 +
3077 +       udelay(17);
3078 +
3079 +       /* Load */
3080 +
3081 +       while (count) {
3082 +               if (ptr[0] != 'S') {
3083 +                       printk(KERN_WARNING "bt3c_cs: Bad address in firmware.\n");
3084 +                       err = -EFAULT;
3085 +                       goto error;
3086 +               }
3087 +
3088 +               memset(b, 0, sizeof(b));
3089 +               memcpy(b, ptr + 2, 2);
3090 +               size = simple_strtol(b, NULL, 16);
3091 +
3092 +               memset(b, 0, sizeof(b));
3093 +               memcpy(b, ptr + 4, 8);
3094 +               addr = simple_strtol(b, NULL, 16);
3095 +
3096 +               memset(b, 0, sizeof(b));
3097 +               memcpy(b, ptr + (size * 2) + 2, 2);
3098 +               fcs = simple_strtol(b, NULL, 16);
3099 +
3100 +               memset(b, 0, sizeof(b));
3101 +               for (tmp = 0, i = 0; i < size; i++) {
3102 +                       memcpy(b, ptr + (i * 2) + 2, 2);
3103 +                       tmp += simple_strtol(b, NULL, 16);
3104 +               }
3105 +
3106 +               if (((tmp + fcs) & 0xff) != 0xff) {
3107 +                       printk(KERN_WARNING "bt3c_cs: Checksum error in firmware.\n");
3108 +                       err = -EILSEQ;
3109 +                       goto error;
3110 +               }
3111 +
3112 +               if (ptr[1] == '3') {
3113 +                       bt3c_address(iobase, addr);
3114 +
3115 +                       memset(b, 0, sizeof(b));
3116 +                       for (i = 0; i < (size - 4) / 2; i++) {
3117 +                               memcpy(b, ptr + (i * 4) + 12, 4);
3118 +                               tmp = simple_strtol(b, NULL, 16);
3119 +                               bt3c_put(iobase, tmp);
3120 +                       }
3121 +               }
3122 +
3123 +               ptr   += (size * 2) + 6;
3124 +               count -= (size * 2) + 6;
3125 +       }
3126 +
3127 +       udelay(17);
3128 +
3129 +       /* Boot */
3130 +
3131 +       bt3c_address(iobase, 0x3000);
3132 +       outb(inb(iobase + CONTROL) | 0x40, iobase + CONTROL);
3133 +
3134 +error:
3135 +       udelay(17);
3136 +
3137 +       /* Clear */
3138 +
3139 +       bt3c_io_write(iobase, 0x7006, 0x0000);
3140 +       bt3c_io_write(iobase, 0x7005, 0x0000);
3141 +       bt3c_io_write(iobase, 0x7001, 0x0000);
3142 +
3143 +       return err;
3144 +}
3145 +
3146 +
3147 +int bt3c_open(bt3c_info_t *info)
3148 +{
3149 +       const struct firmware *firmware;
3150 +       char device[16];
3151 +       struct hci_dev *hdev;
3152 +       int err;
3153 +
3154 +       spin_lock_init(&(info->lock));
3155 +
3156 +       skb_queue_head_init(&(info->txq));
3157 +
3158 +       info->rx_state = RECV_WAIT_PACKET_TYPE;
3159 +       info->rx_count = 0;
3160 +       info->rx_skb = NULL;
3161 +
3162 +       /* Load firmware */
3163 +
3164 +       snprintf(device, sizeof(device), "bt3c%4.4x", info->link.io.BasePort1);
3165 +
3166 +       err = request_firmware(&firmware, "BT3CPCC.bin", device);
3167 +       if (err < 0) {
3168 +               printk(KERN_WARNING "bt3c_cs: Firmware request failed.\n");
3169 +               return err;
3170 +       }
3171 +
3172 +       err = bt3c_load_firmware(info, firmware->data, firmware->size);
3173 +
3174 +       release_firmware(firmware);
3175 +
3176 +       if (err < 0) {
3177 +               printk(KERN_WARNING "bt3c_cs: Firmware loading failed.\n");
3178 +               return err;
3179 +       }
3180 +
3181 +       /* Timeout before it is safe to send the first HCI packet */
3182 +
3183 +       set_current_state(TASK_INTERRUPTIBLE);
3184 +       schedule_timeout(HZ);
3185 +
3186 +
3187 +       /* Initialize and register HCI device */
3188 +
3189 +       hdev = &(info->hdev);
3190 +
3191 +       hdev->type = HCI_PCCARD;
3192 +       hdev->driver_data = info;
3193 +
3194 +       hdev->open = bt3c_hci_open;
3195 +       hdev->close = bt3c_hci_close;
3196 +       hdev->flush = bt3c_hci_flush;
3197 +       hdev->send = bt3c_hci_send_frame;
3198 +       hdev->destruct = bt3c_hci_destruct;
3199 +       hdev->ioctl = bt3c_hci_ioctl;
3200 +
3201 +       if (hci_register_dev(hdev) < 0) {
3202 +               printk(KERN_WARNING "bt3c_cs: Can't register HCI device %s.\n", hdev->name);
3203 +               return -ENODEV;
3204 +       }
3205 +
3206 +       return 0;
3207 +}
3208 +
3209 +
3210 +int bt3c_close(bt3c_info_t *info)
3211 +{
3212 +       struct hci_dev *hdev = &(info->hdev);
3213 +
3214 +       if (info->link.state & DEV_CONFIG_PENDING)
3215 +               return -ENODEV;
3216 +
3217 +       bt3c_hci_close(hdev);
3218 +
3219 +       if (hci_unregister_dev(hdev) < 0)
3220 +               printk(KERN_WARNING "bt3c_cs: Can't unregister HCI device %s.\n", hdev->name);
3221 +
3222 +       return 0;
3223 +}
3224 +
3225 +
3226 +
3227 +/* ======================== Card services ======================== */
3228 +
3229 +
3230 +static void cs_error(client_handle_t handle, int func, int ret)
3231 +{
3232 +       error_info_t err = { func, ret };
3233 +
3234 +       CardServices(ReportError, handle, &err);
3235 +}
3236 +
3237 +
3238 +dev_link_t *bt3c_attach(void)
3239 +{
3240 +       bt3c_info_t *info;
3241 +       client_reg_t client_reg;
3242 +       dev_link_t *link;
3243 +       int i, ret;
3244 +
3245 +       /* Create new info device */
3246 +       info = kmalloc(sizeof(*info), GFP_KERNEL);
3247 +       if (!info)
3248 +               return NULL;
3249 +       memset(info, 0, sizeof(*info));
3250 +
3251 +       link = &info->link;
3252 +       link->priv = info;
3253 +
3254 +       link->release.function = &bt3c_release;
3255 +       link->release.data = (u_long)link;
3256 +       link->io.Attributes1 = IO_DATA_PATH_WIDTH_8;
3257 +       link->io.NumPorts1 = 8;
3258 +       link->irq.Attributes = IRQ_TYPE_EXCLUSIVE | IRQ_HANDLE_PRESENT;
3259 +       link->irq.IRQInfo1 = IRQ_INFO2_VALID | IRQ_LEVEL_ID;
3260 +
3261 +       if (irq_list[0] == -1)
3262 +               link->irq.IRQInfo2 = irq_mask;
3263 +       else
3264 +               for (i = 0; i < 4; i++)
3265 +                       link->irq.IRQInfo2 |= 1 << irq_list[i];
3266 +
3267 +       link->irq.Handler = bt3c_interrupt;
3268 +       link->irq.Instance = info;
3269 +
3270 +       link->conf.Attributes = CONF_ENABLE_IRQ;
3271 +       link->conf.Vcc = 50;
3272 +       link->conf.IntType = INT_MEMORY_AND_IO;
3273 +
3274 +       /* Register with Card Services */
3275 +       link->next = dev_list;
3276 +       dev_list = link;
3277 +       client_reg.dev_info = &dev_info;
3278 +       client_reg.Attributes = INFO_IO_CLIENT | INFO_CARD_SHARE;
3279 +       client_reg.EventMask =
3280 +           CS_EVENT_CARD_INSERTION | CS_EVENT_CARD_REMOVAL |
3281 +           CS_EVENT_RESET_PHYSICAL | CS_EVENT_CARD_RESET |
3282 +           CS_EVENT_PM_SUSPEND | CS_EVENT_PM_RESUME;
3283 +       client_reg.event_handler = &bt3c_event;
3284 +       client_reg.Version = 0x0210;
3285 +       client_reg.event_callback_args.client_data = link;
3286 +
3287 +       ret = CardServices(RegisterClient, &link->handle, &client_reg);
3288 +       if (ret != CS_SUCCESS) {
3289 +               cs_error(link->handle, RegisterClient, ret);
3290 +               bt3c_detach(link);
3291 +               return NULL;
3292 +       }
3293 +
3294 +       return link;
3295 +}
3296 +
3297 +
3298 +void bt3c_detach(dev_link_t *link)
3299 +{
3300 +       bt3c_info_t *info = link->priv;
3301 +       dev_link_t **linkp;
3302 +       int ret;
3303 +
3304 +       /* Locate device structure */
3305 +       for (linkp = &dev_list; *linkp; linkp = &(*linkp)->next)
3306 +               if (*linkp == link)
3307 +                       break;
3308 +
3309 +       if (*linkp == NULL)
3310 +               return;
3311 +
3312 +       del_timer(&link->release);
3313 +
3314 +       if (link->state & DEV_CONFIG)
3315 +               bt3c_release((u_long)link);
3316 +
3317 +       if (link->handle) {
3318 +               ret = CardServices(DeregisterClient, link->handle);
3319 +               if (ret != CS_SUCCESS)
3320 +                       cs_error(link->handle, DeregisterClient, ret);
3321 +       }
3322 +
3323 +       /* Unlink device structure, free bits */
3324 +       *linkp = link->next;
3325 +
3326 +       kfree(info);
3327 +}
3328 +
3329 +
3330 +static int get_tuple(int fn, client_handle_t handle, tuple_t *tuple, cisparse_t *parse)
3331 +{
3332 +       int i;
3333 +
3334 +       i = CardServices(fn, handle, tuple);
3335 +       if (i != CS_SUCCESS)
3336 +               return CS_NO_MORE_ITEMS;
3337 +
3338 +       i = CardServices(GetTupleData, handle, tuple);
3339 +       if (i != CS_SUCCESS)
3340 +               return i;
3341 +
3342 +       return CardServices(ParseTuple, handle, tuple, parse);
3343 +}
3344 +
3345 +
3346 +#define first_tuple(a, b, c) get_tuple(GetFirstTuple, a, b, c)
3347 +#define next_tuple(a, b, c) get_tuple(GetNextTuple, a, b, c)
3348 +
3349 +void bt3c_config(dev_link_t *link)
3350 +{
3351 +       static ioaddr_t base[5] = { 0x3f8, 0x2f8, 0x3e8, 0x2e8, 0x0 };
3352 +       client_handle_t handle = link->handle;
3353 +       bt3c_info_t *info = link->priv;
3354 +       tuple_t tuple;
3355 +       u_short buf[256];
3356 +       cisparse_t parse;
3357 +       cistpl_cftable_entry_t *cf = &parse.cftable_entry;
3358 +       config_info_t config;
3359 +       int i, j, try, last_ret, last_fn;
3360 +
3361 +       tuple.TupleData = (cisdata_t *)buf;
3362 +       tuple.TupleOffset = 0;
3363 +       tuple.TupleDataMax = 255;
3364 +       tuple.Attributes = 0;
3365 +
3366 +       /* Get configuration register information */
3367 +       tuple.DesiredTuple = CISTPL_CONFIG;
3368 +       last_ret = first_tuple(handle, &tuple, &parse);
3369 +       if (last_ret != CS_SUCCESS) {
3370 +               last_fn = ParseTuple;
3371 +               goto cs_failed;
3372 +       }
3373 +       link->conf.ConfigBase = parse.config.base;
3374 +       link->conf.Present = parse.config.rmask[0];
3375 +
3376 +       /* Configure card */
3377 +       link->state |= DEV_CONFIG;
3378 +       i = CardServices(GetConfigurationInfo, handle, &config);
3379 +       link->conf.Vcc = config.Vcc;
3380 +
3381 +       /* First pass: look for a config entry that looks normal. */
3382 +       tuple.TupleData = (cisdata_t *)buf;
3383 +       tuple.TupleOffset = 0;
3384 +       tuple.TupleDataMax = 255;
3385 +       tuple.Attributes = 0;
3386 +       tuple.DesiredTuple = CISTPL_CFTABLE_ENTRY;
3387 +       /* Two tries: without IO aliases, then with aliases */
3388 +       for (try = 0; try < 2; try++) {
3389 +               i = first_tuple(handle, &tuple, &parse);
3390 +               while (i != CS_NO_MORE_ITEMS) {
3391 +                       if (i != CS_SUCCESS)
3392 +                               goto next_entry;
3393 +                       if (cf->vpp1.present & (1 << CISTPL_POWER_VNOM))
3394 +                               link->conf.Vpp1 = link->conf.Vpp2 = cf->vpp1.param[CISTPL_POWER_VNOM] / 10000;
3395 +                       if ((cf->io.nwin > 0) && (cf->io.win[0].len == 8) && (cf->io.win[0].base != 0)) {
3396 +                               link->conf.ConfigIndex = cf->index;
3397 +                               link->io.BasePort1 = cf->io.win[0].base;
3398 +                               link->io.IOAddrLines = (try == 0) ? 16 : cf->io.flags & CISTPL_IO_LINES_MASK;
3399 +                               i = CardServices(RequestIO, link->handle, &link->io);
3400 +                               if (i == CS_SUCCESS)
3401 +                                       goto found_port;
3402 +                       }
3403 +next_entry:
3404 +                       i = next_tuple(handle, &tuple, &parse);
3405 +               }
3406 +       }
3407 +
3408 +       /* Second pass: try to find an entry that isn't picky about
3409 +          its base address, then try to grab any standard serial port
3410 +          address, and finally try to get any free port. */
3411 +       i = first_tuple(handle, &tuple, &parse);
3412 +       while (i != CS_NO_MORE_ITEMS) {
3413 +               if ((i == CS_SUCCESS) && (cf->io.nwin > 0) && ((cf->io.flags & CISTPL_IO_LINES_MASK) <= 3)) {
3414 +                       link->conf.ConfigIndex = cf->index;
3415 +                       for (j = 0; j < 5; j++) {
3416 +                               link->io.BasePort1 = base[j];
3417 +                               link->io.IOAddrLines = base[j] ? 16 : 3;
3418 +                               i = CardServices(RequestIO, link->handle, &link->io);
3419 +                               if (i == CS_SUCCESS)
3420 +                                       goto found_port;
3421 +                       }
3422 +               }
3423 +               i = next_tuple(handle, &tuple, &parse);
3424 +       }
3425 +
3426 +found_port:
3427 +       if (i != CS_SUCCESS) {
3428 +               printk(KERN_NOTICE "bt3c_cs: No usable port range found. Giving up.\n");
3429 +               cs_error(link->handle, RequestIO, i);
3430 +               goto failed;
3431 +       }
3432 +
3433 +       i = CardServices(RequestIRQ, link->handle, &link->irq);
3434 +       if (i != CS_SUCCESS) {
3435 +               cs_error(link->handle, RequestIRQ, i);
3436 +               link->irq.AssignedIRQ = 0;
3437 +       }
3438 +
3439 +       i = CardServices(RequestConfiguration, link->handle, &link->conf);
3440 +       if (i != CS_SUCCESS) {
3441 +               cs_error(link->handle, RequestConfiguration, i);
3442 +               goto failed;
3443 +       }
3444 +
3445 +       MOD_INC_USE_COUNT;
3446 +
3447 +       if (bt3c_open(info) != 0)
3448 +               goto failed;
3449 +
3450 +       strcpy(info->node.dev_name, info->hdev.name);
3451 +       link->dev = &info->node;
3452 +       link->state &= ~DEV_CONFIG_PENDING;
3453 +
3454 +       return;
3455 +
3456 +cs_failed:
3457 +       cs_error(link->handle, last_fn, last_ret);
3458 +
3459 +failed:
3460 +       bt3c_release((u_long)link);
3461 +}
3462 +
3463 +
3464 +void bt3c_release(u_long arg)
3465 +{
3466 +       dev_link_t *link = (dev_link_t *)arg;
3467 +       bt3c_info_t *info = link->priv;
3468 +
3469 +       if (link->state & DEV_PRESENT)
3470 +               bt3c_close(info);
3471 +
3472 +       MOD_DEC_USE_COUNT;
3473 +
3474 +       link->dev = NULL;
3475 +
3476 +       CardServices(ReleaseConfiguration, link->handle);
3477 +       CardServices(ReleaseIO, link->handle, &link->io);
3478 +       CardServices(ReleaseIRQ, link->handle, &link->irq);
3479 +
3480 +       link->state &= ~DEV_CONFIG;
3481 +}
3482 +
3483 +
3484 +int bt3c_event(event_t event, int priority, event_callback_args_t *args)
3485 +{
3486 +       dev_link_t *link = args->client_data;
3487 +       bt3c_info_t *info = link->priv;
3488 +
3489 +       switch (event) {
3490 +       case CS_EVENT_CARD_REMOVAL:
3491 +               link->state &= ~DEV_PRESENT;
3492 +               if (link->state & DEV_CONFIG) {
3493 +                       bt3c_close(info);
3494 +                       mod_timer(&link->release, jiffies + HZ / 20);
3495 +               }
3496 +               break;
3497 +       case CS_EVENT_CARD_INSERTION:
3498 +               link->state |= DEV_PRESENT | DEV_CONFIG_PENDING;
3499 +               bt3c_config(link);
3500 +               break;
3501 +       case CS_EVENT_PM_SUSPEND:
3502 +               link->state |= DEV_SUSPEND;
3503 +               /* Fall through... */
3504 +       case CS_EVENT_RESET_PHYSICAL:
3505 +               if (link->state & DEV_CONFIG)
3506 +                       CardServices(ReleaseConfiguration, link->handle);
3507 +               break;
3508 +       case CS_EVENT_PM_RESUME:
3509 +               link->state &= ~DEV_SUSPEND;
3510 +               /* Fall through... */
3511 +       case CS_EVENT_CARD_RESET:
3512 +               if (DEV_OK(link))
3513 +                       CardServices(RequestConfiguration, link->handle, &link->conf);
3514 +               break;
3515 +       }
3516 +
3517 +       return 0;
3518 +}
3519 +
3520 +
3521 +
3522 +/* ======================== Module initialization ======================== */
3523 +
3524 +
3525 +int __init init_bt3c_cs(void)
3526 +{
3527 +       servinfo_t serv;
3528 +       int err;
3529 +
3530 +       CardServices(GetCardServicesInfo, &serv);
3531 +       if (serv.Revision != CS_RELEASE_CODE) {
3532 +               printk(KERN_NOTICE "bt3c_cs: Card Services release does not match!\n");
3533 +               return -1;
3534 +       }
3535 +
3536 +       err = register_pccard_driver(&dev_info, &bt3c_attach, &bt3c_detach);
3537 +
3538 +       return err;
3539 +}
3540 +
3541 +
3542 +void __exit exit_bt3c_cs(void)
3543 +{
3544 +       unregister_pccard_driver(&dev_info);
3545 +
3546 +       while (dev_list != NULL)
3547 +               bt3c_detach(dev_list);
3548 +}
3549 +
3550 +
3551 +module_init(init_bt3c_cs);
3552 +module_exit(exit_bt3c_cs);
3553 +
3554 +EXPORT_NO_SYMBOLS;
3555 diff -urN linux-2.4.18/drivers/bluetooth/btuart_cs.c linux-2.4.18-mh15/drivers/bluetooth/btuart_cs.c
3556 --- linux-2.4.18/drivers/bluetooth/btuart_cs.c  1970-01-01 01:00:00.000000000 +0100
3557 +++ linux-2.4.18-mh15/drivers/bluetooth/btuart_cs.c     2004-08-01 16:26:23.000000000 +0200
3558 @@ -0,0 +1,909 @@
3559 +/*
3560 + *
3561 + *  Driver for Bluetooth PCMCIA cards with HCI UART interface
3562 + *
3563 + *  Copyright (C) 2001-2002  Marcel Holtmann <marcel@holtmann.org>
3564 + *
3565 + *
3566 + *  This program is free software; you can redistribute it and/or modify
3567 + *  it under the terms of the GNU General Public License version 2 as
3568 + *  published by the Free Software Foundation;
3569 + *
3570 + *  Software distributed under the License is distributed on an "AS
3571 + *  IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
3572 + *  implied. See the License for the specific language governing
3573 + *  rights and limitations under the License.
3574 + *
3575 + *  The initial developer of the original code is David A. Hinds
3576 + *  <dahinds@users.sourceforge.net>.  Portions created by David A. Hinds
3577 + *  are Copyright (C) 1999 David A. Hinds.  All Rights Reserved.
3578 + *
3579 + */
3580 +
3581 +#include <linux/config.h>
3582 +#include <linux/module.h>
3583 +
3584 +#include <linux/kernel.h>
3585 +#include <linux/init.h>
3586 +#include <linux/slab.h>
3587 +#include <linux/types.h>
3588 +#include <linux/sched.h>
3589 +#include <linux/timer.h>
3590 +#include <linux/errno.h>
3591 +#include <linux/ptrace.h>
3592 +#include <linux/ioport.h>
3593 +#include <linux/spinlock.h>
3594 +
3595 +#include <linux/skbuff.h>
3596 +#include <linux/string.h>
3597 +#include <linux/serial.h>
3598 +#include <linux/serial_reg.h>
3599 +#include <asm/system.h>
3600 +#include <asm/bitops.h>
3601 +#include <asm/io.h>
3602 +
3603 +#include <pcmcia/version.h>
3604 +#include <pcmcia/cs_types.h>
3605 +#include <pcmcia/cs.h>
3606 +#include <pcmcia/cistpl.h>
3607 +#include <pcmcia/ciscode.h>
3608 +#include <pcmcia/ds.h>
3609 +#include <pcmcia/cisreg.h>
3610 +
3611 +#include <net/bluetooth/bluetooth.h>
3612 +#include <net/bluetooth/hci_core.h>
3613 +
3614 +
3615 +
3616 +/* ======================== Module parameters ======================== */
3617 +
3618 +
3619 +/* Bit map of interrupts to choose from */
3620 +static u_int irq_mask = 0xffff;
3621 +static int irq_list[4] = { -1 };
3622 +
3623 +MODULE_PARM(irq_mask, "i");
3624 +MODULE_PARM(irq_list, "1-4i");
3625 +
3626 +MODULE_AUTHOR("Marcel Holtmann <marcel@holtmann.org>");
3627 +MODULE_DESCRIPTION("BlueZ driver for Bluetooth PCMCIA cards with HCI UART interface");
3628 +MODULE_LICENSE("GPL");
3629 +
3630 +
3631 +
3632 +/* ======================== Local structures ======================== */
3633 +
3634 +
3635 +typedef struct btuart_info_t {
3636 +       dev_link_t link;
3637 +       dev_node_t node;
3638 +
3639 +       struct hci_dev hdev;
3640 +
3641 +       spinlock_t lock;        /* For serializing operations */
3642 +
3643 +       struct sk_buff_head txq;
3644 +       unsigned long tx_state;
3645 +
3646 +       unsigned long rx_state;
3647 +       unsigned long rx_count;
3648 +       struct sk_buff *rx_skb;
3649 +} btuart_info_t;
3650 +
3651 +
3652 +void btuart_config(dev_link_t *link);
3653 +void btuart_release(u_long arg);
3654 +int btuart_event(event_t event, int priority, event_callback_args_t *args);
3655 +
3656 +static dev_info_t dev_info = "btuart_cs";
3657 +
3658 +dev_link_t *btuart_attach(void);
3659 +void btuart_detach(dev_link_t *);
3660 +
3661 +static dev_link_t *dev_list = NULL;
3662 +
3663 +
3664 +/* Maximum baud rate */
3665 +#define SPEED_MAX  115200
3666 +
3667 +/* Default baud rate: 57600, 115200, 230400 or 460800 */
3668 +#define DEFAULT_BAUD_RATE  115200
3669 +
3670 +
3671 +/* Transmit states  */
3672 +#define XMIT_SENDING   1
3673 +#define XMIT_WAKEUP    2
3674 +#define XMIT_WAITING   8
3675 +
3676 +/* Receiver states */
3677 +#define RECV_WAIT_PACKET_TYPE  0
3678 +#define RECV_WAIT_EVENT_HEADER 1
3679 +#define RECV_WAIT_ACL_HEADER   2
3680 +#define RECV_WAIT_SCO_HEADER   3
3681 +#define RECV_WAIT_DATA         4
3682 +
3683 +
3684 +
3685 +/* ======================== Interrupt handling ======================== */
3686 +
3687 +
3688 +static int btuart_write(unsigned int iobase, int fifo_size, __u8 *buf, int len)
3689 +{
3690 +       int actual = 0;
3691 +
3692 +       /* Tx FIFO should be empty */
3693 +       if (!(inb(iobase + UART_LSR) & UART_LSR_THRE))
3694 +               return 0;
3695 +
3696 +       /* Fill FIFO with current frame */
3697 +       while ((fifo_size-- > 0) && (actual < len)) {
3698 +               /* Transmit next byte */
3699 +               outb(buf[actual], iobase + UART_TX);
3700 +               actual++;
3701 +       }
3702 +
3703 +       return actual;
3704 +}
3705 +
3706 +
3707 +static void btuart_write_wakeup(btuart_info_t *info)
3708 +{
3709 +       if (!info) {
3710 +               printk(KERN_WARNING "btuart_cs: Call of write_wakeup for unknown device.\n");
3711 +               return;
3712 +       }
3713 +
3714 +       if (test_and_set_bit(XMIT_SENDING, &(info->tx_state))) {
3715 +               set_bit(XMIT_WAKEUP, &(info->tx_state));
3716 +               return;
3717 +       }
3718 +
3719 +       do {
3720 +               register unsigned int iobase = info->link.io.BasePort1;
3721 +               register struct sk_buff *skb;
3722 +               register int len;
3723 +
3724 +               clear_bit(XMIT_WAKEUP, &(info->tx_state));
3725 +
3726 +               if (!(info->link.state & DEV_PRESENT))
3727 +                       return;
3728 +
3729 +               if (!(skb = skb_dequeue(&(info->txq))))
3730 +                       break;
3731 +
3732 +               /* Send frame */
3733 +               len = btuart_write(iobase, 16, skb->data, skb->len);
3734 +               set_bit(XMIT_WAKEUP, &(info->tx_state));
3735 +
3736 +               if (len == skb->len) {
3737 +                       kfree_skb(skb);
3738 +               } else {
3739 +                       skb_pull(skb, len);
3740 +                       skb_queue_head(&(info->txq), skb);
3741 +               }
3742 +
3743 +               info->hdev.stat.byte_tx += len;
3744 +
3745 +       } while (test_bit(XMIT_WAKEUP, &(info->tx_state)));
3746 +
3747 +       clear_bit(XMIT_SENDING, &(info->tx_state));
3748 +}
3749 +
3750 +
3751 +static void btuart_receive(btuart_info_t *info)
3752 +{
3753 +       unsigned int iobase;
3754 +       int boguscount = 0;
3755 +
3756 +       if (!info) {
3757 +               printk(KERN_WARNING "btuart_cs: Call of receive for unknown device.\n");
3758 +               return;
3759 +       }
3760 +
3761 +       iobase = info->link.io.BasePort1;
3762 +
3763 +       do {
3764 +               info->hdev.stat.byte_rx++;
3765 +
3766 +               /* Allocate packet */
3767 +               if (info->rx_skb == NULL) {
3768 +                       info->rx_state = RECV_WAIT_PACKET_TYPE;
3769 +                       info->rx_count = 0;
3770 +                       if (!(info->rx_skb = bluez_skb_alloc(HCI_MAX_FRAME_SIZE, GFP_ATOMIC))) {
3771 +                               printk(KERN_WARNING "btuart_cs: Can't allocate mem for new packet.\n");
3772 +                               return;
3773 +                       }
3774 +               }
3775 +
3776 +               if (info->rx_state == RECV_WAIT_PACKET_TYPE) {
3777 +
3778 +                       info->rx_skb->dev = (void *)&(info->hdev);
3779 +                       info->rx_skb->pkt_type = inb(iobase + UART_RX);
3780 +
3781 +                       switch (info->rx_skb->pkt_type) {
3782 +
3783 +                       case HCI_EVENT_PKT:
3784 +                               info->rx_state = RECV_WAIT_EVENT_HEADER;
3785 +                               info->rx_count = HCI_EVENT_HDR_SIZE;
3786 +                               break;
3787 +
3788 +                       case HCI_ACLDATA_PKT:
3789 +                               info->rx_state = RECV_WAIT_ACL_HEADER;
3790 +                               info->rx_count = HCI_ACL_HDR_SIZE;
3791 +                               break;
3792 +
3793 +                       case HCI_SCODATA_PKT:
3794 +                               info->rx_state = RECV_WAIT_SCO_HEADER;
3795 +                               info->rx_count = HCI_SCO_HDR_SIZE;
3796 +                               break;
3797 +
3798 +                       default:
3799 +                               /* Unknown packet */
3800 +                               printk(KERN_WARNING "btuart_cs: Unknown HCI packet with type 0x%02x received.\n", info->rx_skb->pkt_type);
3801 +                               info->hdev.stat.err_rx++;
3802 +                               clear_bit(HCI_RUNNING, &(info->hdev.flags));
3803 +
3804 +                               kfree_skb(info->rx_skb);
3805 +                               info->rx_skb = NULL;
3806 +                               break;
3807 +
3808 +                       }
3809 +
3810 +               } else {
3811 +
3812 +                       *skb_put(info->rx_skb, 1) = inb(iobase + UART_RX);
3813 +                       info->rx_count--;
3814 +
3815 +                       if (info->rx_count == 0) {
3816 +
3817 +                               int dlen;
3818 +                               hci_event_hdr *eh;
3819 +                               hci_acl_hdr *ah;
3820 +                               hci_sco_hdr *sh;
3821 +
3822 +
3823 +                               switch (info->rx_state) {
3824 +
3825 +                               case RECV_WAIT_EVENT_HEADER:
3826 +                                       eh = (hci_event_hdr *)(info->rx_skb->data);
3827 +                                       info->rx_state = RECV_WAIT_DATA;
3828 +                                       info->rx_count = eh->plen;
3829 +                                       break;
3830 +
3831 +                               case RECV_WAIT_ACL_HEADER:
3832 +                                       ah = (hci_acl_hdr *)(info->rx_skb->data);
3833 +                                       dlen = __le16_to_cpu(ah->dlen);
3834 +                                       info->rx_state = RECV_WAIT_DATA;
3835 +                                       info->rx_count = dlen;
3836 +                                       break;
3837 +
3838 +                               case RECV_WAIT_SCO_HEADER:
3839 +                                       sh = (hci_sco_hdr *)(info->rx_skb->data);
3840 +                                       info->rx_state = RECV_WAIT_DATA;
3841 +                                       info->rx_count = sh->dlen;
3842 +                                       break;
3843 +
3844 +                               case RECV_WAIT_DATA:
3845 +                                       hci_recv_frame(info->rx_skb);
3846 +                                       info->rx_skb = NULL;
3847 +                                       break;
3848 +
3849 +                               }
3850 +
3851 +                       }
3852 +
3853 +               }
3854 +
3855 +               /* Make sure we don't stay here to long */
3856 +               if (boguscount++ > 16)
3857 +                       break;
3858 +
3859 +       } while (inb(iobase + UART_LSR) & UART_LSR_DR);
3860 +}
3861 +
3862 +
3863 +void btuart_interrupt(int irq, void *dev_inst, struct pt_regs *regs)
3864 +{
3865 +       btuart_info_t *info = dev_inst;
3866 +       unsigned int iobase;
3867 +       int boguscount = 0;
3868 +       int iir, lsr;
3869 +
3870 +       if (!info) {
3871 +               printk(KERN_WARNING "btuart_cs: Call of irq %d for unknown device.\n", irq);
3872 +               return;
3873 +       }
3874 +
3875 +       iobase = info->link.io.BasePort1;
3876 +
3877 +       spin_lock(&(info->lock));
3878 +
3879 +       iir = inb(iobase + UART_IIR) & UART_IIR_ID;
3880 +       while (iir) {
3881 +
3882 +               /* Clear interrupt */
3883 +               lsr = inb(iobase + UART_LSR);
3884 +
3885 +               switch (iir) {
3886 +               case UART_IIR_RLSI:
3887 +                       printk(KERN_NOTICE "btuart_cs: RLSI\n");
3888 +                       break;
3889 +               case UART_IIR_RDI:
3890 +                       /* Receive interrupt */
3891 +                       btuart_receive(info);
3892 +                       break;
3893 +               case UART_IIR_THRI:
3894 +                       if (lsr & UART_LSR_THRE) {
3895 +                               /* Transmitter ready for data */
3896 +                               btuart_write_wakeup(info);
3897 +                       }
3898 +                       break;
3899 +               default:
3900 +                       printk(KERN_NOTICE "btuart_cs: Unhandled IIR=%#x\n", iir);
3901 +                       break;
3902 +               }
3903 +
3904 +               /* Make sure we don't stay here to long */
3905 +               if (boguscount++ > 100)
3906 +                       break;
3907 +
3908 +               iir = inb(iobase + UART_IIR) & UART_IIR_ID;
3909 +
3910 +       }
3911 +
3912 +       spin_unlock(&(info->lock));
3913 +}
3914 +
3915 +
3916 +static void btuart_change_speed(btuart_info_t *info, unsigned int speed)
3917 +{
3918 +       unsigned long flags;
3919 +       unsigned int iobase;
3920 +       int fcr;                /* FIFO control reg */
3921 +       int lcr;                /* Line control reg */
3922 +       int divisor;
3923 +
3924 +       if (!info) {
3925 +               printk(KERN_WARNING "btuart_cs: Call of change speed for unknown device.\n");
3926 +               return;
3927 +       }
3928 +
3929 +       iobase = info->link.io.BasePort1;
3930 +
3931 +       spin_lock_irqsave(&(info->lock), flags);
3932 +
3933 +       /* Turn off interrupts */
3934 +       outb(0, iobase + UART_IER);
3935 +
3936 +       divisor = SPEED_MAX / speed;
3937 +
3938 +       fcr = UART_FCR_ENABLE_FIFO | UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT;
3939 +
3940 +       /* 
3941 +        * Use trigger level 1 to avoid 3 ms. timeout delay at 9600 bps, and
3942 +        * almost 1,7 ms at 19200 bps. At speeds above that we can just forget
3943 +        * about this timeout since it will always be fast enough. 
3944 +        */
3945 +
3946 +       if (speed < 38400)
3947 +               fcr |= UART_FCR_TRIGGER_1;
3948 +       else
3949 +               fcr |= UART_FCR_TRIGGER_14;
3950 +
3951 +       /* Bluetooth cards use 8N1 */
3952 +       lcr = UART_LCR_WLEN8;
3953 +
3954 +       outb(UART_LCR_DLAB | lcr, iobase + UART_LCR);   /* Set DLAB */
3955 +       outb(divisor & 0xff, iobase + UART_DLL);        /* Set speed */
3956 +       outb(divisor >> 8, iobase + UART_DLM);
3957 +       outb(lcr, iobase + UART_LCR);   /* Set 8N1  */
3958 +       outb(fcr, iobase + UART_FCR);   /* Enable FIFO's */
3959 +
3960 +       /* Turn on interrups */
3961 +       outb(UART_IER_RLSI | UART_IER_RDI | UART_IER_THRI, iobase + UART_IER);
3962 +
3963 +       spin_unlock_irqrestore(&(info->lock), flags);
3964 +}
3965 +
3966 +
3967 +
3968 +/* ======================== HCI interface ======================== */
3969 +
3970 +
3971 +static int btuart_hci_flush(struct hci_dev *hdev)
3972 +{
3973 +       btuart_info_t *info = (btuart_info_t *)(hdev->driver_data);
3974 +
3975 +       /* Drop TX queue */
3976 +       skb_queue_purge(&(info->txq));
3977 +
3978 +       return 0;
3979 +}
3980 +
3981 +
3982 +static int btuart_hci_open(struct hci_dev *hdev)
3983 +{
3984 +       set_bit(HCI_RUNNING, &(hdev->flags));
3985 +
3986 +       return 0;
3987 +}
3988 +
3989 +
3990 +static int btuart_hci_close(struct hci_dev *hdev)
3991 +{
3992 +       if (!test_and_clear_bit(HCI_RUNNING, &(hdev->flags)))
3993 +               return 0;
3994 +
3995 +       btuart_hci_flush(hdev);
3996 +
3997 +       return 0;
3998 +}
3999 +
4000 +
4001 +static int btuart_hci_send_frame(struct sk_buff *skb)
4002 +{
4003 +       btuart_info_t *info;
4004 +       struct hci_dev *hdev = (struct hci_dev *)(skb->dev);
4005 +
4006 +       if (!hdev) {
4007 +               printk(KERN_WARNING "btuart_cs: Frame for unknown HCI device (hdev=NULL).");
4008 +               return -ENODEV;
4009 +       }
4010 +
4011 +       info = (btuart_info_t *)(hdev->driver_data);
4012 +
4013 +       switch (skb->pkt_type) {
4014 +       case HCI_COMMAND_PKT:
4015 +               hdev->stat.cmd_tx++;
4016 +               break;
4017 +       case HCI_ACLDATA_PKT:
4018 +               hdev->stat.acl_tx++;
4019 +               break;
4020 +       case HCI_SCODATA_PKT:
4021 +               hdev->stat.sco_tx++;
4022 +               break;
4023 +       };
4024 +
4025 +       /* Prepend skb with frame type */
4026 +       memcpy(skb_push(skb, 1), &(skb->pkt_type), 1);
4027 +       skb_queue_tail(&(info->txq), skb);
4028 +
4029 +       btuart_write_wakeup(info);
4030 +
4031 +       return 0;
4032 +}
4033 +
4034 +
4035 +static void btuart_hci_destruct(struct hci_dev *hdev)
4036 +{
4037 +}
4038 +
4039 +
4040 +static int btuart_hci_ioctl(struct hci_dev *hdev, unsigned int cmd, unsigned long arg)
4041 +{
4042 +       return -ENOIOCTLCMD;
4043 +}
4044 +
4045 +
4046 +
4047 +/* ======================== Card services HCI interaction ======================== */
4048 +
4049 +
4050 +int btuart_open(btuart_info_t *info)
4051 +{
4052 +       unsigned long flags;
4053 +       unsigned int iobase = info->link.io.BasePort1;
4054 +       struct hci_dev *hdev;
4055 +
4056 +       spin_lock_init(&(info->lock));
4057 +
4058 +       skb_queue_head_init(&(info->txq));
4059 +
4060 +       info->rx_state = RECV_WAIT_PACKET_TYPE;
4061 +       info->rx_count = 0;
4062 +       info->rx_skb = NULL;
4063 +
4064 +       spin_lock_irqsave(&(info->lock), flags);
4065 +
4066 +       /* Reset UART */
4067 +       outb(0, iobase + UART_MCR);
4068 +
4069 +       /* Turn off interrupts */
4070 +       outb(0, iobase + UART_IER);
4071 +
4072 +       /* Initialize UART */
4073 +       outb(UART_LCR_WLEN8, iobase + UART_LCR);        /* Reset DLAB */
4074 +       outb((UART_MCR_DTR | UART_MCR_RTS | UART_MCR_OUT2), iobase + UART_MCR);
4075 +
4076 +       /* Turn on interrupts */
4077 +       // outb(UART_IER_RLSI | UART_IER_RDI | UART_IER_THRI, iobase + UART_IER);
4078 +
4079 +       spin_unlock_irqrestore(&(info->lock), flags);
4080 +
4081 +       btuart_change_speed(info, DEFAULT_BAUD_RATE);
4082 +
4083 +       /* Timeout before it is safe to send the first HCI packet */
4084 +       set_current_state(TASK_INTERRUPTIBLE);
4085 +       schedule_timeout(HZ);
4086 +
4087 +
4088 +       /* Initialize and register HCI device */
4089 +
4090 +       hdev = &(info->hdev);
4091 +
4092 +       hdev->type = HCI_PCCARD;
4093 +       hdev->driver_data = info;
4094 +
4095 +       hdev->open = btuart_hci_open;
4096 +       hdev->close = btuart_hci_close;
4097 +       hdev->flush = btuart_hci_flush;
4098 +       hdev->send = btuart_hci_send_frame;
4099 +       hdev->destruct = btuart_hci_destruct;
4100 +       hdev->ioctl = btuart_hci_ioctl;
4101 +
4102 +       if (hci_register_dev(hdev) < 0) {
4103 +               printk(KERN_WARNING "btuart_cs: Can't register HCI device %s.\n", hdev->name);
4104 +               return -ENODEV;
4105 +       }
4106 +
4107 +       return 0;
4108 +}
4109 +
4110 +
4111 +int btuart_close(btuart_info_t *info)
4112 +{
4113 +       unsigned long flags;
4114 +       unsigned int iobase = info->link.io.BasePort1;
4115 +       struct hci_dev *hdev = &(info->hdev);
4116 +
4117 +       if (info->link.state & DEV_CONFIG_PENDING)
4118 +               return -ENODEV;
4119 +
4120 +       btuart_hci_close(hdev);
4121 +
4122 +       spin_lock_irqsave(&(info->lock), flags);
4123 +
4124 +       /* Reset UART */
4125 +       outb(0, iobase + UART_MCR);
4126 +
4127 +       /* Turn off interrupts */
4128 +       outb(0, iobase + UART_IER);
4129 +
4130 +       spin_unlock_irqrestore(&(info->lock), flags);
4131 +
4132 +       if (hci_unregister_dev(hdev) < 0)
4133 +               printk(KERN_WARNING "btuart_cs: Can't unregister HCI device %s.\n", hdev->name);
4134 +
4135 +       return 0;
4136 +}
4137 +
4138 +
4139 +
4140 +/* ======================== Card services ======================== */
4141 +
4142 +
4143 +static void cs_error(client_handle_t handle, int func, int ret)
4144 +{
4145 +       error_info_t err = { func, ret };
4146 +
4147 +       CardServices(ReportError, handle, &err);
4148 +}
4149 +
4150 +
4151 +dev_link_t *btuart_attach(void)
4152 +{
4153 +       btuart_info_t *info;
4154 +       client_reg_t client_reg;
4155 +       dev_link_t *link;
4156 +       int i, ret;
4157 +
4158 +       /* Create new info device */
4159 +       info = kmalloc(sizeof(*info), GFP_KERNEL);
4160 +       if (!info)
4161 +               return NULL;
4162 +       memset(info, 0, sizeof(*info));
4163 +
4164 +       link = &info->link;
4165 +       link->priv = info;
4166 +
4167 +       link->release.function = &btuart_release;
4168 +       link->release.data = (u_long)link;
4169 +       link->io.Attributes1 = IO_DATA_PATH_WIDTH_8;
4170 +       link->io.NumPorts1 = 8;
4171 +       link->irq.Attributes = IRQ_TYPE_EXCLUSIVE | IRQ_HANDLE_PRESENT;
4172 +       link->irq.IRQInfo1 = IRQ_INFO2_VALID | IRQ_LEVEL_ID;
4173 +
4174 +       if (irq_list[0] == -1)
4175 +               link->irq.IRQInfo2 = irq_mask;
4176 +       else
4177 +               for (i = 0; i < 4; i++)
4178 +                       link->irq.IRQInfo2 |= 1 << irq_list[i];
4179 +
4180 +       link->irq.Handler = btuart_interrupt;
4181 +       link->irq.Instance = info;
4182 +
4183 +       link->conf.Attributes = CONF_ENABLE_IRQ;
4184 +       link->conf.Vcc = 50;
4185 +       link->conf.IntType = INT_MEMORY_AND_IO;
4186 +
4187 +       /* Register with Card Services */
4188 +       link->next = dev_list;
4189 +       dev_list = link;
4190 +       client_reg.dev_info = &dev_info;
4191 +       client_reg.Attributes = INFO_IO_CLIENT | INFO_CARD_SHARE;
4192 +       client_reg.EventMask =
4193 +               CS_EVENT_CARD_INSERTION | CS_EVENT_CARD_REMOVAL |
4194 +               CS_EVENT_RESET_PHYSICAL | CS_EVENT_CARD_RESET |
4195 +               CS_EVENT_PM_SUSPEND | CS_EVENT_PM_RESUME;
4196 +       client_reg.event_handler = &btuart_event;
4197 +       client_reg.Version = 0x0210;
4198 +       client_reg.event_callback_args.client_data = link;
4199 +
4200 +       ret = CardServices(RegisterClient, &link->handle, &client_reg);
4201 +       if (ret != CS_SUCCESS) {
4202 +               cs_error(link->handle, RegisterClient, ret);
4203 +               btuart_detach(link);
4204 +               return NULL;
4205 +       }
4206 +
4207 +       return link;
4208 +}
4209 +
4210 +
4211 +void btuart_detach(dev_link_t *link)
4212 +{
4213 +       btuart_info_t *info = link->priv;
4214 +       dev_link_t **linkp;
4215 +       int ret;
4216 +
4217 +       /* Locate device structure */
4218 +       for (linkp = &dev_list; *linkp; linkp = &(*linkp)->next)
4219 +               if (*linkp == link)
4220 +                       break;
4221 +
4222 +       if (*linkp == NULL)
4223 +               return;
4224 +
4225 +       del_timer(&link->release);
4226 +       if (link->state & DEV_CONFIG)
4227 +               btuart_release((u_long)link);
4228 +
4229 +       if (link->handle) {
4230 +               ret = CardServices(DeregisterClient, link->handle);
4231 +               if (ret != CS_SUCCESS)
4232 +                       cs_error(link->handle, DeregisterClient, ret);
4233 +       }
4234 +
4235 +       /* Unlink device structure, free bits */
4236 +       *linkp = link->next;
4237 +
4238 +       kfree(info);
4239 +}
4240 +
4241 +
4242 +static int get_tuple(int fn, client_handle_t handle, tuple_t *tuple, cisparse_t *parse)
4243 +{
4244 +       int i;
4245 +
4246 +       i = CardServices(fn, handle, tuple);
4247 +       if (i != CS_SUCCESS)
4248 +               return CS_NO_MORE_ITEMS;
4249 +
4250 +       i = CardServices(GetTupleData, handle, tuple);
4251 +       if (i != CS_SUCCESS)
4252 +               return i;
4253 +
4254 +       return CardServices(ParseTuple, handle, tuple, parse);
4255 +}
4256 +
4257 +
4258 +#define first_tuple(a, b, c) get_tuple(GetFirstTuple, a, b, c)
4259 +#define next_tuple(a, b, c) get_tuple(GetNextTuple, a, b, c)
4260 +
4261 +void btuart_config(dev_link_t *link)
4262 +{
4263 +       static ioaddr_t base[5] = { 0x3f8, 0x2f8, 0x3e8, 0x2e8, 0x0 };
4264 +       client_handle_t handle = link->handle;
4265 +       btuart_info_t *info = link->priv;
4266 +       tuple_t tuple;
4267 +       u_short buf[256];
4268 +       cisparse_t parse;
4269 +       cistpl_cftable_entry_t *cf = &parse.cftable_entry;
4270 +       config_info_t config;
4271 +       int i, j, try, last_ret, last_fn;
4272 +
4273 +       tuple.TupleData = (cisdata_t *)buf;
4274 +       tuple.TupleOffset = 0;
4275 +       tuple.TupleDataMax = 255;
4276 +       tuple.Attributes = 0;
4277 +
4278 +       /* Get configuration register information */
4279 +       tuple.DesiredTuple = CISTPL_CONFIG;
4280 +       last_ret = first_tuple(handle, &tuple, &parse);
4281 +       if (last_ret != CS_SUCCESS) {
4282 +               last_fn = ParseTuple;
4283 +               goto cs_failed;
4284 +       }
4285 +       link->conf.ConfigBase = parse.config.base;
4286 +       link->conf.Present = parse.config.rmask[0];
4287 +
4288 +       /* Configure card */
4289 +       link->state |= DEV_CONFIG;
4290 +       i = CardServices(GetConfigurationInfo, handle, &config);
4291 +       link->conf.Vcc = config.Vcc;
4292 +
4293 +       /* First pass: look for a config entry that looks normal. */
4294 +       tuple.TupleData = (cisdata_t *) buf;
4295 +       tuple.TupleOffset = 0;
4296 +       tuple.TupleDataMax = 255;
4297 +       tuple.Attributes = 0;
4298 +       tuple.DesiredTuple = CISTPL_CFTABLE_ENTRY;
4299 +       /* Two tries: without IO aliases, then with aliases */
4300 +       for (try = 0; try < 2; try++) {
4301 +               i = first_tuple(handle, &tuple, &parse);
4302 +               while (i != CS_NO_MORE_ITEMS) {
4303 +                       if (i != CS_SUCCESS)
4304 +                               goto next_entry;
4305 +                       if (cf->vpp1.present & (1 << CISTPL_POWER_VNOM))
4306 +                               link->conf.Vpp1 = link->conf.Vpp2 = cf->vpp1.param[CISTPL_POWER_VNOM] / 10000;
4307 +                       if ((cf->io.nwin > 0) && (cf->io.win[0].len == 8) && (cf->io.win[0].base != 0)) {
4308 +                               link->conf.ConfigIndex = cf->index;
4309 +                               link->io.BasePort1 = cf->io.win[0].base;
4310 +                               link->io.IOAddrLines = (try == 0) ? 16 : cf->io.flags & CISTPL_IO_LINES_MASK;
4311 +                               i = CardServices(RequestIO, link->handle, &link->io);
4312 +                               if (i == CS_SUCCESS)
4313 +                                       goto found_port;
4314 +                       }
4315 +next_entry:
4316 +                       i = next_tuple(handle, &tuple, &parse);
4317 +               }
4318 +       }
4319 +
4320 +       /* Second pass: try to find an entry that isn't picky about
4321 +          its base address, then try to grab any standard serial port
4322 +          address, and finally try to get any free port. */
4323 +       i = first_tuple(handle, &tuple, &parse);
4324 +       while (i != CS_NO_MORE_ITEMS) {
4325 +               if ((i == CS_SUCCESS) && (cf->io.nwin > 0)
4326 +                   && ((cf->io.flags & CISTPL_IO_LINES_MASK) <= 3)) {
4327 +                       link->conf.ConfigIndex = cf->index;
4328 +                       for (j = 0; j < 5; j++) {
4329 +                               link->io.BasePort1 = base[j];
4330 +                               link->io.IOAddrLines = base[j] ? 16 : 3;
4331 +                               i = CardServices(RequestIO, link->handle, &link->io);
4332 +                               if (i == CS_SUCCESS)
4333 +                                       goto found_port;
4334 +                       }
4335 +               }
4336 +               i = next_tuple(handle, &tuple, &parse);
4337 +       }
4338 +
4339 +found_port:
4340 +       if (i != CS_SUCCESS) {
4341 +               printk(KERN_NOTICE "btuart_cs: No usable port range found. Giving up.\n");
4342 +               cs_error(link->handle, RequestIO, i);
4343 +               goto failed;
4344 +       }
4345 +
4346 +       i = CardServices(RequestIRQ, link->handle, &link->irq);
4347 +       if (i != CS_SUCCESS) {
4348 +               cs_error(link->handle, RequestIRQ, i);
4349 +               link->irq.AssignedIRQ = 0;
4350 +       }
4351 +
4352 +       i = CardServices(RequestConfiguration, link->handle, &link->conf);
4353 +       if (i != CS_SUCCESS) {
4354 +               cs_error(link->handle, RequestConfiguration, i);
4355 +               goto failed;
4356 +       }
4357 +
4358 +       MOD_INC_USE_COUNT;
4359 +
4360 +       if (btuart_open(info) != 0)
4361 +               goto failed;
4362 +
4363 +       strcpy(info->node.dev_name, info->hdev.name);
4364 +       link->dev = &info->node;
4365 +       link->state &= ~DEV_CONFIG_PENDING;
4366 +
4367 +       return;
4368 +
4369 +cs_failed:
4370 +       cs_error(link->handle, last_fn, last_ret);
4371 +
4372 +failed:
4373 +       btuart_release((u_long) link);
4374 +}
4375 +
4376 +
4377 +void btuart_release(u_long arg)
4378 +{
4379 +       dev_link_t *link = (dev_link_t *)arg;
4380 +       btuart_info_t *info = link->priv;
4381 +
4382 +       if (link->state & DEV_PRESENT)
4383 +               btuart_close(info);
4384 +
4385 +       MOD_DEC_USE_COUNT;
4386 +
4387 +       link->dev = NULL;
4388 +
4389 +       CardServices(ReleaseConfiguration, link->handle);
4390 +       CardServices(ReleaseIO, link->handle, &link->io);
4391 +       CardServices(ReleaseIRQ, link->handle, &link->irq);
4392 +
4393 +       link->state &= ~DEV_CONFIG;
4394 +}
4395 +
4396 +
4397 +int btuart_event(event_t event, int priority, event_callback_args_t *args)
4398 +{
4399 +       dev_link_t *link = args->client_data;
4400 +       btuart_info_t *info = link->priv;
4401 +
4402 +       switch (event) {
4403 +       case CS_EVENT_CARD_REMOVAL:
4404 +               link->state &= ~DEV_PRESENT;
4405 +               if (link->state & DEV_CONFIG) {
4406 +                       btuart_close(info);
4407 +                       mod_timer(&link->release, jiffies + HZ / 20);
4408 +               }
4409 +               break;
4410 +       case CS_EVENT_CARD_INSERTION:
4411 +               link->state |= DEV_PRESENT | DEV_CONFIG_PENDING;
4412 +               btuart_config(link);
4413 +               break;
4414 +       case CS_EVENT_PM_SUSPEND:
4415 +               link->state |= DEV_SUSPEND;
4416 +               /* Fall through... */
4417 +       case CS_EVENT_RESET_PHYSICAL:
4418 +               if (link->state & DEV_CONFIG)
4419 +                       CardServices(ReleaseConfiguration, link->handle);
4420 +               break;
4421 +       case CS_EVENT_PM_RESUME:
4422 +               link->state &= ~DEV_SUSPEND;
4423 +               /* Fall through... */
4424 +       case CS_EVENT_CARD_RESET:
4425 +               if (DEV_OK(link))
4426 +                       CardServices(RequestConfiguration, link->handle, &link->conf);
4427 +               break;
4428 +       }
4429 +
4430 +       return 0;
4431 +}
4432 +
4433 +
4434 +
4435 +/* ======================== Module initialization ======================== */
4436 +
4437 +
4438 +int __init init_btuart_cs(void)
4439 +{
4440 +       servinfo_t serv;
4441 +       int err;
4442 +
4443 +       CardServices(GetCardServicesInfo, &serv);
4444 +       if (serv.Revision != CS_RELEASE_CODE) {
4445 +               printk(KERN_NOTICE "btuart_cs: Card Services release does not match!\n");
4446 +               return -1;
4447 +       }
4448 +
4449 +       err = register_pccard_driver(&dev_info, &btuart_attach, &btuart_detach);
4450 +
4451 +       return err;
4452 +}
4453 +
4454 +
4455 +void __exit exit_btuart_cs(void)
4456 +{
4457 +       unregister_pccard_driver(&dev_info);
4458 +
4459 +       while (dev_list != NULL)
4460 +               btuart_detach(dev_list);
4461 +}
4462 +
4463 +
4464 +module_init(init_btuart_cs);
4465 +module_exit(exit_btuart_cs);
4466 +
4467 +EXPORT_NO_SYMBOLS;
4468 diff -urN linux-2.4.18/drivers/bluetooth/Config.in linux-2.4.18-mh15/drivers/bluetooth/Config.in
4469 --- linux-2.4.18/drivers/bluetooth/Config.in    2001-09-07 18:28:38.000000000 +0200
4470 +++ linux-2.4.18-mh15/drivers/bluetooth/Config.in       2004-08-01 16:26:23.000000000 +0200
4471 @@ -1,8 +1,33 @@
4472 +#
4473 +# Bluetooth HCI device drivers configuration
4474 +#
4475 +
4476  mainmenu_option next_comment
4477  comment 'Bluetooth device drivers'
4478  
4479  dep_tristate 'HCI USB driver' CONFIG_BLUEZ_HCIUSB $CONFIG_BLUEZ $CONFIG_USB
4480 +if [ "$CONFIG_BLUEZ_HCIUSB" != "n" ]; then
4481 +   bool '  SCO (voice) support'  CONFIG_BLUEZ_HCIUSB_SCO
4482 +fi
4483 +
4484  dep_tristate 'HCI UART driver' CONFIG_BLUEZ_HCIUART $CONFIG_BLUEZ
4485 -dep_tristate 'HCI VHCI virtual HCI device driver' CONFIG_BLUEZ_HCIVHCI $CONFIG_BLUEZ
4486 +if [ "$CONFIG_BLUEZ_HCIUART" != "n" ]; then
4487 +   bool '  UART (H4) protocol support' CONFIG_BLUEZ_HCIUART_H4
4488 +   bool '  BCSP protocol support' CONFIG_BLUEZ_HCIUART_BCSP
4489 +   dep_bool '  Transmit CRC with every BCSP packet' CONFIG_BLUEZ_HCIUART_BCSP_TXCRC $CONFIG_BLUEZ_HCIUART_BCSP
4490 +fi
4491 +
4492 +dep_tristate 'HCI BlueFRITZ! USB driver' CONFIG_BLUEZ_HCIBFUSB $CONFIG_BLUEZ $CONFIG_USB
4493 +
4494 +dep_tristate 'HCI DTL1 (PC Card) driver' CONFIG_BLUEZ_HCIDTL1 $CONFIG_PCMCIA $CONFIG_BLUEZ
4495 +
4496 +dep_tristate 'HCI BT3C (PC Card) driver' CONFIG_BLUEZ_HCIBT3C $CONFIG_PCMCIA $CONFIG_BLUEZ
4497 +
4498 +dep_tristate 'HCI BlueCard (PC Card) driver' CONFIG_BLUEZ_HCIBLUECARD $CONFIG_PCMCIA $CONFIG_BLUEZ
4499 +
4500 +dep_tristate 'HCI UART (PC Card) driver' CONFIG_BLUEZ_HCIBTUART $CONFIG_PCMCIA $CONFIG_BLUEZ
4501 +
4502 +dep_tristate 'HCI VHCI (Virtual HCI device) driver' CONFIG_BLUEZ_HCIVHCI $CONFIG_BLUEZ
4503  
4504  endmenu
4505 +
4506 diff -urN linux-2.4.18/drivers/bluetooth/dtl1_cs.c linux-2.4.18-mh15/drivers/bluetooth/dtl1_cs.c
4507 --- linux-2.4.18/drivers/bluetooth/dtl1_cs.c    1970-01-01 01:00:00.000000000 +0100
4508 +++ linux-2.4.18-mh15/drivers/bluetooth/dtl1_cs.c       2004-08-01 16:26:23.000000000 +0200
4509 @@ -0,0 +1,861 @@
4510 +/*
4511 + *
4512 + *  A driver for Nokia Connectivity Card DTL-1 devices
4513 + *
4514 + *  Copyright (C) 2001-2002  Marcel Holtmann <marcel@holtmann.org>
4515 + *
4516 + *
4517 + *  This program is free software; you can redistribute it and/or modify
4518 + *  it under the terms of the GNU General Public License version 2 as
4519 + *  published by the Free Software Foundation;
4520 + *
4521 + *  Software distributed under the License is distributed on an "AS
4522 + *  IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
4523 + *  implied. See the License for the specific language governing
4524 + *  rights and limitations under the License.
4525 + *
4526 + *  The initial developer of the original code is David A. Hinds
4527 + *  <dahinds@users.sourceforge.net>.  Portions created by David A. Hinds
4528 + *  are Copyright (C) 1999 David A. Hinds.  All Rights Reserved.
4529 + *
4530 + */
4531 +
4532 +#include <linux/config.h>
4533 +#include <linux/module.h>
4534 +
4535 +#include <linux/kernel.h>
4536 +#include <linux/init.h>
4537 +#include <linux/slab.h>
4538 +#include <linux/types.h>
4539 +#include <linux/sched.h>
4540 +#include <linux/timer.h>
4541 +#include <linux/errno.h>
4542 +#include <linux/ptrace.h>
4543 +#include <linux/ioport.h>
4544 +#include <linux/spinlock.h>
4545 +
4546 +#include <linux/skbuff.h>
4547 +#include <linux/string.h>
4548 +#include <linux/serial.h>
4549 +#include <linux/serial_reg.h>
4550 +#include <asm/system.h>
4551 +#include <asm/bitops.h>
4552 +#include <asm/io.h>
4553 +
4554 +#include <pcmcia/version.h>
4555 +#include <pcmcia/cs_types.h>
4556 +#include <pcmcia/cs.h>
4557 +#include <pcmcia/cistpl.h>
4558 +#include <pcmcia/ciscode.h>
4559 +#include <pcmcia/ds.h>
4560 +#include <pcmcia/cisreg.h>
4561 +
4562 +#include <net/bluetooth/bluetooth.h>
4563 +#include <net/bluetooth/hci_core.h>
4564 +
4565 +
4566 +
4567 +/* ======================== Module parameters ======================== */
4568 +
4569 +
4570 +/* Bit map of interrupts to choose from */
4571 +static u_int irq_mask = 0xffff;
4572 +static int irq_list[4] = { -1 };
4573 +
4574 +MODULE_PARM(irq_mask, "i");
4575 +MODULE_PARM(irq_list, "1-4i");
4576 +
4577 +MODULE_AUTHOR("Marcel Holtmann <marcel@holtmann.org>");
4578 +MODULE_DESCRIPTION("BlueZ driver for Nokia Connectivity Card DTL-1");
4579 +MODULE_LICENSE("GPL");
4580 +
4581 +
4582 +
4583 +/* ======================== Local structures ======================== */
4584 +
4585 +
4586 +typedef struct dtl1_info_t {
4587 +       dev_link_t link;
4588 +       dev_node_t node;
4589 +
4590 +       struct hci_dev hdev;
4591 +
4592 +       spinlock_t lock;                /* For serializing operations */
4593 +
4594 +       unsigned long flowmask;         /* HCI flow mask */
4595 +       int ri_latch;
4596 +
4597 +       struct sk_buff_head txq;
4598 +       unsigned long tx_state;
4599 +
4600 +       unsigned long rx_state;
4601 +       unsigned long rx_count;
4602 +       struct sk_buff *rx_skb;
4603 +} dtl1_info_t;
4604 +
4605 +
4606 +void dtl1_config(dev_link_t *link);
4607 +void dtl1_release(u_long arg);
4608 +int dtl1_event(event_t event, int priority, event_callback_args_t *args);
4609 +
4610 +static dev_info_t dev_info = "dtl1_cs";
4611 +
4612 +dev_link_t *dtl1_attach(void);
4613 +void dtl1_detach(dev_link_t *);
4614 +
4615 +static dev_link_t *dev_list = NULL;
4616 +
4617 +
4618 +/* Transmit states  */
4619 +#define XMIT_SENDING  1
4620 +#define XMIT_WAKEUP   2
4621 +#define XMIT_WAITING  8
4622 +
4623 +/* Receiver States */
4624 +#define RECV_WAIT_NSH   0
4625 +#define RECV_WAIT_DATA  1
4626 +
4627 +
4628 +typedef struct {
4629 +       u8 type;
4630 +       u8 zero;
4631 +       u16 len;
4632 +} __attribute__ ((packed)) nsh_t;      /* Nokia Specific Header */
4633 +
4634 +#define NSHL  4                                /* Nokia Specific Header Length */
4635 +
4636 +
4637 +
4638 +/* ======================== Interrupt handling ======================== */
4639 +
4640 +
4641 +static int dtl1_write(unsigned int iobase, int fifo_size, __u8 *buf, int len)
4642 +{
4643 +       int actual = 0;
4644 +
4645 +       /* Tx FIFO should be empty */
4646 +       if (!(inb(iobase + UART_LSR) & UART_LSR_THRE))
4647 +               return 0;
4648 +
4649 +       /* Fill FIFO with current frame */
4650 +       while ((fifo_size-- > 0) && (actual < len)) {
4651 +               /* Transmit next byte */
4652 +               outb(buf[actual], iobase + UART_TX);
4653 +               actual++;
4654 +       }
4655 +
4656 +       return actual;
4657 +}
4658 +
4659 +
4660 +static void dtl1_write_wakeup(dtl1_info_t *info)
4661 +{
4662 +       if (!info) {
4663 +               printk(KERN_WARNING "dtl1_cs: Call of write_wakeup for unknown device.\n");
4664 +               return;
4665 +       }
4666 +
4667 +       if (test_bit(XMIT_WAITING, &(info->tx_state))) {
4668 +               set_bit(XMIT_WAKEUP, &(info->tx_state));
4669 +               return;
4670 +       }
4671 +
4672 +       if (test_and_set_bit(XMIT_SENDING, &(info->tx_state))) {
4673 +               set_bit(XMIT_WAKEUP, &(info->tx_state));
4674 +               return;
4675 +       }
4676 +
4677 +       do {
4678 +               register unsigned int iobase = info->link.io.BasePort1;
4679 +               register struct sk_buff *skb;
4680 +               register int len;
4681 +
4682 +               clear_bit(XMIT_WAKEUP, &(info->tx_state));
4683 +
4684 +               if (!(info->link.state & DEV_PRESENT))
4685 +                       return;
4686 +
4687 +               if (!(skb = skb_dequeue(&(info->txq))))
4688 +                       break;
4689 +
4690 +               /* Send frame */
4691 +               len = dtl1_write(iobase, 32, skb->data, skb->len);
4692 +
4693 +               if (len == skb->len) {
4694 +                       set_bit(XMIT_WAITING, &(info->tx_state));
4695 +                       kfree_skb(skb);
4696 +               } else {
4697 +                       skb_pull(skb, len);
4698 +                       skb_queue_head(&(info->txq), skb);
4699 +               }
4700 +
4701 +               info->hdev.stat.byte_tx += len;
4702 +
4703 +       } while (test_bit(XMIT_WAKEUP, &(info->tx_state)));
4704 +
4705 +       clear_bit(XMIT_SENDING, &(info->tx_state));
4706 +}
4707 +
4708 +
4709 +static void dtl1_control(dtl1_info_t *info, struct sk_buff *skb)
4710 +{
4711 +       u8 flowmask = *(u8 *)skb->data;
4712 +       int i;
4713 +
4714 +       printk(KERN_INFO "dtl1_cs: Nokia control data = ");
4715 +       for (i = 0; i < skb->len; i++) {
4716 +               printk("%02x ", skb->data[i]);
4717 +       }
4718 +       printk("\n");
4719 +
4720 +       /* transition to active state */
4721 +       if (((info->flowmask & 0x07) == 0) && ((flowmask & 0x07) != 0)) {
4722 +               clear_bit(XMIT_WAITING, &(info->tx_state));
4723 +               dtl1_write_wakeup(info);
4724 +       }
4725 +
4726 +       info->flowmask = flowmask;
4727 +
4728 +       kfree_skb(skb);
4729 +}
4730 +
4731 +
4732 +static void dtl1_receive(dtl1_info_t *info)
4733 +{
4734 +       unsigned int iobase;
4735 +       nsh_t *nsh;
4736 +       int boguscount = 0;
4737 +
4738 +       if (!info) {
4739 +               printk(KERN_WARNING "dtl1_cs: Call of receive for unknown device.\n");
4740 +               return;
4741 +       }
4742 +
4743 +       iobase = info->link.io.BasePort1;
4744 +
4745 +       do {
4746 +               info->hdev.stat.byte_rx++;
4747 +
4748 +               /* Allocate packet */
4749 +               if (info->rx_skb == NULL)
4750 +                       if (!(info->rx_skb = bluez_skb_alloc(HCI_MAX_FRAME_SIZE, GFP_ATOMIC))) {
4751 +                               printk(KERN_WARNING "dtl1_cs: Can't allocate mem for new packet.\n");
4752 +                               info->rx_state = RECV_WAIT_NSH;
4753 +                               info->rx_count = NSHL;
4754 +                               return;
4755 +                       }
4756 +
4757 +               *skb_put(info->rx_skb, 1) = inb(iobase + UART_RX);
4758 +               nsh = (nsh_t *)info->rx_skb->data;
4759 +
4760 +               info->rx_count--;
4761 +
4762 +               if (info->rx_count == 0) {
4763 +
4764 +                       switch (info->rx_state) {
4765 +                       case RECV_WAIT_NSH:
4766 +                               info->rx_state = RECV_WAIT_DATA;
4767 +                               info->rx_count = nsh->len + (nsh->len & 0x0001);
4768 +                               break;
4769 +                       case RECV_WAIT_DATA:
4770 +                               info->rx_skb->pkt_type = nsh->type;
4771 +
4772 +                               /* remove PAD byte if it exists */
4773 +                               if (nsh->len & 0x0001) {
4774 +                                       info->rx_skb->tail--;
4775 +                                       info->rx_skb->len--;
4776 +                               }
4777 +
4778 +                               /* remove NSH */
4779 +                               skb_pull(info->rx_skb, NSHL);
4780 +
4781 +                               switch (info->rx_skb->pkt_type) {
4782 +                               case 0x80:
4783 +                                       /* control data for the Nokia Card */
4784 +                                       dtl1_control(info, info->rx_skb);
4785 +                                       break;
4786 +                               case 0x82:
4787 +                               case 0x83:
4788 +                               case 0x84:
4789 +                                       /* send frame to the HCI layer */
4790 +                                       info->rx_skb->dev = (void *)&(info->hdev);
4791 +                                       info->rx_skb->pkt_type &= 0x0f;
4792 +                                       hci_recv_frame(info->rx_skb);
4793 +                                       break;
4794 +                               default:
4795 +                                       /* unknown packet */
4796 +                                       printk(KERN_WARNING "dtl1_cs: Unknown HCI packet with type 0x%02x received.\n", info->rx_skb->pkt_type);
4797 +                                       kfree_skb(info->rx_skb);
4798 +                                       break;
4799 +                               }
4800 +
4801 +                               info->rx_state = RECV_WAIT_NSH;
4802 +                               info->rx_count = NSHL;
4803 +                               info->rx_skb = NULL;
4804 +                               break;
4805 +                       }
4806 +
4807 +               }
4808 +
4809 +               /* Make sure we don't stay here to long */
4810 +               if (boguscount++ > 32)
4811 +                       break;
4812 +
4813 +       } while (inb(iobase + UART_LSR) & UART_LSR_DR);
4814 +}
4815 +
4816 +
4817 +void dtl1_interrupt(int irq, void *dev_inst, struct pt_regs *regs)
4818 +{
4819 +       dtl1_info_t *info = dev_inst;
4820 +       unsigned int iobase;
4821 +       unsigned char msr;
4822 +       int boguscount = 0;
4823 +       int iir, lsr;
4824 +
4825 +       if (!info) {
4826 +               printk(KERN_WARNING "dtl1_cs: Call of irq %d for unknown device.\n", irq);
4827 +               return;
4828 +       }
4829 +
4830 +       iobase = info->link.io.BasePort1;
4831 +
4832 +       spin_lock(&(info->lock));
4833 +
4834 +       iir = inb(iobase + UART_IIR) & UART_IIR_ID;
4835 +       while (iir) {
4836 +
4837 +               /* Clear interrupt */
4838 +               lsr = inb(iobase + UART_LSR);
4839 +
4840 +               switch (iir) {
4841 +               case UART_IIR_RLSI:
4842 +                       printk(KERN_NOTICE "dtl1_cs: RLSI\n");
4843 +                       break;
4844 +               case UART_IIR_RDI:
4845 +                       /* Receive interrupt */
4846 +                       dtl1_receive(info);
4847 +                       break;
4848 +               case UART_IIR_THRI:
4849 +                       if (lsr & UART_LSR_THRE) {
4850 +                               /* Transmitter ready for data */
4851 +                               dtl1_write_wakeup(info);
4852 +                       }
4853 +                       break;
4854 +               default:
4855 +                       printk(KERN_NOTICE "dtl1_cs: Unhandled IIR=%#x\n", iir);
4856 +                       break;
4857 +               }
4858 +
4859 +               /* Make sure we don't stay here to long */
4860 +               if (boguscount++ > 100)
4861 +                       break;
4862 +
4863 +               iir = inb(iobase + UART_IIR) & UART_IIR_ID;
4864 +
4865 +       }
4866 +
4867 +       msr = inb(iobase + UART_MSR);
4868 +
4869 +       if (info->ri_latch ^ (msr & UART_MSR_RI)) {
4870 +               info->ri_latch = msr & UART_MSR_RI;
4871 +               clear_bit(XMIT_WAITING, &(info->tx_state));
4872 +               dtl1_write_wakeup(info);
4873 +       }
4874 +
4875 +       spin_unlock(&(info->lock));
4876 +}
4877 +
4878 +
4879 +
4880 +/* ======================== HCI interface ======================== */
4881 +
4882 +
4883 +static int dtl1_hci_open(struct hci_dev *hdev)
4884 +{
4885 +       set_bit(HCI_RUNNING, &(hdev->flags));
4886 +
4887 +       return 0;
4888 +}
4889 +
4890 +
4891 +static int dtl1_hci_flush(struct hci_dev *hdev)
4892 +{
4893 +       dtl1_info_t *info = (dtl1_info_t *)(hdev->driver_data);
4894 +
4895 +       /* Drop TX queue */
4896 +       skb_queue_purge(&(info->txq));
4897 +
4898 +       return 0;
4899 +}
4900 +
4901 +
4902 +static int dtl1_hci_close(struct hci_dev *hdev)
4903 +{
4904 +       if (!test_and_clear_bit(HCI_RUNNING, &(hdev->flags)))
4905 +               return 0;
4906 +
4907 +       dtl1_hci_flush(hdev);
4908 +
4909 +       return 0;
4910 +}
4911 +
4912 +
4913 +static int dtl1_hci_send_frame(struct sk_buff *skb)
4914 +{
4915 +       dtl1_info_t *info;
4916 +       struct hci_dev *hdev = (struct hci_dev *)(skb->dev);
4917 +       struct sk_buff *s;
4918 +       nsh_t nsh;
4919 +
4920 +       if (!hdev) {
4921 +               printk(KERN_WARNING "dtl1_cs: Frame for unknown HCI device (hdev=NULL).");
4922 +               return -ENODEV;
4923 +       }
4924 +
4925 +       info = (dtl1_info_t *)(hdev->driver_data);
4926 +
4927 +       switch (skb->pkt_type) {
4928 +       case HCI_COMMAND_PKT:
4929 +               hdev->stat.cmd_tx++;
4930 +               nsh.type = 0x81;
4931 +               break;
4932 +       case HCI_ACLDATA_PKT:
4933 +               hdev->stat.acl_tx++;
4934 +               nsh.type = 0x82;
4935 +               break;
4936 +       case HCI_SCODATA_PKT:
4937 +               hdev->stat.sco_tx++;
4938 +               nsh.type = 0x83;
4939 +               break;
4940 +       };
4941 +
4942 +       nsh.zero = 0;
4943 +       nsh.len = skb->len;
4944 +
4945 +       s = bluez_skb_alloc(NSHL + skb->len + 1, GFP_ATOMIC);
4946 +       skb_reserve(s, NSHL);
4947 +       memcpy(skb_put(s, skb->len), skb->data, skb->len);
4948 +       if (skb->len & 0x0001)
4949 +               *skb_put(s, 1) = 0;     /* PAD */
4950 +
4951 +       /* Prepend skb with Nokia frame header and queue */
4952 +       memcpy(skb_push(s, NSHL), &nsh, NSHL);
4953 +       skb_queue_tail(&(info->txq), s);
4954 +
4955 +       dtl1_write_wakeup(info);
4956 +
4957 +       kfree_skb(skb);
4958 +
4959 +       return 0;
4960 +}
4961 +
4962 +
4963 +static void dtl1_hci_destruct(struct hci_dev *hdev)
4964 +{
4965 +}
4966 +
4967 +
4968 +static int dtl1_hci_ioctl(struct hci_dev *hdev, unsigned int cmd,  unsigned long arg)
4969 +{
4970 +       return -ENOIOCTLCMD;
4971 +}
4972 +
4973 +
4974 +
4975 +/* ======================== Card services HCI interaction ======================== */
4976 +
4977 +
4978 +int dtl1_open(dtl1_info_t *info)
4979 +{
4980 +       unsigned long flags;
4981 +       unsigned int iobase = info->link.io.BasePort1;
4982 +       struct hci_dev *hdev;
4983 +
4984 +       spin_lock_init(&(info->lock));
4985 +
4986 +       skb_queue_head_init(&(info->txq));
4987 +
4988 +       info->rx_state = RECV_WAIT_NSH;
4989 +       info->rx_count = NSHL;
4990 +       info->rx_skb = NULL;
4991 +
4992 +       set_bit(XMIT_WAITING, &(info->tx_state));
4993 +
4994 +       spin_lock_irqsave(&(info->lock), flags);
4995 +
4996 +       /* Reset UART */
4997 +       outb(0, iobase + UART_MCR);
4998 +
4999 +       /* Turn off interrupts */
5000 +       outb(0, iobase + UART_IER);
5001 +
5002 +       /* Initialize UART */
5003 +       outb(UART_LCR_WLEN8, iobase + UART_LCR);        /* Reset DLAB */
5004 +       outb((UART_MCR_DTR | UART_MCR_RTS | UART_MCR_OUT2), iobase + UART_MCR);
5005 +
5006 +       info->ri_latch = inb(info->link.io.BasePort1 + UART_MSR) & UART_MSR_RI;
5007 +
5008 +       /* Turn on interrupts */
5009 +       outb(UART_IER_RLSI | UART_IER_RDI | UART_IER_THRI, iobase + UART_IER);
5010 +
5011 +       spin_unlock_irqrestore(&(info->lock), flags);
5012 +
5013 +       /* Timeout before it is safe to send the first HCI packet */
5014 +       set_current_state(TASK_INTERRUPTIBLE);
5015 +       schedule_timeout(HZ * 2);
5016 +
5017 +
5018 +       /* Initialize and register HCI device */
5019 +
5020 +       hdev = &(info->hdev);
5021 +
5022 +       hdev->type = HCI_PCCARD;
5023 +       hdev->driver_data = info;
5024 +
5025 +       hdev->open = dtl1_hci_open;
5026 +       hdev->close = dtl1_hci_close;
5027 +       hdev->flush = dtl1_hci_flush;
5028 +       hdev->send = dtl1_hci_send_frame;
5029 +       hdev->destruct = dtl1_hci_destruct;
5030 +       hdev->ioctl = dtl1_hci_ioctl;
5031 +
5032 +       if (hci_register_dev(hdev) < 0) {
5033 +               printk(KERN_WARNING "dtl1_cs: Can't register HCI device %s.\n", hdev->name);
5034 +               return -ENODEV;
5035 +       }
5036 +
5037 +       return 0;
5038 +}
5039 +
5040 +
5041 +int dtl1_close(dtl1_info_t *info)
5042 +{
5043 +       unsigned long flags;
5044 +       unsigned int iobase = info->link.io.BasePort1;
5045 +       struct hci_dev *hdev = &(info->hdev);
5046 +
5047 +       if (info->link.state & DEV_CONFIG_PENDING)
5048 +               return -ENODEV;
5049 +
5050 +       dtl1_hci_close(hdev);
5051 +
5052 +       spin_lock_irqsave(&(info->lock), flags);
5053 +
5054 +       /* Reset UART */
5055 +       outb(0, iobase + UART_MCR);
5056 +
5057 +       /* Turn off interrupts */
5058 +       outb(0, iobase + UART_IER);
5059 +
5060 +       spin_unlock_irqrestore(&(info->lock), flags);
5061 +
5062 +       if (hci_unregister_dev(hdev) < 0)
5063 +               printk(KERN_WARNING "dtl1_cs: Can't unregister HCI device %s.\n", hdev->name);
5064 +
5065 +       return 0;
5066 +}
5067 +
5068 +
5069 +
5070 +/* ======================== Card services ======================== */
5071 +
5072 +
5073 +static void cs_error(client_handle_t handle, int func, int ret)
5074 +{
5075 +       error_info_t err = { func, ret };
5076 +
5077 +       CardServices(ReportError, handle, &err);
5078 +}
5079 +
5080 +
5081 +dev_link_t *dtl1_attach(void)
5082 +{
5083 +       dtl1_info_t *info;
5084 +       client_reg_t client_reg;
5085 +       dev_link_t *link;
5086 +       int i, ret;
5087 +
5088 +       /* Create new info device */
5089 +       info = kmalloc(sizeof(*info), GFP_KERNEL);
5090 +       if (!info)
5091 +               return NULL;
5092 +       memset(info, 0, sizeof(*info));
5093 +
5094 +       link = &info->link;
5095 +       link->priv = info;
5096 +
5097 +       link->release.function = &dtl1_release;
5098 +       link->release.data = (u_long)link;
5099 +       link->io.Attributes1 = IO_DATA_PATH_WIDTH_8;
5100 +       link->io.NumPorts1 = 8;
5101 +       link->irq.Attributes = IRQ_TYPE_EXCLUSIVE | IRQ_HANDLE_PRESENT;
5102 +       link->irq.IRQInfo1 = IRQ_INFO2_VALID | IRQ_LEVEL_ID;
5103 +
5104 +       if (irq_list[0] == -1)
5105 +               link->irq.IRQInfo2 = irq_mask;
5106 +       else
5107 +               for (i = 0; i < 4; i++)
5108 +                       link->irq.IRQInfo2 |= 1 << irq_list[i];
5109 +
5110 +       link->irq.Handler = dtl1_interrupt;
5111 +       link->irq.Instance = info;
5112 +
5113 +       link->conf.Attributes = CONF_ENABLE_IRQ;
5114 +       link->conf.Vcc = 50;
5115 +       link->conf.IntType = INT_MEMORY_AND_IO;
5116 +
5117 +       /* Register with Card Services */
5118 +       link->next = dev_list;
5119 +       dev_list = link;
5120 +       client_reg.dev_info = &dev_info;
5121 +       client_reg.Attributes = INFO_IO_CLIENT | INFO_CARD_SHARE;
5122 +       client_reg.EventMask =
5123 +               CS_EVENT_CARD_INSERTION | CS_EVENT_CARD_REMOVAL |
5124 +               CS_EVENT_RESET_PHYSICAL | CS_EVENT_CARD_RESET |
5125 +               CS_EVENT_PM_SUSPEND | CS_EVENT_PM_RESUME;
5126 +       client_reg.event_handler = &dtl1_event;
5127 +       client_reg.Version = 0x0210;
5128 +       client_reg.event_callback_args.client_data = link;
5129 +
5130 +       ret = CardServices(RegisterClient, &link->handle, &client_reg);
5131 +       if (ret != CS_SUCCESS) {
5132 +               cs_error(link->handle, RegisterClient, ret);
5133 +               dtl1_detach(link);
5134 +               return NULL;
5135 +       }
5136 +
5137 +       return link;
5138 +}
5139 +
5140 +
5141 +void dtl1_detach(dev_link_t *link)
5142 +{
5143 +       dtl1_info_t *info = link->priv;
5144 +       dev_link_t **linkp;
5145 +       int ret;
5146 +
5147 +       /* Locate device structure */
5148 +       for (linkp = &dev_list; *linkp; linkp = &(*linkp)->next)
5149 +               if (*linkp == link)
5150 +                       break;
5151 +
5152 +       if (*linkp == NULL)
5153 +               return;
5154 +
5155 +       del_timer(&link->release);
5156 +       if (link->state & DEV_CONFIG)
5157 +               dtl1_release((u_long)link);
5158 +
5159 +       if (link->handle) {
5160 +               ret = CardServices(DeregisterClient, link->handle);
5161 +               if (ret != CS_SUCCESS)
5162 +                       cs_error(link->handle, DeregisterClient, ret);
5163 +       }
5164 +
5165 +       /* Unlink device structure, free bits */
5166 +       *linkp = link->next;
5167 +
5168 +       kfree(info);
5169 +}
5170 +
5171 +
5172 +static int get_tuple(int fn, client_handle_t handle, tuple_t *tuple, cisparse_t *parse)
5173 +{
5174 +       int i;
5175 +
5176 +       i = CardServices(fn, handle, tuple);
5177 +       if (i != CS_SUCCESS)
5178 +               return CS_NO_MORE_ITEMS;
5179 +
5180 +       i = CardServices(GetTupleData, handle, tuple);
5181 +       if (i != CS_SUCCESS)
5182 +               return i;
5183 +
5184 +       return CardServices(ParseTuple, handle, tuple, parse);
5185 +}
5186 +
5187 +
5188 +#define first_tuple(a, b, c) get_tuple(GetFirstTuple, a, b, c)
5189 +#define next_tuple(a, b, c) get_tuple(GetNextTuple, a, b, c)
5190 +
5191 +void dtl1_config(dev_link_t *link)
5192 +{
5193 +       client_handle_t handle = link->handle;
5194 +       dtl1_info_t *info = link->priv;
5195 +       tuple_t tuple;
5196 +       u_short buf[256];
5197 +       cisparse_t parse;
5198 +       cistpl_cftable_entry_t *cf = &parse.cftable_entry;
5199 +       config_info_t config;
5200 +       int i, last_ret, last_fn;
5201 +
5202 +       tuple.TupleData = (cisdata_t *)buf;
5203 +       tuple.TupleOffset = 0;
5204 +       tuple.TupleDataMax = 255;
5205 +       tuple.Attributes = 0;
5206 +
5207 +       /* Get configuration register information */
5208 +       tuple.DesiredTuple = CISTPL_CONFIG;
5209 +       last_ret = first_tuple(handle, &tuple, &parse);
5210 +       if (last_ret != CS_SUCCESS) {
5211 +               last_fn = ParseTuple;
5212 +               goto cs_failed;
5213 +       }
5214 +       link->conf.ConfigBase = parse.config.base;
5215 +       link->conf.Present = parse.config.rmask[0];
5216 +
5217 +       /* Configure card */
5218 +       link->state |= DEV_CONFIG;
5219 +       i = CardServices(GetConfigurationInfo, handle, &config);
5220 +       link->conf.Vcc = config.Vcc;
5221 +
5222 +       tuple.TupleData = (cisdata_t *)buf;
5223 +       tuple.TupleOffset = 0;
5224 +       tuple.TupleDataMax = 255;
5225 +       tuple.Attributes = 0;
5226 +       tuple.DesiredTuple = CISTPL_CFTABLE_ENTRY;
5227 +
5228 +       /* Look for a generic full-sized window */
5229 +       link->io.NumPorts1 = 8;
5230 +       i = first_tuple(handle, &tuple, &parse);
5231 +       while (i != CS_NO_MORE_ITEMS) {
5232 +               if ((i == CS_SUCCESS) && (cf->io.nwin == 1) && (cf->io.win[0].len > 8)) {
5233 +                       link->conf.ConfigIndex = cf->index;
5234 +                       link->io.BasePort1 = cf->io.win[0].base;
5235 +                       link->io.NumPorts1 = cf->io.win[0].len; /*yo */
5236 +                       link->io.IOAddrLines = cf->io.flags & CISTPL_IO_LINES_MASK;
5237 +                       i = CardServices(RequestIO, link->handle, &link->io);
5238 +                       if (i == CS_SUCCESS)
5239 +                               break;
5240 +               }
5241 +               i = next_tuple(handle, &tuple, &parse);
5242 +       }
5243 +
5244 +       if (i != CS_SUCCESS) {
5245 +               cs_error(link->handle, RequestIO, i);
5246 +               goto failed;
5247 +       }
5248 +
5249 +       i = CardServices(RequestIRQ, link->handle, &link->irq);
5250 +       if (i != CS_SUCCESS) {
5251 +               cs_error(link->handle, RequestIRQ, i);
5252 +               link->irq.AssignedIRQ = 0;
5253 +       }
5254 +
5255 +       i = CardServices(RequestConfiguration, link->handle, &link->conf);
5256 +       if (i != CS_SUCCESS) {
5257 +               cs_error(link->handle, RequestConfiguration, i);
5258 +               goto failed;
5259 +       }
5260 +
5261 +       MOD_INC_USE_COUNT;
5262 +
5263 +       if (dtl1_open(info) != 0)
5264 +               goto failed;
5265 +
5266 +       strcpy(info->node.dev_name, info->hdev.name);
5267 +       link->dev = &info->node;
5268 +       link->state &= ~DEV_CONFIG_PENDING;
5269 +
5270 +       return;
5271 +
5272 +cs_failed:
5273 +       cs_error(link->handle, last_fn, last_ret);
5274 +
5275 +failed:
5276 +       dtl1_release((u_long)link);
5277 +}
5278 +
5279 +
5280 +void dtl1_release(u_long arg)
5281 +{
5282 +       dev_link_t *link = (dev_link_t *)arg;
5283 +       dtl1_info_t *info = link->priv;
5284 +
5285 +       if (link->state & DEV_PRESENT)
5286 +               dtl1_close(info);
5287 +
5288 +       MOD_DEC_USE_COUNT;
5289 +
5290 +       link->dev = NULL;
5291 +
5292 +       CardServices(ReleaseConfiguration, link->handle);
5293 +       CardServices(ReleaseIO, link->handle, &link->io);
5294 +       CardServices(ReleaseIRQ, link->handle, &link->irq);
5295 +
5296 +       link->state &= ~DEV_CONFIG;
5297 +}
5298 +
5299 +
5300 +int dtl1_event(event_t event, int priority, event_callback_args_t *args)
5301 +{
5302 +       dev_link_t *link = args->client_data;
5303 +       dtl1_info_t *info = link->priv;
5304 +
5305 +       switch (event) {
5306 +       case CS_EVENT_CARD_REMOVAL:
5307 +               link->state &= ~DEV_PRESENT;
5308 +               if (link->state & DEV_CONFIG) {
5309 +                       dtl1_close(info);
5310 +                       mod_timer(&link->release, jiffies + HZ / 20);
5311 +               }
5312 +               break;
5313 +       case CS_EVENT_CARD_INSERTION:
5314 +               link->state |= DEV_PRESENT | DEV_CONFIG_PENDING;
5315 +               dtl1_config(link);
5316 +               break;
5317 +       case CS_EVENT_PM_SUSPEND:
5318 +               link->state |= DEV_SUSPEND;
5319 +               /* Fall through... */
5320 +       case CS_EVENT_RESET_PHYSICAL:
5321 +               if (link->state & DEV_CONFIG)
5322 +                       CardServices(ReleaseConfiguration, link->handle);
5323 +               break;
5324 +       case CS_EVENT_PM_RESUME:
5325 +               link->state &= ~DEV_SUSPEND;
5326 +               /* Fall through... */
5327 +       case CS_EVENT_CARD_RESET:
5328 +               if (DEV_OK(link))
5329 +                       CardServices(RequestConfiguration, link->handle, &link->conf);
5330 +               break;
5331 +       }
5332 +
5333 +       return 0;
5334 +}
5335 +
5336 +
5337 +
5338 +/* ======================== Module initialization ======================== */
5339 +
5340 +
5341 +int __init init_dtl1_cs(void)
5342 +{
5343 +       servinfo_t serv;
5344 +       int err;
5345 +
5346 +       CardServices(GetCardServicesInfo, &serv);
5347 +       if (serv.Revision != CS_RELEASE_CODE) {
5348 +               printk(KERN_NOTICE "dtl1_cs: Card Services release does not match!\n");
5349 +               return -1;
5350 +       }
5351 +
5352 +       err = register_pccard_driver(&dev_info, &dtl1_attach, &dtl1_detach);
5353 +
5354 +       return err;
5355 +}
5356 +
5357 +
5358 +void __exit exit_dtl1_cs(void)
5359 +{
5360 +       unregister_pccard_driver(&dev_info);
5361 +
5362 +       while (dev_list != NULL)
5363 +               dtl1_detach(dev_list);
5364 +}
5365 +
5366 +
5367 +module_init(init_dtl1_cs);
5368 +module_exit(exit_dtl1_cs);
5369 +
5370 +EXPORT_NO_SYMBOLS;
5371 diff -urN linux-2.4.18/drivers/bluetooth/hci_bcsp.c linux-2.4.18-mh15/drivers/bluetooth/hci_bcsp.c
5372 --- linux-2.4.18/drivers/bluetooth/hci_bcsp.c   1970-01-01 01:00:00.000000000 +0100
5373 +++ linux-2.4.18-mh15/drivers/bluetooth/hci_bcsp.c      2004-08-01 16:26:23.000000000 +0200
5374 @@ -0,0 +1,710 @@
5375 +/* 
5376 +   BlueCore Serial Protocol (BCSP) for Linux Bluetooth stack (BlueZ).
5377 +   Copyright 2002 by Fabrizio Gennari <fabrizio.gennari@philips.com>
5378 +
5379 +   Based on
5380 +       hci_h4.c  by Maxim Krasnyansky <maxk@qualcomm.com>
5381 +       ABCSP     by Carl Orsborn <cjo@csr.com>
5382 +
5383 +   This program is free software; you can redistribute it and/or modify
5384 +   it under the terms of the GNU General Public License version 2 as
5385 +   published by the Free Software Foundation;
5386 +
5387 +   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
5388 +   OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
5389 +   FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS.
5390 +   IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) AND AUTHOR(S) BE LIABLE FOR ANY
5391 +   CLAIM, OR ANY SPECIAL INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES 
5392 +   WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 
5393 +   ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 
5394 +   OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
5395 +
5396 +   ALL LIABILITY, INCLUDING LIABILITY FOR INFRINGEMENT OF ANY PATENTS, 
5397 +   COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS, RELATING TO USE OF THIS 
5398 +   SOFTWARE IS DISCLAIMED.
5399 +*/
5400 +
5401 +/*
5402 + * $Id: hci_bcsp.c,v 1.2 2002/09/26 05:05:14 maxk Exp $
5403 + */
5404 +
5405 +#define VERSION "0.1"
5406 +
5407 +#include <linux/config.h>
5408 +#include <linux/module.h>
5409 +
5410 +#include <linux/version.h>
5411 +#include <linux/kernel.h>
5412 +#include <linux/init.h>
5413 +#include <linux/sched.h>
5414 +#include <linux/types.h>
5415 +#include <linux/fcntl.h>
5416 +#include <linux/interrupt.h>
5417 +#include <linux/ptrace.h>
5418 +#include <linux/poll.h>
5419 +
5420 +#include <linux/slab.h>
5421 +#include <linux/tty.h>
5422 +#include <linux/errno.h>
5423 +#include <linux/string.h>
5424 +#include <linux/signal.h>
5425 +#include <linux/ioctl.h>
5426 +#include <linux/skbuff.h>
5427 +
5428 +#include <net/bluetooth/bluetooth.h>
5429 +#include <net/bluetooth/hci_core.h>
5430 +#include "hci_uart.h"
5431 +#include "hci_bcsp.h"
5432 +
5433 +#ifndef HCI_UART_DEBUG
5434 +#undef  BT_DBG
5435 +#define BT_DBG( A... )
5436 +#undef  BT_DMP
5437 +#define BT_DMP( A... )
5438 +#endif
5439 +
5440 +/* ---- BCSP CRC calculation ---- */
5441 +
5442 +/* Table for calculating CRC for polynomial 0x1021, LSB processed first,
5443 +initial value 0xffff, bits shifted in reverse order. */
5444 +
5445 +static const u16 crc_table[] = {
5446 +       0x0000, 0x1081, 0x2102, 0x3183,
5447 +       0x4204, 0x5285, 0x6306, 0x7387,
5448 +       0x8408, 0x9489, 0xa50a, 0xb58b,
5449 +       0xc60c, 0xd68d, 0xe70e, 0xf78f
5450 +};
5451 +
5452 +/* Initialise the crc calculator */
5453 +#define BCSP_CRC_INIT(x) x = 0xffff
5454 +
5455 +/*
5456 +   Update crc with next data byte
5457 +
5458 +   Implementation note
5459 +        The data byte is treated as two nibbles.  The crc is generated
5460 +        in reverse, i.e., bits are fed into the register from the top.
5461 +*/
5462 +static void bcsp_crc_update(u16 *crc, u8 d)
5463 +{
5464 +       u16 reg = *crc;
5465 +
5466 +       reg = (reg >> 4) ^ crc_table[(reg ^ d) & 0x000f];
5467 +       reg = (reg >> 4) ^ crc_table[(reg ^ (d >> 4)) & 0x000f];
5468 +
5469 +       *crc = reg;
5470 +}
5471 +
5472 +/*
5473 +   Get reverse of generated crc
5474 +
5475 +   Implementation note
5476 +        The crc generator (bcsp_crc_init() and bcsp_crc_update())
5477 +        creates a reversed crc, so it needs to be swapped back before
5478 +        being passed on.
5479 +*/
5480 +static u16 bcsp_crc_reverse(u16 crc)
5481 +{
5482 +       u16 b, rev;
5483 +
5484 +       for (b = 0, rev = 0; b < 16; b++) {
5485 +               rev = rev << 1;
5486 +               rev |= (crc & 1);
5487 +               crc = crc >> 1;
5488 +       }
5489 +       return (rev);
5490 +}
5491 +
5492 +/* ---- BCSP core ---- */
5493 +
5494 +static void bcsp_slip_msgdelim(struct sk_buff *skb)
5495 +{
5496 +       const char pkt_delim = 0xc0;
5497 +       memcpy(skb_put(skb, 1), &pkt_delim, 1);
5498 +}
5499 +
5500 +static void bcsp_slip_one_byte(struct sk_buff *skb, u8 c)
5501 +{
5502 +       const char esc_c0[2] = { 0xdb, 0xdc };
5503 +       const char esc_db[2] = { 0xdb, 0xdd };
5504 +
5505 +       switch (c) {
5506 +       case 0xc0:
5507 +               memcpy(skb_put(skb, 2), &esc_c0, 2);
5508 +               break;
5509 +       case 0xdb:
5510 +               memcpy(skb_put(skb, 2), &esc_db, 2);
5511 +               break;
5512 +       default:
5513 +               memcpy(skb_put(skb, 1), &c, 1);
5514 +       }
5515 +}
5516 +
5517 +static int bcsp_enqueue(struct hci_uart *hu, struct sk_buff *skb)
5518 +{
5519 +       struct bcsp_struct *bcsp = hu->priv;
5520 +
5521 +       if (skb->len > 0xFFF) {
5522 +               BT_ERR("Packet too long");
5523 +               kfree_skb(skb);
5524 +               return 0;
5525 +       }
5526 +
5527 +       switch (skb->pkt_type) {
5528 +       case HCI_ACLDATA_PKT:
5529 +       case HCI_COMMAND_PKT:
5530 +               skb_queue_tail(&bcsp->rel, skb);
5531 +               break;
5532 +
5533 +       case HCI_SCODATA_PKT:
5534 +               skb_queue_tail(&bcsp->unrel, skb);
5535 +               break;
5536 +               
5537 +       default:
5538 +               BT_ERR("Unknown packet type");
5539 +               kfree_skb(skb);
5540 +               break;
5541 +       }
5542 +       return 0;
5543 +}
5544 +
5545 +static struct sk_buff *bcsp_prepare_pkt(struct bcsp_struct *bcsp, u8 *data,
5546 +               int len, int pkt_type)
5547 +{
5548 +       struct sk_buff *nskb;
5549 +       u8  hdr[4], chan;
5550 +       int rel, i;
5551 +
5552 +#ifdef CONFIG_BLUEZ_HCIUART_BCSP_TXCRC
5553 +       u16 BCSP_CRC_INIT(bcsp_txmsg_crc);
5554 +#endif
5555 +
5556 +       switch (pkt_type) {
5557 +       case HCI_ACLDATA_PKT:
5558 +               chan = 6;       /* BCSP ACL channel */
5559 +               rel = 1;        /* reliable channel */
5560 +               break;
5561 +       case HCI_COMMAND_PKT:
5562 +               chan = 5;       /* BCSP cmd/evt channel */
5563 +               rel = 1;        /* reliable channel */
5564 +               break;
5565 +       case HCI_SCODATA_PKT:
5566 +               chan = 7;       /* BCSP SCO channel */
5567 +               rel = 0;        /* unreliable channel */
5568 +               break;
5569 +       case BCSP_LE_PKT:
5570 +               chan = 1;       /* BCSP LE channel */
5571 +               rel = 0;        /* unreliable channel */
5572 +               break;
5573 +       case BCSP_ACK_PKT:
5574 +               chan = 0;       /* BCSP internal channel */
5575 +               rel = 0;        /* unreliable channel */
5576 +               break;
5577 +       default:
5578 +               BT_ERR("Unknown packet type");
5579 +               return NULL;
5580 +       }
5581 +
5582 +       /* Max len of packet: (original len +4(bcsp hdr) +2(crc))*2
5583 +          (because bytes 0xc0 and 0xdb are escaped, worst case is
5584 +          when the packet is all made of 0xc0 and 0xdb :) )
5585 +          + 2 (0xc0 delimiters at start and end). */
5586 +
5587 +       nskb = alloc_skb((len + 6) * 2 + 2, GFP_ATOMIC);
5588 +       if (!nskb)
5589 +               return NULL;
5590 +
5591 +       nskb->pkt_type = pkt_type;
5592 +
5593 +       bcsp_slip_msgdelim(nskb);
5594 +
5595 +       hdr[0] = bcsp->rxseq_txack << 3;
5596 +       bcsp->txack_req = 0;
5597 +       BT_DBG("We request packet no %u to card", bcsp->rxseq_txack);
5598 +
5599 +       if (rel) {
5600 +               hdr[0] |= 0x80 + bcsp->msgq_txseq;
5601 +               BT_DBG("Sending packet with seqno %u", bcsp->msgq_txseq);
5602 +               bcsp->msgq_txseq = ++(bcsp->msgq_txseq) & 0x07;
5603 +       }
5604 +#ifdef  CONFIG_BLUEZ_HCIUART_BCSP_TXCRC
5605 +       hdr[0] |= 0x40;
5606 +#endif
5607 +
5608 +       hdr[1]  = (len << 4) & 0xFF;
5609 +       hdr[1] |= chan;
5610 +       hdr[2]  = len >> 4;
5611 +       hdr[3]  = ~(hdr[0] + hdr[1] + hdr[2]);
5612 +
5613 +       /* Put BCSP header */
5614 +       for (i = 0; i < 4; i++) {
5615 +               bcsp_slip_one_byte(nskb, hdr[i]);
5616 +#ifdef  CONFIG_BLUEZ_HCIUART_BCSP_TXCRC
5617 +               bcsp_crc_update(&bcsp_txmsg_crc, hdr[i]);
5618 +#endif
5619 +       }
5620 +
5621 +       /* Put payload */
5622 +       for (i = 0; i < len; i++) {
5623 +               bcsp_slip_one_byte(nskb, data[i]);
5624 +#ifdef  CONFIG_BLUEZ_HCIUART_BCSP_TXCRC
5625 +               bcsp_crc_update(&bcsp_txmsg_crc, data[i]);
5626 +#endif
5627 +       }
5628 +
5629 +#ifdef CONFIG_BLUEZ_HCIUART_BCSP_TXCRC
5630 +       /* Put CRC */
5631 +       bcsp_txmsg_crc = bcsp_crc_reverse(bcsp_txmsg_crc);
5632 +       bcsp_slip_one_byte(nskb, (u8) ((bcsp_txmsg_crc >> 8) & 0x00ff));
5633 +       bcsp_slip_one_byte(nskb, (u8) (bcsp_txmsg_crc & 0x00ff));
5634 +#endif
5635 +
5636 +       bcsp_slip_msgdelim(nskb);
5637 +       return nskb;
5638 +}
5639 +
5640 +/* This is a rewrite of pkt_avail in ABCSP */
5641 +static struct sk_buff *bcsp_dequeue(struct hci_uart *hu)
5642 +{
5643 +       struct bcsp_struct *bcsp = (struct bcsp_struct *) hu->priv;
5644 +       unsigned long flags;
5645 +       struct sk_buff *skb;
5646 +       
5647 +       /* First of all, check for unreliable messages in the queue,
5648 +          since they have priority */
5649 +
5650 +       if ((skb = skb_dequeue(&bcsp->unrel)) != NULL) {
5651 +               struct sk_buff *nskb = bcsp_prepare_pkt(bcsp, skb->data, skb->len, skb->pkt_type);
5652 +               if (nskb) {
5653 +                       kfree_skb(skb);
5654 +                       return nskb;
5655 +               } else {
5656 +                       skb_queue_head(&bcsp->unrel, skb);
5657 +                       BT_ERR("Could not dequeue pkt because alloc_skb failed");
5658 +               }
5659 +       }
5660 +
5661 +       /* Now, try to send a reliable pkt. We can only send a
5662 +          reliable packet if the number of packets sent but not yet ack'ed
5663 +          is < than the winsize */
5664 +
5665 +       spin_lock_irqsave(&bcsp->unack.lock, flags);
5666 +
5667 +       if (bcsp->unack.qlen < BCSP_TXWINSIZE && (skb = skb_dequeue(&bcsp->rel)) != NULL) {
5668 +               struct sk_buff *nskb = bcsp_prepare_pkt(bcsp, skb->data, skb->len, skb->pkt_type);
5669 +               if (nskb) {
5670 +                       __skb_queue_tail(&bcsp->unack, skb);
5671 +                       mod_timer(&bcsp->tbcsp, jiffies + HZ / 4);
5672 +                       spin_unlock_irqrestore(&bcsp->unack.lock, flags);
5673 +                       return nskb;
5674 +               } else {
5675 +                       skb_queue_head(&bcsp->rel, skb);
5676 +                       BT_ERR("Could not dequeue pkt because alloc_skb failed");
5677 +               }
5678 +       }
5679 +
5680 +       spin_unlock_irqrestore(&bcsp->unack.lock, flags);
5681 +
5682 +
5683 +       /* We could not send a reliable packet, either because there are
5684 +          none or because there are too many unack'ed pkts. Did we receive
5685 +          any packets we have not acknowledged yet ? */
5686 +
5687 +       if (bcsp->txack_req) {
5688 +               /* if so, craft an empty ACK pkt and send it on BCSP unreliable
5689 +                  channel 0 */
5690 +               struct sk_buff *nskb = bcsp_prepare_pkt(bcsp, NULL, 0, BCSP_ACK_PKT);
5691 +               return nskb;
5692 +       }
5693 +
5694 +       /* We have nothing to send */
5695 +       return NULL;
5696 +}
5697 +
5698 +static int bcsp_flush(struct hci_uart *hu)
5699 +{
5700 +       BT_DBG("hu %p", hu);
5701 +       return 0;
5702 +}
5703 +
5704 +/* Remove ack'ed packets */
5705 +static void bcsp_pkt_cull(struct bcsp_struct *bcsp)
5706 +{
5707 +       unsigned long flags;
5708 +       struct sk_buff *skb;
5709 +       int i, pkts_to_be_removed;
5710 +       u8 seqno;
5711 +
5712 +       spin_lock_irqsave(&bcsp->unack.lock, flags);
5713 +
5714 +       pkts_to_be_removed = bcsp->unack.qlen;
5715 +       seqno = bcsp->msgq_txseq;
5716 +
5717 +       while (pkts_to_be_removed) {
5718 +               if (bcsp->rxack == seqno)
5719 +                       break;
5720 +               pkts_to_be_removed--;
5721 +               seqno = (seqno - 1) & 0x07;
5722 +       }
5723 +
5724 +       if (bcsp->rxack != seqno)
5725 +               BT_ERR("Peer acked invalid packet");
5726 +
5727 +       BT_DBG("Removing %u pkts out of %u, up to seqno %u",
5728 +              pkts_to_be_removed, bcsp->unack.qlen, (seqno - 1) & 0x07);
5729 +
5730 +       for (i = 0, skb = ((struct sk_buff *) &bcsp->unack)->next; i < pkts_to_be_removed
5731 +                       && skb != (struct sk_buff *) &bcsp->unack; i++) {
5732 +               struct sk_buff *nskb;
5733 +
5734 +               nskb = skb->next;
5735 +               __skb_unlink(skb, &bcsp->unack);
5736 +               kfree_skb(skb);
5737 +               skb = nskb;
5738 +       }
5739 +       if (bcsp->unack.qlen == 0)
5740 +               del_timer(&bcsp->tbcsp);
5741 +       spin_unlock_irqrestore(&bcsp->unack.lock, flags);
5742 +
5743 +       if (i != pkts_to_be_removed)
5744 +               BT_ERR("Removed only %u out of %u pkts", i, pkts_to_be_removed);
5745 +}
5746 +
5747 +/* Handle BCSP link-establishment packets. When we
5748 +   detect a "sync" packet, symptom that the BT module has reset,
5749 +   we do nothing :) (yet) */
5750 +static void bcsp_handle_le_pkt(struct hci_uart *hu)
5751 +{
5752 +       struct bcsp_struct *bcsp = hu->priv;
5753 +       u8 conf_pkt[4]     = { 0xad, 0xef, 0xac, 0xed };
5754 +       u8 conf_rsp_pkt[4] = { 0xde, 0xad, 0xd0, 0xd0 };
5755 +       u8 sync_pkt[4]     = { 0xda, 0xdc, 0xed, 0xed };
5756 +
5757 +       /* spot "conf" pkts and reply with a "conf rsp" pkt */
5758 +       if (bcsp->rx_skb->data[1] >> 4 == 4 && bcsp->rx_skb->data[2] == 0 &&
5759 +                       !memcmp(&bcsp->rx_skb->data[4], conf_pkt, 4)) {
5760 +               struct sk_buff *nskb = alloc_skb(4, GFP_ATOMIC);
5761 +
5762 +               BT_DBG("Found a LE conf pkt");
5763 +               if (!nskb)
5764 +                       return;
5765 +               memcpy(skb_put(nskb, 4), conf_rsp_pkt, 4);
5766 +               nskb->pkt_type = BCSP_LE_PKT;
5767 +
5768 +               skb_queue_head(&bcsp->unrel, nskb);
5769 +               hci_uart_tx_wakeup(hu);
5770 +       }
5771 +       /* Spot "sync" pkts. If we find one...disaster! */
5772 +       else if (bcsp->rx_skb->data[1] >> 4 == 4 && bcsp->rx_skb->data[2] == 0 &&
5773 +                       !memcmp(&bcsp->rx_skb->data[4], sync_pkt, 4)) {
5774 +               BT_ERR("Found a LE sync pkt, card has reset");
5775 +       }
5776 +}
5777 +
5778 +static inline void bcsp_unslip_one_byte(struct bcsp_struct *bcsp, unsigned char byte)
5779 +{
5780 +       const u8 c0 = 0xc0, db = 0xdb;
5781 +
5782 +       switch (bcsp->rx_esc_state) {
5783 +       case BCSP_ESCSTATE_NOESC:
5784 +               switch (byte) {
5785 +               case 0xdb:
5786 +                       bcsp->rx_esc_state = BCSP_ESCSTATE_ESC;
5787 +                       break;
5788 +               default:
5789 +                       memcpy(skb_put(bcsp->rx_skb, 1), &byte, 1);
5790 +                       if ((bcsp->rx_skb-> data[0] & 0x40) != 0 && 
5791 +                                       bcsp->rx_state != BCSP_W4_CRC)
5792 +                               bcsp_crc_update(&bcsp->message_crc, byte);
5793 +                       bcsp->rx_count--;
5794 +               }
5795 +               break;
5796 +
5797 +       case BCSP_ESCSTATE_ESC:
5798 +               switch (byte) {
5799 +               case 0xdc:
5800 +                       memcpy(skb_put(bcsp->rx_skb, 1), &c0, 1);
5801 +                       if ((bcsp->rx_skb-> data[0] & 0x40) != 0 && 
5802 +                                       bcsp->rx_state != BCSP_W4_CRC)
5803 +                               bcsp_crc_update(&bcsp-> message_crc, 0xc0);
5804 +                       bcsp->rx_esc_state = BCSP_ESCSTATE_NOESC;
5805 +                       bcsp->rx_count--;
5806 +                       break;
5807 +
5808 +               case 0xdd:
5809 +                       memcpy(skb_put(bcsp->rx_skb, 1), &db, 1);
5810 +                       if ((bcsp->rx_skb-> data[0] & 0x40) != 0 && 
5811 +                                       bcsp->rx_state != BCSP_W4_CRC) 
5812 +                               bcsp_crc_update(&bcsp-> message_crc, 0xdb);
5813 +                       bcsp->rx_esc_state = BCSP_ESCSTATE_NOESC;
5814 +                       bcsp->rx_count--;
5815 +                       break;
5816 +
5817 +               default:
5818 +                       BT_ERR ("Invalid byte %02x after esc byte", byte);
5819 +                       kfree_skb(bcsp->rx_skb);
5820 +                       bcsp->rx_skb = NULL;
5821 +                       bcsp->rx_state = BCSP_W4_PKT_DELIMITER;
5822 +                       bcsp->rx_count = 0;
5823 +               }
5824 +       }
5825 +}
5826 +
5827 +static inline void bcsp_complete_rx_pkt(struct hci_uart *hu)
5828 +{
5829 +       struct bcsp_struct *bcsp = hu->priv;
5830 +       int pass_up;
5831 +
5832 +       if (bcsp->rx_skb->data[0] & 0x80) {     /* reliable pkt */
5833 +               BT_DBG("Received seqno %u from card", bcsp->rxseq_txack);
5834 +               bcsp->rxseq_txack++;
5835 +               bcsp->rxseq_txack %= 0x8;
5836 +               bcsp->txack_req    = 1;
5837 +
5838 +               /* If needed, transmit an ack pkt */
5839 +               hci_uart_tx_wakeup(hu);
5840 +       }
5841 +
5842 +       bcsp->rxack = (bcsp->rx_skb->data[0] >> 3) & 0x07;
5843 +       BT_DBG("Request for pkt %u from card", bcsp->rxack);
5844 +
5845 +       bcsp_pkt_cull(bcsp);
5846 +       if ((bcsp->rx_skb->data[1] & 0x0f) == 6 &&
5847 +                       bcsp->rx_skb->data[0] & 0x80) {
5848 +               bcsp->rx_skb->pkt_type = HCI_ACLDATA_PKT;
5849 +               pass_up = 1;
5850 +       } else if ((bcsp->rx_skb->data[1] & 0x0f) == 5 &&
5851 +                       bcsp->rx_skb->data[0] & 0x80) {
5852 +               bcsp->rx_skb->pkt_type = HCI_EVENT_PKT;
5853 +               pass_up = 1;
5854 +       } else if ((bcsp->rx_skb->data[1] & 0x0f) == 7) {
5855 +               bcsp->rx_skb->pkt_type = HCI_SCODATA_PKT;
5856 +               pass_up = 1;
5857 +       } else if ((bcsp->rx_skb->data[1] & 0x0f) == 1 &&
5858 +                       !(bcsp->rx_skb->data[0] & 0x80)) {
5859 +               bcsp_handle_le_pkt(hu);
5860 +               pass_up = 0;
5861 +       } else
5862 +               pass_up = 0;
5863 +
5864 +       if (!pass_up) {
5865 +               if ((bcsp->rx_skb->data[1] & 0x0f) != 0 &&
5866 +                       (bcsp->rx_skb->data[1] & 0x0f) != 1) {
5867 +                       BT_ERR ("Packet for unknown channel (%u %s)",
5868 +                               bcsp->rx_skb->data[1] & 0x0f,
5869 +                               bcsp->rx_skb->data[0] & 0x80 ? 
5870 +                               "reliable" : "unreliable");
5871 +               }
5872 +               kfree_skb(bcsp->rx_skb);
5873 +       } else {
5874 +               /* Pull out BCSP hdr */
5875 +               skb_pull(bcsp->rx_skb, 4);
5876 +
5877 +               hci_recv_frame(bcsp->rx_skb);
5878 +       }
5879 +       bcsp->rx_state = BCSP_W4_PKT_DELIMITER;
5880 +       bcsp->rx_skb = NULL;
5881 +}
5882 +
5883 +/* Recv data */
5884 +static int bcsp_recv(struct hci_uart *hu, void *data, int count)
5885 +{
5886 +       struct bcsp_struct *bcsp = hu->priv;
5887 +       register unsigned char *ptr;
5888 +
5889 +       BT_DBG("hu %p count %d rx_state %ld rx_count %ld", 
5890 +               hu, count, bcsp->rx_state, bcsp->rx_count);
5891 +
5892 +       ptr = data;
5893 +       while (count) {
5894 +               if (bcsp->rx_count) {
5895 +                       if (*ptr == 0xc0) {
5896 +                               BT_ERR("Short BCSP packet");
5897 +                               kfree_skb(bcsp->rx_skb);
5898 +                               bcsp->rx_state = BCSP_W4_PKT_START;
5899 +                               bcsp->rx_count = 0;
5900 +                       } else
5901 +                               bcsp_unslip_one_byte(bcsp, *ptr);
5902 +
5903 +                       ptr++; count--;
5904 +                       continue;
5905 +               }
5906 +
5907 +               switch (bcsp->rx_state) {
5908 +               case BCSP_W4_BCSP_HDR:
5909 +                       if ((0xff & (u8) ~ (bcsp->rx_skb->data[0] + bcsp->rx_skb->data[1] +
5910 +                                       bcsp->rx_skb->data[2])) != bcsp->rx_skb->data[3]) {
5911 +                               BT_ERR("Error in BCSP hdr checksum");
5912 +                               kfree_skb(bcsp->rx_skb);
5913 +                               bcsp->rx_state = BCSP_W4_PKT_DELIMITER;
5914 +                               bcsp->rx_count = 0;
5915 +                               continue;
5916 +                       }
5917 +                       if (bcsp->rx_skb->data[0] & 0x80        /* reliable pkt */
5918 +                                       && (bcsp->rx_skb->data[0] & 0x07) != bcsp->rxseq_txack) {
5919 +                               BT_ERR ("Out-of-order packet arrived, got %u expected %u",
5920 +                                       bcsp->rx_skb->data[0] & 0x07, bcsp->rxseq_txack);
5921 +
5922 +                               kfree_skb(bcsp->rx_skb);
5923 +                               bcsp->rx_state = BCSP_W4_PKT_DELIMITER;
5924 +                               bcsp->rx_count = 0;
5925 +                               continue;
5926 +                       }
5927 +                       bcsp->rx_state = BCSP_W4_DATA;
5928 +                       bcsp->rx_count = (bcsp->rx_skb->data[1] >> 4) + 
5929 +                                       (bcsp->rx_skb->data[2] << 4);   /* May be 0 */
5930 +                       continue;
5931 +
5932 +               case BCSP_W4_DATA:
5933 +                       if (bcsp->rx_skb->data[0] & 0x40) {     /* pkt with crc */
5934 +                               bcsp->rx_state = BCSP_W4_CRC;
5935 +                               bcsp->rx_count = 2;
5936 +                       } else
5937 +                               bcsp_complete_rx_pkt(hu);
5938 +                       continue;
5939 +
5940 +               case BCSP_W4_CRC:
5941 +                       if (bcsp_crc_reverse(bcsp->message_crc) !=
5942 +                                       (bcsp->rx_skb->data[bcsp->rx_skb->len - 2] << 8) +
5943 +                                       bcsp->rx_skb->data[bcsp->rx_skb->len - 1]) {
5944 +
5945 +                               BT_ERR ("Checksum failed: computed %04x received %04x",
5946 +                                       bcsp_crc_reverse(bcsp->message_crc),
5947 +                                       (bcsp->rx_skb-> data[bcsp->rx_skb->len - 2] << 8) +
5948 +                                       bcsp->rx_skb->data[bcsp->rx_skb->len - 1]);
5949 +
5950 +                               kfree_skb(bcsp->rx_skb);
5951 +                               bcsp->rx_state = BCSP_W4_PKT_DELIMITER;
5952 +                               bcsp->rx_count = 0;
5953 +                               continue;
5954 +                       }
5955 +                       skb_trim(bcsp->rx_skb, bcsp->rx_skb->len - 2);
5956 +                       bcsp_complete_rx_pkt(hu);
5957 +                       continue;
5958 +
5959 +               case BCSP_W4_PKT_DELIMITER:
5960 +                       switch (*ptr) {
5961 +                       case 0xc0:
5962 +                               bcsp->rx_state = BCSP_W4_PKT_START;
5963 +                               break;
5964 +                       default:
5965 +                               /*BT_ERR("Ignoring byte %02x", *ptr);*/
5966 +                               break;
5967 +                       }
5968 +                       ptr++; count--;
5969 +                       break;
5970 +
5971 +               case BCSP_W4_PKT_START:
5972 +                       switch (*ptr) {
5973 +                       case 0xc0:
5974 +                               ptr++; count--;
5975 +                               break;
5976 +
5977 +                       default:
5978 +                               bcsp->rx_state = BCSP_W4_BCSP_HDR;
5979 +                               bcsp->rx_count = 4;
5980 +                               bcsp->rx_esc_state = BCSP_ESCSTATE_NOESC;
5981 +                               BCSP_CRC_INIT(bcsp->message_crc);
5982 +                               
5983 +                               /* Do not increment ptr or decrement count
5984 +                                * Allocate packet. Max len of a BCSP pkt= 
5985 +                                * 0xFFF (payload) +4 (header) +2 (crc) */
5986 +
5987 +                               bcsp->rx_skb = bluez_skb_alloc(0x1005, GFP_ATOMIC);
5988 +                               if (!bcsp->rx_skb) {
5989 +                                       BT_ERR("Can't allocate mem for new packet");
5990 +                                       bcsp->rx_state = BCSP_W4_PKT_DELIMITER;
5991 +                                       bcsp->rx_count = 0;
5992 +                                       return 0;
5993 +                               }
5994 +                               bcsp->rx_skb->dev = (void *) &hu->hdev;
5995 +                               break;
5996 +                       }
5997 +                       break;
5998 +               }
5999 +       }
6000 +       return count;
6001 +}
6002 +
6003 +       /* Arrange to retransmit all messages in the relq. */
6004 +static void bcsp_timed_event(unsigned long arg)
6005 +{
6006 +       struct hci_uart *hu = (struct hci_uart *) arg;
6007 +       struct bcsp_struct *bcsp = (struct bcsp_struct *) hu->priv;
6008 +       struct sk_buff *skb;
6009 +       unsigned long flags;
6010 +
6011 +       BT_DBG("hu %p retransmitting %u pkts", hu, bcsp->unack.qlen);
6012 +
6013 +       spin_lock_irqsave(&bcsp->unack.lock, flags);
6014 +
6015 +       while ((skb = __skb_dequeue_tail(&bcsp->unack)) != NULL) {
6016 +               bcsp->msgq_txseq = (bcsp->msgq_txseq - 1) & 0x07;
6017 +               skb_queue_head(&bcsp->rel, skb);
6018 +       }
6019 +
6020 +       spin_unlock_irqrestore(&bcsp->unack.lock, flags);
6021 +
6022 +       hci_uart_tx_wakeup(hu);
6023 +}
6024 +
6025 +static int bcsp_open(struct hci_uart *hu)
6026 +{
6027 +       struct bcsp_struct *bcsp;
6028 +
6029 +       BT_DBG("hu %p", hu);
6030 +
6031 +       bcsp = kmalloc(sizeof(*bcsp), GFP_ATOMIC);
6032 +       if (!bcsp)
6033 +               return -ENOMEM;
6034 +       memset(bcsp, 0, sizeof(*bcsp));
6035 +
6036 +       hu->priv = bcsp;
6037 +       skb_queue_head_init(&bcsp->unack);
6038 +       skb_queue_head_init(&bcsp->rel);
6039 +       skb_queue_head_init(&bcsp->unrel);
6040 +
6041 +       init_timer(&bcsp->tbcsp);
6042 +       bcsp->tbcsp.function = bcsp_timed_event;
6043 +       bcsp->tbcsp.data     = (u_long) hu;
6044 +
6045 +       bcsp->rx_state = BCSP_W4_PKT_DELIMITER;
6046 +
6047 +       return 0;
6048 +}
6049 +
6050 +static int bcsp_close(struct hci_uart *hu)
6051 +{
6052 +       struct bcsp_struct *bcsp = hu->priv;
6053 +       hu->priv = NULL;
6054 +
6055 +       BT_DBG("hu %p", hu);
6056 +
6057 +       skb_queue_purge(&bcsp->unack);
6058 +       skb_queue_purge(&bcsp->rel);
6059 +       skb_queue_purge(&bcsp->unrel);
6060 +       del_timer(&bcsp->tbcsp);
6061 +
6062 +       kfree(bcsp);
6063 +       return 0;
6064 +}
6065 +
6066 +static struct hci_uart_proto bcsp = {
6067 +       id:      HCI_UART_BCSP,
6068 +       open:    bcsp_open,
6069 +       close:   bcsp_close,
6070 +       enqueue: bcsp_enqueue,
6071 +       dequeue: bcsp_dequeue,
6072 +       recv:    bcsp_recv,
6073 +       flush:   bcsp_flush
6074 +};
6075 +
6076 +int bcsp_init(void)
6077 +{
6078 +       return hci_uart_register_proto(&bcsp);
6079 +}
6080 +
6081 +int bcsp_deinit(void)
6082 +{
6083 +       return hci_uart_unregister_proto(&bcsp);
6084 +}
6085 diff -urN linux-2.4.18/drivers/bluetooth/hci_bcsp.h linux-2.4.18-mh15/drivers/bluetooth/hci_bcsp.h
6086 --- linux-2.4.18/drivers/bluetooth/hci_bcsp.h   1970-01-01 01:00:00.000000000 +0100
6087 +++ linux-2.4.18-mh15/drivers/bluetooth/hci_bcsp.h      2004-08-01 16:26:23.000000000 +0200
6088 @@ -0,0 +1,70 @@
6089 +/* 
6090 +   BlueCore Serial Protocol (BCSP) for Linux Bluetooth stack (BlueZ).
6091 +   Copyright 2002 by Fabrizio Gennari <fabrizio.gennari@philips.com>
6092 +
6093 +   Based on
6094 +       hci_h4.c  by Maxim Krasnyansky <maxk@qualcomm.com>
6095 +       ABCSP     by Carl Orsborn <cjo@csr.com>
6096 +
6097 +   This program is free software; you can redistribute it and/or modify
6098 +   it under the terms of the GNU General Public License version 2 as
6099 +   published by the Free Software Foundation;
6100 +
6101 +   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
6102 +   OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
6103 +   FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS.
6104 +   IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) AND AUTHOR(S) BE LIABLE FOR ANY
6105 +   CLAIM, OR ANY SPECIAL INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES 
6106 +   WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 
6107 +   ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 
6108 +   OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
6109 +
6110 +   ALL LIABILITY, INCLUDING LIABILITY FOR INFRINGEMENT OF ANY PATENTS, 
6111 +   COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS, RELATING TO USE OF THIS 
6112 +   SOFTWARE IS DISCLAIMED.
6113 +*/
6114 +
6115 +/* 
6116 + * $Id: hci_bcsp.h,v 1.2 2002/09/26 05:05:14 maxk Exp $
6117 + */
6118 +
6119 +#ifndef __HCI_BCSP_H__
6120 +#define __HCI_BCSP_H__
6121 +
6122 +#define BCSP_TXWINSIZE  4
6123 +
6124 +#define BCSP_ACK_PKT    0x05
6125 +#define BCSP_LE_PKT     0x06
6126 +
6127 +struct bcsp_struct {
6128 +       struct sk_buff_head unack;      /* Unack'ed packets queue */
6129 +       struct sk_buff_head rel;        /* Reliable packets queue */
6130 +       struct sk_buff_head unrel;      /* Unreliable packets queue */
6131 +
6132 +       unsigned long rx_count;
6133 +       struct  sk_buff *rx_skb;
6134 +       u8      rxseq_txack;            /* rxseq == txack. */
6135 +       u8      rxack;                  /* Last packet sent by us that the peer ack'ed */
6136 +       struct  timer_list tbcsp;
6137 +       
6138 +       enum {
6139 +               BCSP_W4_PKT_DELIMITER,
6140 +               BCSP_W4_PKT_START,
6141 +               BCSP_W4_BCSP_HDR,
6142 +               BCSP_W4_DATA,
6143 +               BCSP_W4_CRC
6144 +       } rx_state;
6145 +
6146 +       enum {
6147 +               BCSP_ESCSTATE_NOESC,
6148 +               BCSP_ESCSTATE_ESC
6149 +       } rx_esc_state;
6150 +
6151 +       u16     message_crc;
6152 +       u8      txack_req;              /* Do we need to send ack's to the peer? */
6153 +
6154 +       /* Reliable packet sequence number - used to assign seq to each rel pkt. */
6155 +       u8      msgq_txseq;
6156 +};
6157 +
6158 +#endif /* __HCI_BCSP_H__ */
6159 diff -urN linux-2.4.18/drivers/bluetooth/hci_h4.c linux-2.4.18-mh15/drivers/bluetooth/hci_h4.c
6160 --- linux-2.4.18/drivers/bluetooth/hci_h4.c     1970-01-01 01:00:00.000000000 +0100
6161 +++ linux-2.4.18-mh15/drivers/bluetooth/hci_h4.c        2004-08-01 16:26:23.000000000 +0200
6162 @@ -0,0 +1,277 @@
6163 +/* 
6164 +   BlueZ - Bluetooth protocol stack for Linux
6165 +   Copyright (C) 2000-2001 Qualcomm Incorporated
6166 +
6167 +   Written 2000,2001 by Maxim Krasnyansky <maxk@qualcomm.com>
6168 +
6169 +   This program is free software; you can redistribute it and/or modify
6170 +   it under the terms of the GNU General Public License version 2 as
6171 +   published by the Free Software Foundation;
6172 +
6173 +   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
6174 +   OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
6175 +   FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS.
6176 +   IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) AND AUTHOR(S) BE LIABLE FOR ANY
6177 +   CLAIM, OR ANY SPECIAL INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES 
6178 +   WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 
6179 +   ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 
6180 +   OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
6181 +
6182 +   ALL LIABILITY, INCLUDING LIABILITY FOR INFRINGEMENT OF ANY PATENTS, 
6183 +   COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS, RELATING TO USE OF THIS 
6184 +   SOFTWARE IS DISCLAIMED.
6185 +*/
6186 +
6187 +/*
6188 + * BlueZ HCI UART(H4) protocol.
6189 + *
6190 + * $Id: hci_h4.c,v 1.3 2002/09/09 01:17:32 maxk Exp $    
6191 + */
6192 +#define VERSION "1.2"
6193 +
6194 +#include <linux/config.h>
6195 +#include <linux/module.h>
6196 +
6197 +#include <linux/version.h>
6198 +#include <linux/kernel.h>
6199 +#include <linux/init.h>
6200 +#include <linux/sched.h>
6201 +#include <linux/types.h>
6202 +#include <linux/fcntl.h>
6203 +#include <linux/interrupt.h>
6204 +#include <linux/ptrace.h>
6205 +#include <linux/poll.h>
6206 +
6207 +#include <linux/slab.h>
6208 +#include <linux/tty.h>
6209 +#include <linux/errno.h>
6210 +#include <linux/string.h>
6211 +#include <linux/signal.h>
6212 +#include <linux/ioctl.h>
6213 +#include <linux/skbuff.h>
6214 +
6215 +#include <net/bluetooth/bluetooth.h>
6216 +#include <net/bluetooth/hci_core.h>
6217 +#include "hci_uart.h"
6218 +#include "hci_h4.h"
6219 +
6220 +#ifndef HCI_UART_DEBUG
6221 +#undef  BT_DBG
6222 +#define BT_DBG( A... )
6223 +#undef  BT_DMP
6224 +#define BT_DMP( A... )
6225 +#endif
6226 +
6227 +/* Initialize protocol */
6228 +static int h4_open(struct hci_uart *hu)
6229 +{
6230 +       struct h4_struct *h4;
6231 +       
6232 +       BT_DBG("hu %p", hu);
6233 +       
6234 +       h4 = kmalloc(sizeof(*h4), GFP_ATOMIC);
6235 +       if (!h4)
6236 +               return -ENOMEM;
6237 +       memset(h4, 0, sizeof(*h4));
6238 +
6239 +       skb_queue_head_init(&h4->txq);
6240 +
6241 +       hu->priv = h4;
6242 +       return 0;
6243 +}
6244 +
6245 +/* Flush protocol data */
6246 +static int h4_flush(struct hci_uart *hu)
6247 +{
6248 +       struct h4_struct *h4 = hu->priv;
6249 +
6250 +       BT_DBG("hu %p", hu);
6251 +       skb_queue_purge(&h4->txq);
6252 +       return 0;
6253 +}
6254 +
6255 +/* Close protocol */
6256 +static int h4_close(struct hci_uart *hu)
6257 +{
6258 +       struct h4_struct *h4 = hu->priv;
6259 +       hu->priv = NULL;
6260 +
6261 +       BT_DBG("hu %p", hu);
6262 +
6263 +       skb_queue_purge(&h4->txq);
6264 +       if (h4->rx_skb)
6265 +               kfree_skb(h4->rx_skb);
6266 +
6267 +       hu->priv = NULL;
6268 +       kfree(h4);
6269 +       return 0;
6270 +}
6271 +
6272 +/* Enqueue frame for transmittion (padding, crc, etc) */
6273 +static int h4_enqueue(struct hci_uart *hu, struct sk_buff *skb)
6274 +{
6275 +       struct h4_struct *h4 = hu->priv;
6276 +
6277 +       BT_DBG("hu %p skb %p", hu, skb);
6278 +
6279 +       /* Prepend skb with frame type */
6280 +       memcpy(skb_push(skb, 1), &skb->pkt_type, 1);
6281 +       skb_queue_tail(&h4->txq, skb);
6282 +       return 0;
6283 +}
6284 +
6285 +static inline int h4_check_data_len(struct h4_struct *h4, int len)
6286 +{
6287 +       register int room = skb_tailroom(h4->rx_skb);
6288 +
6289 +       BT_DBG("len %d room %d", len, room);
6290 +       if (!len) {
6291 +               BT_DMP(h4->rx_skb->data, h4->rx_skb->len);
6292 +               hci_recv_frame(h4->rx_skb);
6293 +       } else if (len > room) {
6294 +               BT_ERR("Data length is too large");
6295 +               kfree_skb(h4->rx_skb);
6296 +       } else {
6297 +               h4->rx_state = H4_W4_DATA;
6298 +               h4->rx_count = len;
6299 +               return len;
6300 +       }
6301 +
6302 +       h4->rx_state = H4_W4_PACKET_TYPE;
6303 +       h4->rx_skb   = NULL;
6304 +       h4->rx_count = 0;
6305 +       return 0;
6306 +}
6307 +
6308 +/* Recv data */
6309 +static int h4_recv(struct hci_uart *hu, void *data, int count)
6310 +{
6311 +       struct h4_struct *h4 = hu->priv;
6312 +       register char *ptr;
6313 +       hci_event_hdr *eh;
6314 +       hci_acl_hdr   *ah;
6315 +       hci_sco_hdr   *sh;
6316 +       register int len, type, dlen;
6317 +
6318 +       BT_DBG("hu %p count %d rx_state %ld rx_count %ld", 
6319 +                       hu, count, h4->rx_state, h4->rx_count);
6320 +
6321 +       ptr = data;
6322 +       while (count) {
6323 +               if (h4->rx_count) {
6324 +                       len = MIN(h4->rx_count, count);
6325 +                       memcpy(skb_put(h4->rx_skb, len), ptr, len);
6326 +                       h4->rx_count -= len; count -= len; ptr += len;
6327 +
6328 +                       if (h4->rx_count)
6329 +                               continue;
6330 +
6331 +                       switch (h4->rx_state) {
6332 +                       case H4_W4_DATA:
6333 +                               BT_DBG("Complete data");
6334 +
6335 +                               BT_DMP(h4->rx_skb->data, h4->rx_skb->len);
6336 +
6337 +                               hci_recv_frame(h4->rx_skb);
6338 +
6339 +                               h4->rx_state = H4_W4_PACKET_TYPE;
6340 +                               h4->rx_skb = NULL;
6341 +                               continue;
6342 +
6343 +                       case H4_W4_EVENT_HDR:
6344 +                               eh = (hci_event_hdr *) h4->rx_skb->data;
6345 +
6346 +                               BT_DBG("Event header: evt 0x%2.2x plen %d", eh->evt, eh->plen);
6347 +
6348 +                               h4_check_data_len(h4, eh->plen);
6349 +                               continue;
6350 +
6351 +                       case H4_W4_ACL_HDR:
6352 +                               ah = (hci_acl_hdr *) h4->rx_skb->data;
6353 +                               dlen = __le16_to_cpu(ah->dlen);
6354 +
6355 +                               BT_DBG("ACL header: dlen %d", dlen);
6356 +
6357 +                               h4_check_data_len(h4, dlen);
6358 +                               continue;
6359 +
6360 +                       case H4_W4_SCO_HDR:
6361 +                               sh = (hci_sco_hdr *) h4->rx_skb->data;
6362 +
6363 +                               BT_DBG("SCO header: dlen %d", sh->dlen);
6364 +
6365 +                               h4_check_data_len(h4, sh->dlen);
6366 +                               continue;
6367 +                       }
6368 +               }
6369 +
6370 +               /* H4_W4_PACKET_TYPE */
6371 +               switch (*ptr) {
6372 +               case HCI_EVENT_PKT:
6373 +                       BT_DBG("Event packet");
6374 +                       h4->rx_state = H4_W4_EVENT_HDR;
6375 +                       h4->rx_count = HCI_EVENT_HDR_SIZE;
6376 +                       type = HCI_EVENT_PKT;
6377 +                       break;
6378 +
6379 +               case HCI_ACLDATA_PKT:
6380 +                       BT_DBG("ACL packet");
6381 +                       h4->rx_state = H4_W4_ACL_HDR;
6382 +                       h4->rx_count = HCI_ACL_HDR_SIZE;
6383 +                       type = HCI_ACLDATA_PKT;
6384 +                       break;
6385 +
6386 +               case HCI_SCODATA_PKT:
6387 +                       BT_DBG("SCO packet");
6388 +                       h4->rx_state = H4_W4_SCO_HDR;
6389 +                       h4->rx_count = HCI_SCO_HDR_SIZE;
6390 +                       type = HCI_SCODATA_PKT;
6391 +                       break;
6392 +
6393 +               default:
6394 +                       BT_ERR("Unknown HCI packet type %2.2x", (__u8)*ptr);
6395 +                       hu->hdev.stat.err_rx++;
6396 +                       ptr++; count--;
6397 +                       continue;
6398 +               };
6399 +               ptr++; count--;
6400 +
6401 +               /* Allocate packet */
6402 +               h4->rx_skb = bluez_skb_alloc(HCI_MAX_FRAME_SIZE, GFP_ATOMIC);
6403 +               if (!h4->rx_skb) {
6404 +                       BT_ERR("Can't allocate mem for new packet");
6405 +                       h4->rx_state = H4_W4_PACKET_TYPE;
6406 +                       h4->rx_count = 0;
6407 +                       return 0;
6408 +               }
6409 +               h4->rx_skb->dev = (void *) &hu->hdev;
6410 +               h4->rx_skb->pkt_type = type;
6411 +       }
6412 +       return count;
6413 +}
6414 +
6415 +static struct sk_buff *h4_dequeue(struct hci_uart *hu)
6416 +{
6417 +       struct h4_struct *h4 = hu->priv;
6418 +       return skb_dequeue(&h4->txq);
6419 +}
6420 +
6421 +static struct hci_uart_proto h4p = {
6422 +       id:      HCI_UART_H4,
6423 +       open:    h4_open,
6424 +       close:   h4_close,
6425 +       recv:    h4_recv,
6426 +       enqueue: h4_enqueue,
6427 +       dequeue: h4_dequeue,
6428 +       flush:   h4_flush,
6429 +};
6430 +             
6431 +int h4_init(void)
6432 +{
6433 +       return hci_uart_register_proto(&h4p);
6434 +}
6435 +
6436 +int h4_deinit(void)
6437 +{
6438 +       return hci_uart_unregister_proto(&h4p);
6439 +}
6440 diff -urN linux-2.4.18/drivers/bluetooth/hci_h4.h linux-2.4.18-mh15/drivers/bluetooth/hci_h4.h
6441 --- linux-2.4.18/drivers/bluetooth/hci_h4.h     1970-01-01 01:00:00.000000000 +0100
6442 +++ linux-2.4.18-mh15/drivers/bluetooth/hci_h4.h        2004-08-01 16:26:23.000000000 +0200
6443 @@ -0,0 +1,44 @@
6444 +/* 
6445 +   BlueZ - Bluetooth protocol stack for Linux
6446 +   Copyright (C) 2000-2001 Qualcomm Incorporated
6447 +
6448 +   Written 2000,2001 by Maxim Krasnyansky <maxk@qualcomm.com>
6449 +
6450 +   This program is free software; you can redistribute it and/or modify
6451 +   it under the terms of the GNU General Public License version 2 as
6452 +   published by the Free Software Foundation;
6453 +
6454 +   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
6455 +   OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
6456 +   FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS.
6457 +   IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) AND AUTHOR(S) BE LIABLE FOR ANY
6458 +   CLAIM, OR ANY SPECIAL INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES 
6459 +   WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 
6460 +   ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 
6461 +   OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
6462 +
6463 +   ALL LIABILITY, INCLUDING LIABILITY FOR INFRINGEMENT OF ANY PATENTS, 
6464 +   COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS, RELATING TO USE OF THIS 
6465 +   SOFTWARE IS DISCLAIMED.
6466 +*/
6467 +
6468 +/*
6469 + * $Id: hci_h4.h,v 1.2 2002/09/09 01:17:32 maxk Exp $
6470 + */
6471 +
6472 +#ifdef __KERNEL__
6473 +struct h4_struct {
6474 +       unsigned long rx_state;
6475 +       unsigned long rx_count;
6476 +       struct sk_buff *rx_skb;
6477 +       struct sk_buff_head txq;
6478 +};
6479 +
6480 +/* H4 receiver States */
6481 +#define H4_W4_PACKET_TYPE 0
6482 +#define H4_W4_EVENT_HDR          1
6483 +#define H4_W4_ACL_HDR     2
6484 +#define H4_W4_SCO_HDR     3
6485 +#define H4_W4_DATA        4
6486 +
6487 +#endif /* __KERNEL__ */
6488 diff -urN linux-2.4.18/drivers/bluetooth/hci_ldisc.c linux-2.4.18-mh15/drivers/bluetooth/hci_ldisc.c
6489 --- linux-2.4.18/drivers/bluetooth/hci_ldisc.c  1970-01-01 01:00:00.000000000 +0100
6490 +++ linux-2.4.18-mh15/drivers/bluetooth/hci_ldisc.c     2004-08-01 16:26:23.000000000 +0200
6491 @@ -0,0 +1,579 @@
6492 +/* 
6493 +   BlueZ - Bluetooth protocol stack for Linux
6494 +   Copyright (C) 2000-2001 Qualcomm Incorporated
6495 +
6496 +   Written 2000,2001 by Maxim Krasnyansky <maxk@qualcomm.com>
6497 +
6498 +   This program is free software; you can redistribute it and/or modify
6499 +   it under the terms of the GNU General Public License version 2 as
6500 +   published by the Free Software Foundation;
6501 +
6502 +   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
6503 +   OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
6504 +   FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS.
6505 +   IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) AND AUTHOR(S) BE LIABLE FOR ANY
6506 +   CLAIM, OR ANY SPECIAL INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES 
6507 +   WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 
6508 +   ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 
6509 +   OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
6510 +
6511 +   ALL LIABILITY, INCLUDING LIABILITY FOR INFRINGEMENT OF ANY PATENTS, 
6512 +   COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS, RELATING TO USE OF THIS 
6513 +   SOFTWARE IS DISCLAIMED.
6514 +*/
6515 +
6516 +/*
6517 + * BlueZ HCI UART driver.
6518 + *
6519 + * $Id: hci_ldisc.c,v 1.5 2002/10/02 18:37:20 maxk Exp $    
6520 + */
6521 +#define VERSION "2.1"
6522 +
6523 +#include <linux/config.h>
6524 +#include <linux/module.h>
6525 +
6526 +#include <linux/version.h>
6527 +#include <linux/kernel.h>
6528 +#include <linux/init.h>
6529 +#include <linux/sched.h>
6530 +#include <linux/types.h>
6531 +#include <linux/fcntl.h>
6532 +#include <linux/interrupt.h>
6533 +#include <linux/ptrace.h>
6534 +#include <linux/poll.h>
6535 +
6536 +#include <linux/slab.h>
6537 +#include <linux/tty.h>
6538 +#include <linux/errno.h>
6539 +#include <linux/string.h>
6540 +#include <linux/signal.h>
6541 +#include <linux/ioctl.h>
6542 +#include <linux/skbuff.h>
6543 +
6544 +#include <net/bluetooth/bluetooth.h>
6545 +#include <net/bluetooth/hci_core.h>
6546 +#include "hci_uart.h"
6547 +
6548 +#ifndef HCI_UART_DEBUG
6549 +#undef  BT_DBG
6550 +#define BT_DBG( A... )
6551 +#undef  BT_DMP
6552 +#define BT_DMP( A... )
6553 +#endif
6554 +
6555 +static struct hci_uart_proto *hup[HCI_UART_MAX_PROTO];
6556 +
6557 +int hci_uart_register_proto(struct hci_uart_proto *p)
6558 +{
6559 +       if (p->id >= HCI_UART_MAX_PROTO)
6560 +               return -EINVAL;
6561 +
6562 +       if (hup[p->id])
6563 +               return -EEXIST;
6564 +
6565 +       hup[p->id] = p;
6566 +       return 0;
6567 +}
6568 +
6569 +int hci_uart_unregister_proto(struct hci_uart_proto *p)
6570 +{
6571 +       if (p->id >= HCI_UART_MAX_PROTO)
6572 +               return -EINVAL;
6573 +
6574 +       if (!hup[p->id])
6575 +               return -EINVAL;
6576 +
6577 +       hup[p->id] = NULL;
6578 +       return 0;
6579 +}
6580 +
6581 +static struct hci_uart_proto *hci_uart_get_proto(unsigned int id)
6582 +{
6583 +       if (id >= HCI_UART_MAX_PROTO)
6584 +               return NULL;
6585 +       return hup[id];
6586 +}
6587 +
6588 +static inline void hci_uart_tx_complete(struct hci_uart *hu, int pkt_type)
6589 +{
6590 +       struct hci_dev *hdev = &hu->hdev;
6591 +       
6592 +       /* Update HCI stat counters */
6593 +       switch (pkt_type) {
6594 +       case HCI_COMMAND_PKT:
6595 +               hdev->stat.cmd_tx++;
6596 +               break;
6597 +
6598 +       case HCI_ACLDATA_PKT:
6599 +               hdev->stat.acl_tx++;
6600 +               break;
6601 +
6602 +       case HCI_SCODATA_PKT:
6603 +               hdev->stat.cmd_tx++;
6604 +               break;
6605 +       }
6606 +}
6607 +
6608 +static inline struct sk_buff *hci_uart_dequeue(struct hci_uart *hu)
6609 +{
6610 +       struct sk_buff *skb = hu->tx_skb;
6611 +       if (!skb)
6612 +               skb = hu->proto->dequeue(hu);
6613 +       else
6614 +               hu->tx_skb = NULL;
6615 +       return skb;
6616 +}
6617 +
6618 +int hci_uart_tx_wakeup(struct hci_uart *hu)
6619 +{
6620 +       struct tty_struct *tty = hu->tty;
6621 +       struct hci_dev *hdev = &hu->hdev;
6622 +       struct sk_buff *skb;
6623 +       
6624 +       if (test_and_set_bit(HCI_UART_SENDING, &hu->tx_state)) {
6625 +               set_bit(HCI_UART_TX_WAKEUP, &hu->tx_state);
6626 +               return 0;
6627 +       }
6628 +
6629 +       BT_DBG("");
6630 +
6631 +restart:
6632 +       clear_bit(HCI_UART_TX_WAKEUP, &hu->tx_state);
6633 +
6634 +       while ((skb = hci_uart_dequeue(hu))) {
6635 +               int len;
6636 +       
6637 +               set_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
6638 +               len = tty->driver.write(tty, 0, skb->data, skb->len);
6639 +               hdev->stat.byte_tx += len;
6640 +
6641 +               skb_pull(skb, len);
6642 +               if (skb->len) {
6643 +                       hu->tx_skb = skb;
6644 +                       break;
6645 +               }
6646 +       
6647 +               hci_uart_tx_complete(hu, skb->pkt_type);
6648 +               kfree_skb(skb);
6649 +       } 
6650 +       
6651 +       if (test_bit(HCI_UART_TX_WAKEUP, &hu->tx_state))
6652 +               goto restart;
6653 +
6654 +       clear_bit(HCI_UART_SENDING, &hu->tx_state);
6655 +       return 0;
6656 +}
6657 +
6658 +/* ------- Interface to HCI layer ------ */
6659 +/* Initialize device */
6660 +static int hci_uart_open(struct hci_dev *hdev)
6661 +{
6662 +       BT_DBG("%s %p", hdev->name, hdev);
6663 +
6664 +       /* Nothing to do for UART driver */
6665 +
6666 +       set_bit(HCI_RUNNING, &hdev->flags);
6667 +       return 0;
6668 +}
6669 +
6670 +/* Reset device */
6671 +static int hci_uart_flush(struct hci_dev *hdev)
6672 +{
6673 +       struct hci_uart *hu  = (struct hci_uart *) hdev->driver_data;
6674 +       struct tty_struct *tty = hu->tty;
6675 +
6676 +       BT_DBG("hdev %p tty %p", hdev, tty);
6677 +
6678 +       if (hu->tx_skb) {
6679 +               kfree_skb(hu->tx_skb); hu->tx_skb = NULL;
6680 +       }
6681 +
6682 +       /* Flush any pending characters in the driver and discipline. */
6683 +       if (tty->ldisc.flush_buffer)
6684 +               tty->ldisc.flush_buffer(tty);
6685 +
6686 +       if (tty->driver.flush_buffer)
6687 +               tty->driver.flush_buffer(tty);
6688 +
6689 +       if (test_bit(HCI_UART_PROTO_SET, &hu->flags))
6690 +               hu->proto->flush(hu);
6691 +
6692 +       return 0;
6693 +}
6694 +
6695 +/* Close device */
6696 +static int hci_uart_close(struct hci_dev *hdev)
6697 +{
6698 +       BT_DBG("hdev %p", hdev);
6699 +
6700 +       if (!test_and_clear_bit(HCI_RUNNING, &hdev->flags))
6701 +               return 0;
6702 +
6703 +       hci_uart_flush(hdev);
6704 +       return 0;
6705 +}
6706 +
6707 +/* Send frames from HCI layer */
6708 +static int hci_uart_send_frame(struct sk_buff *skb)
6709 +{
6710 +       struct hci_dev* hdev = (struct hci_dev *) skb->dev;
6711 +       struct tty_struct *tty;
6712 +       struct hci_uart *hu;
6713 +
6714 +       if (!hdev) {
6715 +               BT_ERR("Frame for uknown device (hdev=NULL)");
6716 +               return -ENODEV;
6717 +       }
6718 +
6719 +       if (!test_bit(HCI_RUNNING, &hdev->flags))
6720 +               return -EBUSY;
6721 +
6722 +       hu = (struct hci_uart *) hdev->driver_data;
6723 +       tty = hu->tty;
6724 +
6725 +       BT_DBG("%s: type %d len %d", hdev->name, skb->pkt_type, skb->len);
6726 +
6727 +       hu->proto->enqueue(hu, skb);
6728 +
6729 +       hci_uart_tx_wakeup(hu);
6730 +       return 0;
6731 +}
6732 +
6733 +static void hci_uart_destruct(struct hci_dev *hdev)
6734 +{
6735 +       struct hci_uart *hu;
6736 +
6737 +       if (!hdev) return;
6738 +
6739 +       BT_DBG("%s", hdev->name);
6740 +
6741 +       hu = (struct hci_uart *) hdev->driver_data;
6742 +       kfree(hu);
6743 +
6744 +       MOD_DEC_USE_COUNT;
6745 +}
6746 +
6747 +/* ------ LDISC part ------ */
6748 +/* hci_uart_tty_open
6749 + * 
6750 + *     Called when line discipline changed to HCI_UART.
6751 + *
6752 + * Arguments:
6753 + *     tty    pointer to tty info structure
6754 + * Return Value:    
6755 + *     0 if success, otherwise error code
6756 + */
6757 +static int hci_uart_tty_open(struct tty_struct *tty)
6758 +{
6759 +       struct hci_uart *hu = (void *) tty->disc_data;
6760 +
6761 +       BT_DBG("tty %p", tty);
6762 +
6763 +       if (hu)
6764 +               return -EEXIST;
6765 +
6766 +       if (!(hu = kmalloc(sizeof(struct hci_uart), GFP_KERNEL))) {
6767 +               BT_ERR("Can't allocate controll structure");
6768 +               return -ENFILE;
6769 +       }
6770 +       memset(hu, 0, sizeof(struct hci_uart));
6771 +
6772 +       tty->disc_data = hu;
6773 +       hu->tty = tty;
6774 +
6775 +       spin_lock_init(&hu->rx_lock);
6776 +
6777 +       /* Flush any pending characters in the driver and line discipline */
6778 +       if (tty->ldisc.flush_buffer)
6779 +               tty->ldisc.flush_buffer(tty);
6780 +
6781 +       if (tty->driver.flush_buffer)
6782 +               tty->driver.flush_buffer(tty);
6783 +       
6784 +       MOD_INC_USE_COUNT;
6785 +       return 0;
6786 +}
6787 +
6788 +/* hci_uart_tty_close()
6789 + *
6790 + *    Called when the line discipline is changed to something
6791 + *    else, the tty is closed, or the tty detects a hangup.
6792 + */
6793 +static void hci_uart_tty_close(struct tty_struct *tty)
6794 +{
6795 +       struct hci_uart *hu = (void *)tty->disc_data;
6796 +
6797 +       BT_DBG("tty %p", tty);
6798 +
6799 +       /* Detach from the tty */
6800 +       tty->disc_data = NULL;
6801 +
6802 +       if (hu) {
6803 +               struct hci_dev *hdev = &hu->hdev;
6804 +               hci_uart_close(hdev);
6805 +
6806 +               if (test_and_clear_bit(HCI_UART_PROTO_SET, &hu->flags)) {
6807 +                       hu->proto->close(hu);
6808 +                       hci_unregister_dev(hdev);
6809 +               }
6810 +
6811 +               MOD_DEC_USE_COUNT;
6812 +       }
6813 +}
6814 +
6815 +/* hci_uart_tty_wakeup()
6816 + *
6817 + *    Callback for transmit wakeup. Called when low level
6818 + *    device driver can accept more send data.
6819 + *
6820 + * Arguments:        tty    pointer to associated tty instance data
6821 + * Return Value:    None
6822 + */
6823 +static void hci_uart_tty_wakeup(struct tty_struct *tty)
6824 +{
6825 +       struct hci_uart *hu = (void *)tty->disc_data;
6826 +
6827 +       BT_DBG("");
6828 +
6829 +       if (!hu)
6830 +               return;
6831 +
6832 +       clear_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
6833 +
6834 +       if (tty != hu->tty)
6835 +               return;
6836 +
6837 +       if (test_bit(HCI_UART_PROTO_SET, &hu->flags))
6838 +               hci_uart_tx_wakeup(hu);
6839 +}
6840 +
6841 +/* hci_uart_tty_room()
6842 + * 
6843 + *    Callback function from tty driver. Return the amount of 
6844 + *    space left in the receiver's buffer to decide if remote
6845 + *    transmitter is to be throttled.
6846 + *
6847 + * Arguments:        tty    pointer to associated tty instance data
6848 + * Return Value:    number of bytes left in receive buffer
6849 + */
6850 +static int hci_uart_tty_room (struct tty_struct *tty)
6851 +{
6852 +       return 65536;
6853 +}
6854 +
6855 +/* hci_uart_tty_receive()
6856 + * 
6857 + *     Called by tty low level driver when receive data is
6858 + *     available.
6859 + *     
6860 + * Arguments:  tty          pointer to tty isntance data
6861 + *             data         pointer to received data
6862 + *             flags        pointer to flags for data
6863 + *             count        count of received data in bytes
6864 + *     
6865 + * Return Value:    None
6866 + */
6867 +static void hci_uart_tty_receive(struct tty_struct *tty, const __u8 *data, char *flags, int count)
6868 +{
6869 +       struct hci_uart *hu = (void *)tty->disc_data;
6870 +       
6871 +       if (!hu || tty != hu->tty)
6872 +               return;
6873 +
6874 +       if (!test_bit(HCI_UART_PROTO_SET, &hu->flags))
6875 +               return;
6876 +       
6877 +       spin_lock(&hu->rx_lock);
6878 +       hu->proto->recv(hu, (void *) data, count);
6879 +       hu->hdev.stat.byte_rx += count;
6880 +       spin_unlock(&hu->rx_lock);
6881 +
6882 +       if (test_and_clear_bit(TTY_THROTTLED,&tty->flags) && tty->driver.unthrottle)
6883 +               tty->driver.unthrottle(tty);
6884 +}
6885 +
6886 +static int hci_uart_register_dev(struct hci_uart *hu)
6887 +{
6888 +       struct hci_dev *hdev;
6889 +
6890 +       BT_DBG("");
6891 +
6892 +       /* Initialize and register HCI device */
6893 +       hdev = &hu->hdev;
6894 +
6895 +       hdev->type = HCI_UART;
6896 +       hdev->driver_data = hu;
6897 +
6898 +       hdev->open  = hci_uart_open;
6899 +       hdev->close = hci_uart_close;
6900 +       hdev->flush = hci_uart_flush;
6901 +       hdev->send  = hci_uart_send_frame;
6902 +       hdev->destruct = hci_uart_destruct;
6903 +
6904 +       if (hci_register_dev(hdev) < 0) {
6905 +               BT_ERR("Can't register HCI device %s", hdev->name);
6906 +               return -ENODEV;
6907 +       }
6908 +       MOD_INC_USE_COUNT;
6909 +       return 0;
6910 +}
6911 +
6912 +static int hci_uart_set_proto(struct hci_uart *hu, int id)
6913 +{
6914 +       struct hci_uart_proto *p;
6915 +       int err;        
6916 +       
6917 +       p = hci_uart_get_proto(id);
6918 +       if (!p)
6919 +               return -EPROTONOSUPPORT;
6920 +
6921 +       err = p->open(hu);
6922 +       if (err)
6923 +               return err;
6924 +
6925 +       hu->proto = p;
6926 +
6927 +       err = hci_uart_register_dev(hu);
6928 +       if (err) {
6929 +               p->close(hu);
6930 +               return err;
6931 +       }
6932 +       return 0;
6933 +}
6934 +
6935 +/* hci_uart_tty_ioctl()
6936 + *
6937 + *    Process IOCTL system call for the tty device.
6938 + *
6939 + * Arguments:
6940 + *
6941 + *    tty        pointer to tty instance data
6942 + *    file       pointer to open file object for device
6943 + *    cmd        IOCTL command code
6944 + *    arg        argument for IOCTL call (cmd dependent)
6945 + *
6946 + * Return Value:    Command dependent
6947 + */
6948 +static int hci_uart_tty_ioctl(struct tty_struct *tty, struct file * file,
6949 +                            unsigned int cmd, unsigned long arg)
6950 +{
6951 +       struct hci_uart *hu = (void *)tty->disc_data;
6952 +       int err = 0;
6953 +
6954 +       BT_DBG("");
6955 +
6956 +       /* Verify the status of the device */
6957 +       if (!hu)
6958 +               return -EBADF;
6959 +
6960 +       switch (cmd) {
6961 +       case HCIUARTSETPROTO:
6962 +               if (!test_and_set_bit(HCI_UART_PROTO_SET, &hu->flags)) {
6963 +                       err = hci_uart_set_proto(hu, arg);
6964 +                       if (err) {
6965 +                               clear_bit(HCI_UART_PROTO_SET, &hu->flags);
6966 +                               return err;
6967 +                       }
6968 +                       tty->low_latency = 1;
6969 +               } else  
6970 +                       return -EBUSY;
6971 +
6972 +       case HCIUARTGETPROTO:
6973 +               if (test_bit(HCI_UART_PROTO_SET, &hu->flags))
6974 +                       return hu->proto->id;
6975 +               return -EUNATCH;
6976 +               
6977 +       default:
6978 +               err = n_tty_ioctl(tty, file, cmd, arg);
6979 +               break;
6980 +       };
6981 +
6982 +       return err;
6983 +}
6984 +
6985 +/*
6986 + * We don't provide read/write/poll interface for user space.
6987 + */
6988 +static ssize_t hci_uart_tty_read(struct tty_struct *tty, struct file *file, unsigned char *buf, size_t nr)
6989 +{
6990 +       return 0;
6991 +}
6992 +static ssize_t hci_uart_tty_write(struct tty_struct *tty, struct file *file, const unsigned char *data, size_t count)
6993 +{
6994 +       return 0;
6995 +}
6996 +static unsigned int hci_uart_tty_poll(struct tty_struct *tty, struct file *filp, poll_table *wait)
6997 +{
6998 +       return 0;
6999 +}
7000 +
7001 +#ifdef CONFIG_BLUEZ_HCIUART_H4
7002 +int h4_init(void);
7003 +int h4_deinit(void);
7004 +#endif
7005 +#ifdef CONFIG_BLUEZ_HCIUART_BCSP
7006 +int bcsp_init(void);
7007 +int bcsp_deinit(void);
7008 +#endif
7009 +
7010 +int __init hci_uart_init(void)
7011 +{
7012 +       static struct tty_ldisc hci_uart_ldisc;
7013 +       int err;
7014 +
7015 +       BT_INFO("BlueZ HCI UART driver ver %s Copyright (C) 2000,2001 Qualcomm Inc", 
7016 +               VERSION);
7017 +       BT_INFO("Written 2000,2001 by Maxim Krasnyansky <maxk@qualcomm.com>");
7018 +
7019 +       /* Register the tty discipline */
7020 +
7021 +       memset(&hci_uart_ldisc, 0, sizeof (hci_uart_ldisc));
7022 +       hci_uart_ldisc.magic       = TTY_LDISC_MAGIC;
7023 +       hci_uart_ldisc.name        = "n_hci";
7024 +       hci_uart_ldisc.open        = hci_uart_tty_open;
7025 +       hci_uart_ldisc.close       = hci_uart_tty_close;
7026 +       hci_uart_ldisc.read        = hci_uart_tty_read;
7027 +       hci_uart_ldisc.write       = hci_uart_tty_write;
7028 +       hci_uart_ldisc.ioctl       = hci_uart_tty_ioctl;
7029 +       hci_uart_ldisc.poll        = hci_uart_tty_poll;
7030 +       hci_uart_ldisc.receive_room= hci_uart_tty_room;
7031 +       hci_uart_ldisc.receive_buf = hci_uart_tty_receive;
7032 +       hci_uart_ldisc.write_wakeup= hci_uart_tty_wakeup;
7033 +
7034 +       if ((err = tty_register_ldisc(N_HCI, &hci_uart_ldisc))) {
7035 +               BT_ERR("Can't register HCI line discipline (%d)", err);
7036 +               return err;
7037 +       }
7038 +
7039 +#ifdef CONFIG_BLUEZ_HCIUART_H4
7040 +       h4_init();
7041 +#endif
7042 +#ifdef CONFIG_BLUEZ_HCIUART_BCSP
7043 +       bcsp_init();
7044 +#endif
7045 +       
7046 +       return 0;
7047 +}
7048 +
7049 +void hci_uart_cleanup(void)
7050 +{
7051 +       int err;
7052 +
7053 +#ifdef CONFIG_BLUEZ_HCIUART_H4
7054 +       h4_deinit();
7055 +#endif
7056 +#ifdef CONFIG_BLUEZ_HCIUART_BCSP
7057 +       bcsp_deinit();
7058 +#endif
7059 +
7060 +       /* Release tty registration of line discipline */
7061 +       if ((err = tty_register_ldisc(N_HCI, NULL)))
7062 +               BT_ERR("Can't unregister HCI line discipline (%d)", err);
7063 +}
7064 +
7065 +module_init(hci_uart_init);
7066 +module_exit(hci_uart_cleanup);
7067 +
7068 +MODULE_AUTHOR("Maxim Krasnyansky <maxk@qualcomm.com>");
7069 +MODULE_DESCRIPTION("BlueZ HCI UART driver ver " VERSION);
7070 +MODULE_LICENSE("GPL");
7071 diff -urN linux-2.4.18/drivers/bluetooth/hci_uart.c linux-2.4.18-mh15/drivers/bluetooth/hci_uart.c
7072 --- linux-2.4.18/drivers/bluetooth/hci_uart.c   2001-09-07 18:28:38.000000000 +0200
7073 +++ linux-2.4.18-mh15/drivers/bluetooth/hci_uart.c      1970-01-01 01:00:00.000000000 +0100
7074 @@ -1,580 +0,0 @@
7075 -/* 
7076 -   BlueZ - Bluetooth protocol stack for Linux
7077 -   Copyright (C) 2000-2001 Qualcomm Incorporated
7078 -
7079 -   Written 2000,2001 by Maxim Krasnyansky <maxk@qualcomm.com>
7080 -
7081 -   This program is free software; you can redistribute it and/or modify
7082 -   it under the terms of the GNU General Public License version 2 as
7083 -   published by the Free Software Foundation;
7084 -
7085 -   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
7086 -   OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
7087 -   FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS.
7088 -   IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) AND AUTHOR(S) BE LIABLE FOR ANY
7089 -   CLAIM, OR ANY SPECIAL INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES 
7090 -   WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 
7091 -   ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 
7092 -   OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
7093 -
7094 -   ALL LIABILITY, INCLUDING LIABILITY FOR INFRINGEMENT OF ANY PATENTS, 
7095 -   COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS, RELATING TO USE OF THIS 
7096 -   SOFTWARE IS DISCLAIMED.
7097 -*/
7098 -
7099 -/*
7100 - * BlueZ HCI UART driver.
7101 - *
7102 - * $Id: hci_uart.c,v 1.5 2001/07/05 18:42:44 maxk Exp $    
7103 - */
7104 -#define VERSION "1.0"
7105 -
7106 -#include <linux/config.h>
7107 -#include <linux/module.h>
7108 -
7109 -#include <linux/version.h>
7110 -#include <linux/config.h>
7111 -#include <linux/kernel.h>
7112 -#include <linux/init.h>
7113 -#include <linux/sched.h>
7114 -#include <linux/types.h>
7115 -#include <linux/fcntl.h>
7116 -#include <linux/interrupt.h>
7117 -#include <linux/ptrace.h>
7118 -#include <linux/poll.h>
7119 -
7120 -#include <linux/slab.h>
7121 -#include <linux/tty.h>
7122 -#include <linux/errno.h>
7123 -#include <linux/string.h>
7124 -#include <linux/signal.h>
7125 -#include <linux/ioctl.h>
7126 -#include <linux/skbuff.h>
7127 -
7128 -#include <net/bluetooth/bluetooth.h>
7129 -#include <net/bluetooth/bluez.h>
7130 -#include <net/bluetooth/hci_core.h>
7131 -#include <net/bluetooth/hci_uart.h>
7132 -
7133 -#ifndef HCI_UART_DEBUG
7134 -#undef  DBG
7135 -#define DBG( A... )
7136 -#undef  DMP
7137 -#define DMP( A... )
7138 -#endif
7139 -
7140 -/* ------- Interface to HCI layer ------ */
7141 -/* Initialize device */
7142 -int n_hci_open(struct hci_dev *hdev)
7143 -{
7144 -       DBG("%s %p", hdev->name, hdev);
7145 -
7146 -       /* Nothing to do for UART driver */
7147 -
7148 -       hdev->flags |= HCI_RUNNING;
7149 -
7150 -       return 0;
7151 -}
7152 -
7153 -/* Reset device */
7154 -int n_hci_flush(struct hci_dev *hdev)
7155 -{
7156 -       struct n_hci *n_hci  = (struct n_hci *) hdev->driver_data;
7157 -       struct tty_struct *tty = n_hci->tty;
7158 -
7159 -       DBG("hdev %p tty %p", hdev, tty);
7160 -
7161 -       /* Drop TX queue */
7162 -       skb_queue_purge(&n_hci->txq);
7163 -
7164 -       /* Flush any pending characters in the driver and discipline. */
7165 -       if (tty->ldisc.flush_buffer)
7166 -               tty->ldisc.flush_buffer(tty);
7167 -
7168 -       if (tty->driver.flush_buffer)
7169 -               tty->driver.flush_buffer(tty);
7170 -
7171 -       return 0;
7172 -}
7173 -
7174 -/* Close device */
7175 -int n_hci_close(struct hci_dev *hdev)
7176 -{
7177 -       DBG("hdev %p", hdev);
7178 -
7179 -       hdev->flags &= ~HCI_RUNNING;
7180 -
7181 -       n_hci_flush(hdev);
7182 -
7183 -       return 0;
7184 -}
7185 -
7186 -int n_hci_tx_wakeup(struct n_hci *n_hci)
7187 -{
7188 -       register struct tty_struct *tty = n_hci->tty;
7189 -
7190 -       if (test_and_set_bit(TRANS_SENDING, &n_hci->tx_state)) {
7191 -               set_bit(TRANS_WAKEUP, &n_hci->tx_state);
7192 -               return 0;
7193 -       }
7194 -
7195 -       DBG("");
7196 -       do {
7197 -               register struct sk_buff *skb;
7198 -               register int len;
7199 -
7200 -               clear_bit(TRANS_WAKEUP, &n_hci->tx_state);
7201 -
7202 -               if (!(skb = skb_dequeue(&n_hci->txq)))
7203 -                       break;
7204 -
7205 -               DMP(skb->data, skb->len);
7206 -
7207 -               /* Send frame to TTY driver */
7208 -               tty->flags |= (1 << TTY_DO_WRITE_WAKEUP);
7209 -               len = tty->driver.write(tty, 0, skb->data, skb->len);
7210 -
7211 -               n_hci->hdev.stat.byte_tx += len;
7212 -
7213 -               DBG("sent %d", len);
7214 -
7215 -               if (len == skb->len) {
7216 -                       /* Full frame was sent */
7217 -                       kfree_skb(skb);
7218 -               } else {
7219 -                       /* Subtract sent part and requeue  */
7220 -                       skb_pull(skb, len);
7221 -                       skb_queue_head(&n_hci->txq, skb);
7222 -               }
7223 -       } while (test_bit(TRANS_WAKEUP, &n_hci->tx_state));
7224 -       clear_bit(TRANS_SENDING, &n_hci->tx_state);
7225 -
7226 -       return 0;
7227 -}
7228 -
7229 -/* Send frames from HCI layer */
7230 -int n_hci_send_frame(struct sk_buff *skb)
7231 -{
7232 -       struct hci_dev* hdev = (struct hci_dev *) skb->dev;
7233 -       struct tty_struct *tty;
7234 -       struct n_hci *n_hci;
7235 -
7236 -       if (!hdev) {
7237 -               ERR("Frame for uknown device (hdev=NULL)");
7238 -               return -ENODEV;
7239 -       }
7240 -
7241 -       if (!(hdev->flags & HCI_RUNNING))
7242 -               return -EBUSY;
7243 -
7244 -       n_hci = (struct n_hci *) hdev->driver_data;
7245 -       tty = n_hci2tty(n_hci);
7246 -
7247 -       DBG("%s: type %d len %d", hdev->name, skb->pkt_type, skb->len);
7248 -
7249 -       switch (skb->pkt_type) {
7250 -               case HCI_COMMAND_PKT:
7251 -                       hdev->stat.cmd_tx++;
7252 -                       break;
7253 -
7254 -                case HCI_ACLDATA_PKT:
7255 -                       hdev->stat.acl_tx++;
7256 -                        break;
7257 -
7258 -               case HCI_SCODATA_PKT:
7259 -                       hdev->stat.cmd_tx++;
7260 -                        break;
7261 -       };
7262 -
7263 -       /* Prepend skb with frame type and queue */
7264 -       memcpy(skb_push(skb, 1), &skb->pkt_type, 1);
7265 -       skb_queue_tail(&n_hci->txq, skb);
7266 -
7267 -       n_hci_tx_wakeup(n_hci);
7268 -
7269 -       return 0;
7270 -}
7271 -
7272 -/* ------ LDISC part ------ */
7273 -
7274 -/* n_hci_tty_open
7275 - * 
7276 - *     Called when line discipline changed to N_HCI.
7277 - *     
7278 - * Arguments:    
7279 - *     tty    pointer to tty info structure
7280 - * Return Value:    
7281 - *     0 if success, otherwise error code
7282 - */
7283 -static int n_hci_tty_open(struct tty_struct *tty)
7284 -{
7285 -       struct n_hci *n_hci = tty2n_hci(tty);
7286 -       struct hci_dev *hdev;
7287 -
7288 -       DBG("tty %p", tty);
7289 -
7290 -       if (n_hci)
7291 -               return -EEXIST;
7292 -
7293 -       if (!(n_hci = kmalloc(sizeof(struct n_hci), GFP_KERNEL))) {
7294 -               ERR("Can't allocate controll structure");
7295 -               return -ENFILE;
7296 -       }
7297 -       memset(n_hci, 0, sizeof(struct n_hci));
7298 -
7299 -       /* Initialize and register HCI device */
7300 -       hdev = &n_hci->hdev;
7301 -
7302 -       hdev->type = HCI_UART;
7303 -       hdev->driver_data = n_hci;
7304 -
7305 -       hdev->open  = n_hci_open;
7306 -       hdev->close = n_hci_close;
7307 -       hdev->flush = n_hci_flush;
7308 -       hdev->send  = n_hci_send_frame;
7309 -
7310 -       if (hci_register_dev(hdev) < 0) {
7311 -               ERR("Can't register HCI device %s", hdev->name);
7312 -               kfree(n_hci);
7313 -               return -ENODEV;
7314 -       }
7315 -
7316 -       tty->disc_data = n_hci;
7317 -       n_hci->tty = tty;
7318 -
7319 -       spin_lock_init(&n_hci->rx_lock);
7320 -       n_hci->rx_state = WAIT_PACKET_TYPE;
7321 -
7322 -       skb_queue_head_init(&n_hci->txq);
7323 -
7324 -       MOD_INC_USE_COUNT;
7325 -
7326 -       /* Flush any pending characters in the driver and discipline. */
7327 -       if (tty->ldisc.flush_buffer)
7328 -               tty->ldisc.flush_buffer(tty);
7329 -
7330 -       if (tty->driver.flush_buffer)
7331 -               tty->driver.flush_buffer(tty);
7332 -
7333 -       return 0;
7334 -}
7335 -
7336 -/* n_hci_tty_close()
7337 - *
7338 - *    Called when the line discipline is changed to something
7339 - *    else, the tty is closed, or the tty detects a hangup.
7340 - */
7341 -static void n_hci_tty_close(struct tty_struct *tty)
7342 -{
7343 -       struct n_hci *n_hci = tty2n_hci(tty);
7344 -       struct hci_dev *hdev = &n_hci->hdev;
7345 -
7346 -       DBG("tty %p hdev %p", tty, hdev);
7347 -
7348 -       if (n_hci != NULL) {
7349 -               n_hci_close(hdev);
7350 -
7351 -               if (hci_unregister_dev(hdev) < 0) {
7352 -                       ERR("Can't unregister HCI device %s",hdev->name);
7353 -               }
7354 -
7355 -               hdev->driver_data = NULL;
7356 -               tty->disc_data = NULL;
7357 -               kfree(n_hci);
7358 -
7359 -               MOD_DEC_USE_COUNT;
7360 -       }
7361 -}
7362 -
7363 -/* n_hci_tty_wakeup()
7364 - *
7365 - *    Callback for transmit wakeup. Called when low level
7366 - *    device driver can accept more send data.
7367 - *
7368 - * Arguments:        tty    pointer to associated tty instance data
7369 - * Return Value:    None
7370 - */
7371 -static void n_hci_tty_wakeup( struct tty_struct *tty )
7372 -{
7373 -       struct n_hci *n_hci = tty2n_hci(tty);
7374 -
7375 -       DBG("");
7376 -
7377 -       if (!n_hci)
7378 -               return;
7379 -
7380 -       tty->flags &= ~(1 << TTY_DO_WRITE_WAKEUP);
7381 -
7382 -       if (tty != n_hci->tty)
7383 -               return;
7384 -
7385 -       n_hci_tx_wakeup(n_hci);
7386 -}
7387 -
7388 -/* n_hci_tty_room()
7389 - * 
7390 - *    Callback function from tty driver. Return the amount of 
7391 - *    space left in the receiver's buffer to decide if remote
7392 - *    transmitter is to be throttled.
7393 - *
7394 - * Arguments:        tty    pointer to associated tty instance data
7395 - * Return Value:    number of bytes left in receive buffer
7396 - */
7397 -static int n_hci_tty_room (struct tty_struct *tty)
7398 -{
7399 -       return 65536;
7400 -}
7401 -
7402 -static inline int n_hci_check_data_len(struct n_hci *n_hci, int len)
7403 -{
7404 -       register int room = skb_tailroom(n_hci->rx_skb);
7405 -
7406 -       DBG("len %d room %d", len, room);
7407 -       if (!len) {
7408 -               DMP(n_hci->rx_skb->data, n_hci->rx_skb->len);
7409 -               hci_recv_frame(n_hci->rx_skb);
7410 -       } else if (len > room) {
7411 -               ERR("Data length is to large");
7412 -               kfree_skb(n_hci->rx_skb);
7413 -               n_hci->hdev.stat.err_rx++;
7414 -       } else {
7415 -               n_hci->rx_state = WAIT_DATA;
7416 -               n_hci->rx_count = len;
7417 -               return len;
7418 -       }
7419 -
7420 -       n_hci->rx_state = WAIT_PACKET_TYPE;
7421 -       n_hci->rx_skb   = NULL;
7422 -       n_hci->rx_count = 0;
7423 -       return 0;
7424 -}
7425 -
7426 -static inline void n_hci_rx(struct n_hci *n_hci, const __u8 * data, char *flags, int count)
7427 -{
7428 -       register const char *ptr;
7429 -       hci_event_hdr *eh;
7430 -       hci_acl_hdr   *ah;
7431 -       hci_sco_hdr   *sh;
7432 -       register int len, type, dlen;
7433 -
7434 -       DBG("count %d state %ld rx_count %ld", count, n_hci->rx_state, n_hci->rx_count);
7435 -
7436 -       n_hci->hdev.stat.byte_rx += count;
7437 -
7438 -       ptr = data;
7439 -       while (count) {
7440 -               if (n_hci->rx_count) {
7441 -                       len = MIN(n_hci->rx_count, count);
7442 -                       memcpy(skb_put(n_hci->rx_skb, len), ptr, len);
7443 -                       n_hci->rx_count -= len; count -= len; ptr += len;
7444 -
7445 -                       if (n_hci->rx_count)
7446 -                               continue;
7447 -
7448 -                       switch (n_hci->rx_state) {
7449 -                               case WAIT_DATA:
7450 -                                       DBG("Complete data");
7451 -
7452 -                                       DMP(n_hci->rx_skb->data, n_hci->rx_skb->len);
7453 -
7454 -                                       hci_recv_frame(n_hci->rx_skb);
7455 -
7456 -                                       n_hci->rx_state = WAIT_PACKET_TYPE;
7457 -                                       n_hci->rx_skb = NULL;
7458 -                                       continue;
7459 -
7460 -                               case WAIT_EVENT_HDR:
7461 -                                       eh = (hci_event_hdr *) n_hci->rx_skb->data;
7462 -
7463 -                                       DBG("Event header: evt 0x%2.2x plen %d", eh->evt, eh->plen);
7464 -
7465 -                                       n_hci_check_data_len(n_hci, eh->plen);
7466 -                                       continue;
7467 -
7468 -                               case WAIT_ACL_HDR:
7469 -                                       ah = (hci_acl_hdr *) n_hci->rx_skb->data;
7470 -                                       dlen = __le16_to_cpu(ah->dlen);
7471 -
7472 -                                       DBG("ACL header: dlen %d", dlen);
7473 -
7474 -                                       n_hci_check_data_len(n_hci, dlen);
7475 -                                       continue;
7476 -
7477 -                               case WAIT_SCO_HDR:
7478 -                                       sh = (hci_sco_hdr *) n_hci->rx_skb->data;
7479 -
7480 -                                       DBG("SCO header: dlen %d", sh->dlen);
7481 -
7482 -                                       n_hci_check_data_len(n_hci, sh->dlen);
7483 -                                       continue;
7484 -                       };
7485 -               }
7486 -
7487 -               /* WAIT_PACKET_TYPE */
7488 -               switch (*ptr) {
7489 -                       case HCI_EVENT_PKT:
7490 -                               DBG("Event packet");
7491 -                               n_hci->rx_state = WAIT_EVENT_HDR;
7492 -                               n_hci->rx_count = HCI_EVENT_HDR_SIZE;
7493 -                               type = HCI_EVENT_PKT;
7494 -                               break;
7495 -
7496 -                       case HCI_ACLDATA_PKT:
7497 -                               DBG("ACL packet");
7498 -                               n_hci->rx_state = WAIT_ACL_HDR;
7499 -                               n_hci->rx_count = HCI_ACL_HDR_SIZE;
7500 -                               type = HCI_ACLDATA_PKT;
7501 -                               break;
7502 -
7503 -                       case HCI_SCODATA_PKT:
7504 -                               DBG("SCO packet");
7505 -                               n_hci->rx_state = WAIT_SCO_HDR;
7506 -                               n_hci->rx_count = HCI_SCO_HDR_SIZE;
7507 -                               type = HCI_SCODATA_PKT;
7508 -                               break;
7509 -
7510 -                       default:
7511 -                               ERR("Unknown HCI packet type %2.2x", (__u8)*ptr);
7512 -                               n_hci->hdev.stat.err_rx++;
7513 -                               ptr++; count--;
7514 -                               continue;
7515 -               };
7516 -               ptr++; count--;
7517 -
7518 -               /* Allocate packet */
7519 -               if (!(n_hci->rx_skb = bluez_skb_alloc(HCI_MAX_FRAME_SIZE, GFP_ATOMIC))) {
7520 -                       ERR("Can't allocate mem for new packet");
7521 -
7522 -                       n_hci->rx_state = WAIT_PACKET_TYPE;
7523 -                       n_hci->rx_count = 0;
7524 -                       return;
7525 -               }
7526 -               n_hci->rx_skb->dev = (void *) &n_hci->hdev;
7527 -               n_hci->rx_skb->pkt_type = type;
7528 -       }
7529 -}
7530 -
7531 -/* n_hci_tty_receive()
7532 - * 
7533 - *     Called by tty low level driver when receive data is
7534 - *     available.
7535 - *     
7536 - * Arguments:  tty          pointer to tty isntance data
7537 - *             data         pointer to received data
7538 - *             flags        pointer to flags for data
7539 - *             count        count of received data in bytes
7540 - *     
7541 - * Return Value:    None
7542 - */
7543 -static void n_hci_tty_receive(struct tty_struct *tty, const __u8 * data, char *flags, int count)
7544 -{
7545 -       struct n_hci *n_hci = tty2n_hci(tty);
7546 -
7547 -       if (!n_hci || tty != n_hci->tty)
7548 -               return;
7549 -
7550 -       spin_lock(&n_hci->rx_lock);
7551 -       n_hci_rx(n_hci, data, flags, count);
7552 -       spin_unlock(&n_hci->rx_lock);
7553 -
7554 -       if (test_and_clear_bit(TTY_THROTTLED,&tty->flags) && tty->driver.unthrottle)
7555 -               tty->driver.unthrottle(tty);
7556 -}
7557 -
7558 -/* n_hci_tty_ioctl()
7559 - *
7560 - *    Process IOCTL system call for the tty device.
7561 - *
7562 - * Arguments:
7563 - *
7564 - *    tty        pointer to tty instance data
7565 - *    file       pointer to open file object for device
7566 - *    cmd        IOCTL command code
7567 - *    arg        argument for IOCTL call (cmd dependent)
7568 - *
7569 - * Return Value:    Command dependent
7570 - */
7571 -static int n_hci_tty_ioctl (struct tty_struct *tty, struct file * file,
7572 -                            unsigned int cmd, unsigned long arg)
7573 -{
7574 -       struct n_hci *n_hci = tty2n_hci(tty);
7575 -       int error = 0;
7576 -
7577 -       DBG("");
7578 -
7579 -       /* Verify the status of the device */
7580 -       if (!n_hci)
7581 -               return -EBADF;
7582 -
7583 -       switch (cmd) {
7584 -               default:
7585 -                       error = n_tty_ioctl(tty, file, cmd, arg);
7586 -                       break;
7587 -       };
7588 -
7589 -       return error;
7590 -}
7591 -
7592 -/*
7593 - * We don't provide read/write/poll interface for user space.
7594 - */
7595 -static ssize_t n_hci_tty_read(struct tty_struct *tty, struct file *file, unsigned char *buf, size_t nr)
7596 -{
7597 -       return 0;
7598 -}
7599 -static ssize_t n_hci_tty_write(struct tty_struct *tty, struct file *file, const unsigned char *data, size_t count)
7600 -{
7601 -       return 0;
7602 -}
7603 -static unsigned int n_hci_tty_poll(struct tty_struct *tty, struct file *filp, poll_table *wait)
7604 -{
7605 -       return 0;
7606 -}
7607 -
7608 -int __init n_hci_init(void)
7609 -{
7610 -       static struct tty_ldisc n_hci_ldisc;
7611 -       int err;
7612 -
7613 -       INF("BlueZ HCI UART driver ver %s Copyright (C) 2000,2001 Qualcomm Inc", 
7614 -               VERSION);
7615 -       INF("Written 2000,2001 by Maxim Krasnyansky <maxk@qualcomm.com>");
7616 -
7617 -       /* Register the tty discipline */
7618 -
7619 -       memset(&n_hci_ldisc, 0, sizeof (n_hci_ldisc));
7620 -       n_hci_ldisc.magic       = TTY_LDISC_MAGIC;
7621 -       n_hci_ldisc.name        = "n_hci";
7622 -       n_hci_ldisc.open        = n_hci_tty_open;
7623 -       n_hci_ldisc.close       = n_hci_tty_close;
7624 -       n_hci_ldisc.read        = n_hci_tty_read;
7625 -       n_hci_ldisc.write       = n_hci_tty_write;
7626 -       n_hci_ldisc.ioctl       = n_hci_tty_ioctl;
7627 -       n_hci_ldisc.poll        = n_hci_tty_poll;
7628 -       n_hci_ldisc.receive_room= n_hci_tty_room;
7629 -       n_hci_ldisc.receive_buf = n_hci_tty_receive;
7630 -       n_hci_ldisc.write_wakeup= n_hci_tty_wakeup;
7631 -
7632 -       if ((err = tty_register_ldisc(N_HCI, &n_hci_ldisc))) {
7633 -               ERR("Can't register HCI line discipline (%d)", err);
7634 -               return err;
7635 -       }
7636 -
7637 -       return 0;
7638 -}
7639 -
7640 -void n_hci_cleanup(void)
7641 -{
7642 -       int err;
7643 -
7644 -       /* Release tty registration of line discipline */
7645 -       if ((err = tty_register_ldisc(N_HCI, NULL)))
7646 -               ERR("Can't unregister HCI line discipline (%d)", err);
7647 -}
7648 -
7649 -module_init(n_hci_init);
7650 -module_exit(n_hci_cleanup);
7651 -
7652 -MODULE_AUTHOR("Maxim Krasnyansky <maxk@qualcomm.com>");
7653 -MODULE_DESCRIPTION("BlueZ HCI UART driver ver " VERSION);
7654 -MODULE_LICENSE("GPL");
7655 diff -urN linux-2.4.18/drivers/bluetooth/hci_uart.h linux-2.4.18-mh15/drivers/bluetooth/hci_uart.h
7656 --- linux-2.4.18/drivers/bluetooth/hci_uart.h   1970-01-01 01:00:00.000000000 +0100
7657 +++ linux-2.4.18-mh15/drivers/bluetooth/hci_uart.h      2004-08-01 16:26:23.000000000 +0200
7658 @@ -0,0 +1,82 @@
7659 +/* 
7660 +   BlueZ - Bluetooth protocol stack for Linux
7661 +   Copyright (C) 2000-2001 Qualcomm Incorporated
7662 +
7663 +   Written 2000,2001 by Maxim Krasnyansky <maxk@qualcomm.com>
7664 +
7665 +   This program is free software; you can redistribute it and/or modify
7666 +   it under the terms of the GNU General Public License version 2 as
7667 +   published by the Free Software Foundation;
7668 +
7669 +   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
7670 +   OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
7671 +   FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS.
7672 +   IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) AND AUTHOR(S) BE LIABLE FOR ANY
7673 +   CLAIM, OR ANY SPECIAL INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES 
7674 +   WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 
7675 +   ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 
7676 +   OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
7677 +
7678 +   ALL LIABILITY, INCLUDING LIABILITY FOR INFRINGEMENT OF ANY PATENTS, 
7679 +   COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS, RELATING TO USE OF THIS 
7680 +   SOFTWARE IS DISCLAIMED.
7681 +*/
7682 +
7683 +/*
7684 + * $Id: hci_uart.h,v 1.2 2002/09/09 01:17:32 maxk Exp $
7685 + */
7686 +
7687 +#ifndef N_HCI 
7688 +#define N_HCI  15
7689 +#endif
7690 +
7691 +/* Ioctls */
7692 +#define HCIUARTSETPROTO        _IOW('U', 200, int)
7693 +#define HCIUARTGETPROTO        _IOR('U', 201, int)
7694 +
7695 +/* UART protocols */
7696 +#define HCI_UART_MAX_PROTO     4
7697 +
7698 +#define HCI_UART_H4    0
7699 +#define HCI_UART_BCSP  1
7700 +#define HCI_UART_3WIRE 2
7701 +#define HCI_UART_H4DS  3
7702 +
7703 +#ifdef __KERNEL__
7704 +struct hci_uart;
7705 +
7706 +struct hci_uart_proto {
7707 +       unsigned int id;
7708 +       int (*open)(struct hci_uart *hu);
7709 +       int (*close)(struct hci_uart *hu);
7710 +       int (*flush)(struct hci_uart *hu);
7711 +       int (*recv)(struct hci_uart *hu, void *data, int len);
7712 +       int (*enqueue)(struct hci_uart *hu, struct sk_buff *skb);
7713 +       struct sk_buff *(*dequeue)(struct hci_uart *hu);
7714 +};
7715 +
7716 +struct hci_uart {
7717 +       struct tty_struct  *tty;
7718 +       struct hci_dev     hdev;
7719 +       unsigned long      flags;
7720 +
7721 +       struct hci_uart_proto *proto;
7722 +       void               *priv;
7723 +       
7724 +       struct sk_buff     *tx_skb;
7725 +       unsigned long      tx_state;
7726 +       spinlock_t         rx_lock;
7727 +};
7728 +
7729 +/* HCI_UART flag bits */
7730 +#define HCI_UART_PROTO_SET             0
7731 +
7732 +/* TX states  */
7733 +#define HCI_UART_SENDING               1
7734 +#define HCI_UART_TX_WAKEUP             2
7735 +
7736 +int hci_uart_register_proto(struct hci_uart_proto *p);
7737 +int hci_uart_unregister_proto(struct hci_uart_proto *p);
7738 +int hci_uart_tx_wakeup(struct hci_uart *hu);
7739 +
7740 +#endif /* __KERNEL__ */
7741 diff -urN linux-2.4.18/drivers/bluetooth/hci_usb.c linux-2.4.18-mh15/drivers/bluetooth/hci_usb.c
7742 --- linux-2.4.18/drivers/bluetooth/hci_usb.c    2001-09-07 18:28:38.000000000 +0200
7743 +++ linux-2.4.18-mh15/drivers/bluetooth/hci_usb.c       2004-08-01 16:26:23.000000000 +0200
7744 @@ -1,9 +1,10 @@
7745  /* 
7746 -   BlueZ - Bluetooth protocol stack for Linux
7747 +   HCI USB driver for Linux Bluetooth protocol stack (BlueZ)
7748     Copyright (C) 2000-2001 Qualcomm Incorporated
7749 -
7750     Written 2000,2001 by Maxim Krasnyansky <maxk@qualcomm.com>
7751  
7752 +   Copyright (C) 2003 Maxim Krasnyansky <maxk@qualcomm.com>
7753 +
7754     This program is free software; you can redistribute it and/or modify
7755     it under the terms of the GNU General Public License version 2 as
7756     published by the Free Software Foundation;
7757 @@ -23,598 +24,938 @@
7758  */
7759  
7760  /*
7761 - * BlueZ HCI USB driver.
7762   * Based on original USB Bluetooth driver for Linux kernel
7763   *    Copyright (c) 2000 Greg Kroah-Hartman        <greg@kroah.com>
7764   *    Copyright (c) 2000 Mark Douglas Corner       <mcorner@umich.edu>
7765   *
7766 - * $Id: hci_usb.c,v 1.5 2001/07/05 18:42:44 maxk Exp $    
7767 + * $Id: hci_usb.c,v 1.8 2002/07/18 17:23:09 maxk Exp $    
7768   */
7769 -#define VERSION "1.0"
7770 +#define VERSION "2.7"
7771  
7772  #include <linux/config.h>
7773  #include <linux/module.h>
7774  
7775  #include <linux/version.h>
7776 -#include <linux/config.h>
7777  #include <linux/kernel.h>
7778  #include <linux/init.h>
7779  #include <linux/sched.h>
7780 +#include <linux/unistd.h>
7781  #include <linux/types.h>
7782 -#include <linux/fcntl.h>
7783  #include <linux/interrupt.h>
7784 -#include <linux/ptrace.h>
7785 -#include <linux/poll.h>
7786  
7787  #include <linux/slab.h>
7788 -#include <linux/tty.h>
7789  #include <linux/errno.h>
7790  #include <linux/string.h>
7791 -#include <linux/signal.h>
7792 -#include <linux/ioctl.h>
7793  #include <linux/skbuff.h>
7794  
7795  #include <linux/usb.h>
7796  
7797  #include <net/bluetooth/bluetooth.h>
7798 -#include <net/bluetooth/bluez.h>
7799  #include <net/bluetooth/hci_core.h>
7800 -#include <net/bluetooth/hci_usb.h>
7801 +
7802 +#include "hci_usb.h"
7803  
7804  #ifndef HCI_USB_DEBUG
7805 -#undef  DBG
7806 -#define DBG( A... )
7807 -#undef  DMP
7808 -#define DMP( A... )
7809 +#undef  BT_DBG
7810 +#define BT_DBG( A... )
7811 +#undef  BT_DMP
7812 +#define BT_DMP( A... )
7813  #endif
7814  
7815 -static struct usb_device_id usb_bluetooth_ids [] = {
7816 +#ifndef CONFIG_BLUEZ_HCIUSB_ZERO_PACKET
7817 +#undef  USB_ZERO_PACKET
7818 +#define USB_ZERO_PACKET 0
7819 +#endif
7820 +
7821 +static struct usb_driver hci_usb_driver; 
7822 +
7823 +static struct usb_device_id bluetooth_ids[] = {
7824 +       /* Generic Bluetooth USB device */
7825         { USB_DEVICE_INFO(HCI_DEV_CLASS, HCI_DEV_SUBCLASS, HCI_DEV_PROTOCOL) },
7826 +
7827 +       /* AVM BlueFRITZ! USB v2.0 */
7828 +       { USB_DEVICE(0x057c, 0x3800) },
7829 +
7830 +       /* Bluetooth Ultraport Module from IBM */
7831 +       { USB_DEVICE(0x04bf, 0x030a) },
7832 +
7833 +       /* ALPS Modules with non-standard id */
7834 +       { USB_DEVICE(0x044e, 0x3001) },
7835 +       { USB_DEVICE(0x044e, 0x3002) },
7836 +
7837 +       /* Ericsson with non-standard id */
7838 +       { USB_DEVICE(0x0bdb, 0x1002) },
7839 +
7840         { }     /* Terminating entry */
7841  };
7842  
7843 -MODULE_DEVICE_TABLE (usb, usb_bluetooth_ids);
7844 +MODULE_DEVICE_TABLE (usb, bluetooth_ids);
7845  
7846 -static int hci_usb_ctrl_msg(struct hci_usb *husb,  struct sk_buff *skb);
7847 -static int hci_usb_write_msg(struct hci_usb *husb, struct sk_buff *skb);
7848 +static struct usb_device_id blacklist_ids[] = {
7849 +       /* Broadcom BCM2033 without firmware */
7850 +       { USB_DEVICE(0x0a5c, 0x2033), driver_info: HCI_IGNORE },
7851  
7852 -static void hci_usb_unlink_urbs(struct hci_usb *husb)
7853 +       /* Broadcom BCM2035 */
7854 +       { USB_DEVICE(0x0a5c, 0x200a), driver_info: HCI_RESET },
7855 +
7856 +       /* ISSC Bluetooth Adapter v3.1 */
7857 +       { USB_DEVICE(0x1131, 0x1001), driver_info: HCI_RESET },
7858 +
7859 +       /* Digianswer device */
7860 +       { USB_DEVICE(0x08fd, 0x0001), driver_info: HCI_DIGIANSWER },
7861 +
7862 +       /* RTX Telecom based adapter with buggy SCO support */
7863 +       { USB_DEVICE(0x0400, 0x0807), driver_info: HCI_BROKEN_ISOC },
7864 +
7865 +       { }     /* Terminating entry */
7866 +};
7867 +
7868 +struct _urb *_urb_alloc(int isoc, int gfp)
7869  {
7870 -       usb_unlink_urb(husb->read_urb);
7871 -       usb_unlink_urb(husb->intr_urb);
7872 -       usb_unlink_urb(husb->ctrl_urb);
7873 -       usb_unlink_urb(husb->write_urb);
7874 +       struct _urb *_urb = kmalloc(sizeof(struct _urb) +
7875 +                               sizeof(iso_packet_descriptor_t) * isoc, gfp);
7876 +       if (_urb) {
7877 +               memset(_urb, 0, sizeof(*_urb));
7878 +               spin_lock_init(&_urb->urb.lock);
7879 +       }
7880 +       return _urb;
7881 +}
7882 +
7883 +struct _urb *_urb_dequeue(struct _urb_queue *q)
7884 +{
7885 +       struct _urb *_urb = NULL;
7886 +        unsigned long flags;
7887 +        spin_lock_irqsave(&q->lock, flags);
7888 +       {
7889 +               struct list_head *head = &q->head;
7890 +               struct list_head *next = head->next;
7891 +               if (next != head) {
7892 +                       _urb = list_entry(next, struct _urb, list);
7893 +                       list_del(next); _urb->queue = NULL;
7894 +               }
7895 +       }
7896 +       spin_unlock_irqrestore(&q->lock, flags);
7897 +       return _urb;
7898  }
7899  
7900 -static void hci_usb_free_bufs(struct hci_usb *husb)
7901 +static void hci_usb_rx_complete(struct urb *urb);
7902 +static void hci_usb_tx_complete(struct urb *urb);
7903 +
7904 +#define __pending_tx(husb, type)  (&husb->pending_tx[type-1])
7905 +#define __pending_q(husb, type)   (&husb->pending_q[type-1])
7906 +#define __completed_q(husb, type) (&husb->completed_q[type-1])
7907 +#define __transmit_q(husb, type)  (&husb->transmit_q[type-1])
7908 +#define __reassembly(husb, type)  (husb->reassembly[type-1])
7909 +
7910 +static inline struct _urb *__get_completed(struct hci_usb *husb, int type)
7911  {
7912 -       if (husb->read_urb) {
7913 -               if (husb->read_urb->transfer_buffer)
7914 -                       kfree(husb->read_urb->transfer_buffer);
7915 -               usb_free_urb(husb->read_urb);
7916 -       }
7917 +       return _urb_dequeue(__completed_q(husb, type)); 
7918 +}
7919  
7920 -       if (husb->intr_urb) {
7921 -               if (husb->intr_urb->transfer_buffer)
7922 -                       kfree(husb->intr_urb->transfer_buffer);
7923 -               usb_free_urb(husb->intr_urb);
7924 +#ifdef CONFIG_BLUEZ_HCIUSB_SCO
7925 +static void __fill_isoc_desc(struct urb *urb, int len, int mtu)
7926 +{
7927 +       int offset = 0, i;
7928 +
7929 +       BT_DBG("len %d mtu %d", len, mtu);
7930 +
7931 +       for (i=0; i < HCI_MAX_ISOC_FRAMES && len >= mtu; i++, offset += mtu, len -= mtu) {
7932 +               urb->iso_frame_desc[i].offset = offset;
7933 +               urb->iso_frame_desc[i].length = mtu;
7934 +               BT_DBG("desc %d offset %d len %d", i, offset, mtu);
7935 +       }
7936 +       if (len && i < HCI_MAX_ISOC_FRAMES) {
7937 +               urb->iso_frame_desc[i].offset = offset;
7938 +               urb->iso_frame_desc[i].length = len;
7939 +               BT_DBG("desc %d offset %d len %d", i, offset, len);
7940 +               i++;
7941         }
7942 +       urb->number_of_packets = i;
7943 +}
7944 +#endif
7945  
7946 -       if (husb->ctrl_urb)
7947 -               usb_free_urb(husb->ctrl_urb);
7948 +static int hci_usb_intr_rx_submit(struct hci_usb *husb)
7949 +{
7950 +       struct _urb *_urb;
7951 +       struct urb *urb;
7952 +       int err, pipe, interval, size;
7953 +       void *buf;
7954 +
7955 +       BT_DBG("%s", husb->hdev.name);
7956 +
7957 +        size = husb->intr_in_ep->wMaxPacketSize;
7958 +
7959 +       buf = kmalloc(size, GFP_ATOMIC);
7960 +       if (!buf)
7961 +               return -ENOMEM;
7962 +
7963 +       _urb = _urb_alloc(0, GFP_ATOMIC);
7964 +       if (!_urb) {
7965 +               kfree(buf);
7966 +               return -ENOMEM;
7967 +       }
7968 +       _urb->type = HCI_EVENT_PKT;
7969 +       _urb_queue_tail(__pending_q(husb, _urb->type), _urb);
7970 +
7971 +       urb = &_urb->urb;
7972 +       pipe     = usb_rcvintpipe(husb->udev, husb->intr_in_ep->bEndpointAddress);
7973 +       interval = husb->intr_in_ep->bInterval;
7974 +       FILL_INT_URB(urb, husb->udev, pipe, buf, size, hci_usb_rx_complete, husb, interval);
7975 +       
7976 +       err = usb_submit_urb(urb);
7977 +       if (err) {
7978 +               BT_ERR("%s intr rx submit failed urb %p err %d",
7979 +                               husb->hdev.name, urb, err);
7980 +               _urb_unlink(_urb);
7981 +               _urb_free(_urb);
7982 +               kfree(buf);
7983 +       }
7984 +       return err;
7985 +}
7986  
7987 -       if (husb->write_urb)
7988 -               usb_free_urb(husb->write_urb);
7989 +static int hci_usb_bulk_rx_submit(struct hci_usb *husb)
7990 +{
7991 +       struct _urb *_urb;
7992 +       struct urb *urb;
7993 +       int err, pipe, size = HCI_MAX_FRAME_SIZE;
7994 +       void *buf;
7995 +
7996 +       buf = kmalloc(size, GFP_ATOMIC);
7997 +       if (!buf)
7998 +               return -ENOMEM;
7999 +
8000 +       _urb = _urb_alloc(0, GFP_ATOMIC);
8001 +       if (!_urb) {
8002 +               kfree(buf);
8003 +               return -ENOMEM;
8004 +       }
8005 +       _urb->type = HCI_ACLDATA_PKT;
8006 +       _urb_queue_tail(__pending_q(husb, _urb->type), _urb);
8007 +
8008 +       urb  = &_urb->urb;
8009 +       pipe = usb_rcvbulkpipe(husb->udev, husb->bulk_in_ep->bEndpointAddress);
8010 +        FILL_BULK_URB(urb, husb->udev, pipe, buf, size, hci_usb_rx_complete, husb);
8011 +        urb->transfer_flags = USB_QUEUE_BULK;
8012 +
8013 +       BT_DBG("%s urb %p", husb->hdev.name, urb);
8014 +
8015 +       err = usb_submit_urb(urb);
8016 +       if (err) {
8017 +               BT_ERR("%s bulk rx submit failed urb %p err %d",
8018 +                               husb->hdev.name, urb, err);
8019 +               _urb_unlink(_urb);
8020 +               _urb_free(_urb);
8021 +               kfree(buf);
8022 +       }
8023 +       return err;
8024 +}
8025  
8026 -       if (husb->intr_skb)
8027 -               kfree_skb(husb->intr_skb);
8028 +#ifdef CONFIG_BLUEZ_HCIUSB_SCO
8029 +static int hci_usb_isoc_rx_submit(struct hci_usb *husb)
8030 +{
8031 +       struct _urb *_urb;
8032 +       struct urb *urb;
8033 +       int err, mtu, size;
8034 +       void *buf;
8035 +
8036 +       mtu  = husb->isoc_in_ep->wMaxPacketSize;
8037 +        size = mtu * HCI_MAX_ISOC_FRAMES;
8038 +
8039 +       buf = kmalloc(size, GFP_ATOMIC);
8040 +       if (!buf)
8041 +               return -ENOMEM;
8042 +
8043 +       _urb = _urb_alloc(HCI_MAX_ISOC_FRAMES, GFP_ATOMIC);
8044 +       if (!_urb) {
8045 +               kfree(buf);
8046 +               return -ENOMEM;
8047 +       }
8048 +       _urb->type = HCI_SCODATA_PKT;
8049 +       _urb_queue_tail(__pending_q(husb, _urb->type), _urb);
8050 +
8051 +       urb = &_urb->urb;
8052 +
8053 +       urb->context  = husb;
8054 +       urb->dev      = husb->udev;
8055 +       urb->pipe     = usb_rcvisocpipe(husb->udev, husb->isoc_in_ep->bEndpointAddress);
8056 +       urb->complete = hci_usb_rx_complete;
8057 +
8058 +       urb->transfer_buffer_length = size;
8059 +       urb->transfer_buffer = buf;
8060 +       urb->transfer_flags  = USB_ISO_ASAP;
8061 +
8062 +       __fill_isoc_desc(urb, size, mtu);
8063 +
8064 +       BT_DBG("%s urb %p", husb->hdev.name, urb);
8065 +
8066 +       err = usb_submit_urb(urb);
8067 +       if (err) {
8068 +               BT_ERR("%s isoc rx submit failed urb %p err %d",
8069 +                               husb->hdev.name, urb, err);
8070 +               _urb_unlink(_urb);
8071 +               _urb_free(_urb);
8072 +               kfree(buf);
8073 +       }
8074 +       return err;
8075  }
8076 +#endif
8077  
8078 -/* ------- Interface to HCI layer ------ */
8079  /* Initialize device */
8080 -int hci_usb_open(struct hci_dev *hdev)
8081 +static int hci_usb_open(struct hci_dev *hdev)
8082  {
8083         struct hci_usb *husb = (struct hci_usb *) hdev->driver_data;
8084 -       int status;
8085 +       int i, err;
8086 +       unsigned long flags;
8087 +
8088 +       BT_DBG("%s", hdev->name);
8089  
8090 -       DBG("%s", hdev->name);
8091 +       if (test_and_set_bit(HCI_RUNNING, &hdev->flags))
8092 +               return 0;
8093  
8094 -       husb->read_urb->dev = husb->udev;
8095 -       if ((status = usb_submit_urb(husb->read_urb)))
8096 -               DBG("read submit failed. %d", status);
8097 +       MOD_INC_USE_COUNT;
8098  
8099 -       husb->intr_urb->dev = husb->udev;
8100 -       if ((status = usb_submit_urb(husb->intr_urb)))
8101 -               DBG("interrupt submit failed. %d", status);
8102 +       write_lock_irqsave(&husb->completion_lock, flags);
8103  
8104 -       hdev->flags |= HCI_RUNNING;
8105 +       err = hci_usb_intr_rx_submit(husb);
8106 +       if (!err) {
8107 +               for (i = 0; i < HCI_MAX_BULK_RX; i++)
8108 +                       hci_usb_bulk_rx_submit(husb);
8109 +
8110 +#ifdef CONFIG_BLUEZ_HCIUSB_SCO
8111 +               if (husb->isoc_iface)
8112 +                       for (i = 0; i < HCI_MAX_ISOC_RX; i++)
8113 +                               hci_usb_isoc_rx_submit(husb);
8114 +#endif
8115 +       } else {
8116 +               clear_bit(HCI_RUNNING, &hdev->flags);
8117 +               MOD_DEC_USE_COUNT;
8118 +       }
8119  
8120 -       return 0;
8121 +       write_unlock_irqrestore(&husb->completion_lock, flags);
8122 +       return err;
8123  }
8124  
8125  /* Reset device */
8126 -int hci_usb_flush(struct hci_dev *hdev)
8127 +static int hci_usb_flush(struct hci_dev *hdev)
8128  {
8129         struct hci_usb *husb = (struct hci_usb *) hdev->driver_data;
8130 +       int i;
8131  
8132 -       DBG("%s", hdev->name);
8133 -
8134 -       /* Drop TX queues */
8135 -       skb_queue_purge(&husb->tx_ctrl_q);
8136 -       skb_queue_purge(&husb->tx_write_q);
8137 +       BT_DBG("%s", hdev->name);
8138  
8139 +       for (i=0; i < 4; i++)
8140 +               skb_queue_purge(&husb->transmit_q[i]);
8141         return 0;
8142  }
8143  
8144 -/* Close device */
8145 -int hci_usb_close(struct hci_dev *hdev)
8146 +static void hci_usb_unlink_urbs(struct hci_usb *husb)
8147  {
8148 -       struct hci_usb *husb = (struct hci_usb *) hdev->driver_data;
8149 +       int i;
8150  
8151 -       DBG("%s", hdev->name);
8152 +       BT_DBG("%s", husb->hdev.name);
8153  
8154 -       hdev->flags &= ~HCI_RUNNING;
8155 -       hci_usb_unlink_urbs(husb);
8156 +       for (i=0; i < 4; i++) {
8157 +               struct _urb *_urb;
8158 +               struct urb *urb;
8159 +
8160 +               /* Kill pending requests */
8161 +               while ((_urb = _urb_dequeue(&husb->pending_q[i]))) {
8162 +                       urb = &_urb->urb;
8163 +                       BT_DBG("%s unlinking _urb %p type %d urb %p", 
8164 +                                       husb->hdev.name, _urb, _urb->type, urb);
8165 +                       usb_unlink_urb(urb);
8166 +                       _urb_queue_tail(__completed_q(husb, _urb->type), _urb);
8167 +               }
8168  
8169 -       hci_usb_flush(hdev);
8170 +               /* Release completed requests */
8171 +               while ((_urb = _urb_dequeue(&husb->completed_q[i]))) {
8172 +                       urb = &_urb->urb;
8173 +                       BT_DBG("%s freeing _urb %p type %d urb %p",
8174 +                                       husb->hdev.name, _urb, _urb->type, urb);
8175 +                       if (urb->setup_packet)
8176 +                               kfree(urb->setup_packet);
8177 +                       if (urb->transfer_buffer)
8178 +                               kfree(urb->transfer_buffer);
8179 +                       _urb_free(_urb);
8180 +               }
8181  
8182 -       return 0;
8183 +               /* Release reassembly buffers */
8184 +               if (husb->reassembly[i]) {
8185 +                       kfree_skb(husb->reassembly[i]);
8186 +                       husb->reassembly[i] = NULL;
8187 +               }
8188 +       }
8189  }
8190  
8191 -void hci_usb_ctrl_wakeup(struct hci_usb *husb)
8192 +/* Close device */
8193 +static int hci_usb_close(struct hci_dev *hdev)
8194  {
8195 -       struct sk_buff *skb;
8196 +       struct hci_usb *husb = (struct hci_usb *) hdev->driver_data;
8197 +       unsigned long flags;
8198 +       
8199 +       if (!test_and_clear_bit(HCI_RUNNING, &hdev->flags))
8200 +               return 0;
8201  
8202 -       if (test_and_set_bit(HCI_TX_CTRL, &husb->tx_state))
8203 -               return;
8204 +       BT_DBG("%s", hdev->name);
8205  
8206 -       DBG("%s", husb->hdev.name);
8207 +       write_lock_irqsave(&husb->completion_lock, flags);
8208 +       
8209 +       hci_usb_unlink_urbs(husb);
8210 +       hci_usb_flush(hdev);
8211  
8212 -       if (!(skb = skb_dequeue(&husb->tx_ctrl_q)))
8213 -               goto done;
8214 +       write_unlock_irqrestore(&husb->completion_lock, flags);
8215  
8216 -       if (hci_usb_ctrl_msg(husb, skb)){
8217 -               kfree_skb(skb);
8218 -               goto done;
8219 -       }
8220 +       MOD_DEC_USE_COUNT;
8221 +       return 0;
8222 +}
8223  
8224 -       DMP(skb->data, skb->len);
8225 +static int __tx_submit(struct hci_usb *husb, struct _urb *_urb)
8226 +{
8227 +       struct urb *urb = &_urb->urb;
8228 +       int err;
8229  
8230 -       husb->hdev.stat.byte_tx += skb->len;
8231 -       return;
8232 +       BT_DBG("%s urb %p type %d", husb->hdev.name, urb, _urb->type);
8233 +       
8234 +       _urb_queue_tail(__pending_q(husb, _urb->type), _urb);
8235 +       err = usb_submit_urb(urb);
8236 +       if (err) {
8237 +               BT_ERR("%s tx submit failed urb %p type %d err %d",
8238 +                               husb->hdev.name, urb, _urb->type, err);
8239 +               _urb_unlink(_urb);
8240 +               _urb_queue_tail(__completed_q(husb, _urb->type), _urb);
8241 +       } else
8242 +               atomic_inc(__pending_tx(husb, _urb->type));
8243  
8244 -done:
8245 -       clear_bit(HCI_TX_CTRL, &husb->tx_state);
8246 -       return;
8247 +       return err;
8248  }
8249  
8250 -void hci_usb_write_wakeup(struct hci_usb *husb)
8251 +static inline int hci_usb_send_ctrl(struct hci_usb *husb, struct sk_buff *skb)
8252  {
8253 -       struct sk_buff *skb;
8254 +       struct _urb *_urb = __get_completed(husb, skb->pkt_type);
8255 +       devrequest *dr;
8256 +       struct urb *urb;
8257 +
8258 +       if (!_urb) {
8259 +               _urb = _urb_alloc(0, GFP_ATOMIC);
8260 +               if (!_urb)
8261 +                       return -ENOMEM;
8262 +               _urb->type = skb->pkt_type;
8263 +
8264 +               dr = kmalloc(sizeof(*dr), GFP_ATOMIC);
8265 +               if (!dr) {
8266 +                       _urb_free(_urb);
8267 +                       return -ENOMEM;
8268 +               }
8269 +       } else
8270 +               dr = (void *) _urb->urb.setup_packet;
8271  
8272 -       if (test_and_set_bit(HCI_TX_WRITE, &husb->tx_state))
8273 -               return;
8274 +       dr->requesttype = husb->ctrl_req;
8275 +       dr->request = 0;
8276 +       dr->index   = 0;
8277 +       dr->value   = 0;
8278 +       dr->length  = __cpu_to_le16(skb->len);
8279  
8280 -       DBG("%s", husb->hdev.name);
8281 +       urb = &_urb->urb;
8282 +       FILL_CONTROL_URB(urb, husb->udev, usb_sndctrlpipe(husb->udev, 0),
8283 +               (void *) dr, skb->data, skb->len, hci_usb_tx_complete, husb);
8284  
8285 -       if (!(skb = skb_dequeue(&husb->tx_write_q)))
8286 -               goto done;
8287 +       BT_DBG("%s skb %p len %d", husb->hdev.name, skb, skb->len);
8288 +       
8289 +       _urb->priv = skb;
8290 +       return __tx_submit(husb, _urb);
8291 +}
8292  
8293 -       if (hci_usb_write_msg(husb, skb)) {
8294 -               skb_queue_head(&husb->tx_write_q, skb);
8295 -               goto done;
8296 +static inline int hci_usb_send_bulk(struct hci_usb *husb, struct sk_buff *skb)
8297 +{
8298 +       struct _urb *_urb = __get_completed(husb, skb->pkt_type);
8299 +       struct urb *urb;
8300 +       int pipe;
8301 +
8302 +       if (!_urb) {
8303 +               _urb = _urb_alloc(0, GFP_ATOMIC);
8304 +               if (!_urb)
8305 +                       return -ENOMEM;
8306 +               _urb->type = skb->pkt_type;
8307         }
8308  
8309 -       DMP(skb->data, skb->len);
8310 +       urb  = &_urb->urb;
8311 +       pipe = usb_sndbulkpipe(husb->udev, husb->bulk_out_ep->bEndpointAddress);
8312 +       FILL_BULK_URB(urb, husb->udev, pipe, skb->data, skb->len, 
8313 +                       hci_usb_tx_complete, husb);
8314 +       urb->transfer_flags = USB_QUEUE_BULK | USB_ZERO_PACKET;
8315  
8316 -       husb->hdev.stat.byte_tx += skb->len;
8317 -       return;
8318 +       BT_DBG("%s skb %p len %d", husb->hdev.name, skb, skb->len);
8319  
8320 -done:
8321 -       clear_bit(HCI_TX_WRITE, &husb->tx_state);
8322 -       return;
8323 +       _urb->priv = skb;
8324 +       return __tx_submit(husb, _urb);
8325  }
8326  
8327 -/* Send frames from HCI layer */
8328 -int hci_usb_send_frame(struct sk_buff *skb)
8329 +#ifdef CONFIG_BLUEZ_HCIUSB_SCO
8330 +static inline int hci_usb_send_isoc(struct hci_usb *husb, struct sk_buff *skb)
8331  {
8332 -       struct hci_dev *hdev = (struct hci_dev *) skb->dev;
8333 -       struct hci_usb *husb;
8334 -
8335 -       if (!hdev) {
8336 -               ERR("frame for uknown device (hdev=NULL)");
8337 -               return -ENODEV;
8338 +       struct _urb *_urb = __get_completed(husb, skb->pkt_type);
8339 +       struct urb *urb;
8340 +       
8341 +       if (!_urb) {
8342 +               _urb = _urb_alloc(HCI_MAX_ISOC_FRAMES, GFP_ATOMIC);
8343 +               if (!_urb)
8344 +                       return -ENOMEM;
8345 +               _urb->type = skb->pkt_type;
8346         }
8347  
8348 -       if (!(hdev->flags & HCI_RUNNING))
8349 -               return 0;
8350 +       BT_DBG("%s skb %p len %d", husb->hdev.name, skb, skb->len);
8351  
8352 -       husb = (struct hci_usb *) hdev->driver_data;
8353 +       urb = &_urb->urb;
8354 +       
8355 +       urb->context  = husb;
8356 +       urb->dev      = husb->udev;
8357 +       urb->pipe     = usb_sndisocpipe(husb->udev, husb->isoc_out_ep->bEndpointAddress);
8358 +       urb->complete = hci_usb_tx_complete;
8359 +       urb->transfer_flags = USB_ISO_ASAP;
8360  
8361 -       DBG("%s type %d len %d", hdev->name, skb->pkt_type, skb->len);
8362 +       urb->transfer_buffer = skb->data;
8363 +       urb->transfer_buffer_length = skb->len;
8364 +       
8365 +       __fill_isoc_desc(urb, skb->len, husb->isoc_out_ep->wMaxPacketSize);
8366  
8367 -       switch (skb->pkt_type) {
8368 -               case HCI_COMMAND_PKT:
8369 -                       skb_queue_tail(&husb->tx_ctrl_q, skb);
8370 -                       hci_usb_ctrl_wakeup(husb);
8371 -                       hdev->stat.cmd_tx++;
8372 -                       return 0;
8373 -
8374 -               case HCI_ACLDATA_PKT:
8375 -                       skb_queue_tail(&husb->tx_write_q, skb);
8376 -                       hci_usb_write_wakeup(husb);
8377 -                       hdev->stat.acl_tx++;
8378 -                       return 0;
8379 -
8380 -               case HCI_SCODATA_PKT:
8381 -                       return -EOPNOTSUPP;
8382 -       };
8383 -
8384 -       return 0;
8385 +       _urb->priv = skb;
8386 +       return __tx_submit(husb, _urb);
8387  }
8388 +#endif
8389  
8390 -/* ---------- USB ------------- */
8391 -
8392 -static void hci_usb_ctrl(struct urb *urb)
8393 +static void hci_usb_tx_process(struct hci_usb *husb)
8394  {
8395 -       struct sk_buff *skb = (struct sk_buff *) urb->context;
8396 -       struct hci_dev *hdev;
8397 -       struct hci_usb *husb;
8398 +       struct sk_buff_head *q;
8399 +       struct sk_buff *skb;
8400  
8401 -       if (!skb)
8402 -               return;
8403 -       hdev = (struct hci_dev *) skb->dev;
8404 -       husb = (struct hci_usb *) hdev->driver_data;
8405 +       BT_DBG("%s", husb->hdev.name);
8406  
8407 -       DBG("%s", hdev->name);
8408 +       do {
8409 +               clear_bit(HCI_USB_TX_WAKEUP, &husb->state);
8410  
8411 -       if (urb->status)
8412 -               DBG("%s ctrl status: %d", hdev->name, urb->status);
8413 +               /* Process command queue */
8414 +               q = __transmit_q(husb, HCI_COMMAND_PKT);
8415 +               if (!atomic_read(__pending_tx(husb, HCI_COMMAND_PKT)) &&
8416 +                               (skb = skb_dequeue(q))) {
8417 +                       if (hci_usb_send_ctrl(husb, skb) < 0)
8418 +                               skb_queue_head(q, skb);
8419 +               }
8420  
8421 -       clear_bit(HCI_TX_CTRL, &husb->tx_state);
8422 -       kfree_skb(skb);
8423 +#ifdef CONFIG_BLUEZ_HCIUSB_SCO
8424 +               /* Process SCO queue */
8425 +               q = __transmit_q(husb, HCI_SCODATA_PKT);
8426 +               if (atomic_read(__pending_tx(husb, HCI_SCODATA_PKT)) < HCI_MAX_ISOC_TX &&
8427 +                               (skb = skb_dequeue(q))) {
8428 +                       if (hci_usb_send_isoc(husb, skb) < 0)
8429 +                               skb_queue_head(q, skb);
8430 +               }
8431 +#endif
8432 +               
8433 +               /* Process ACL queue */
8434 +               q = __transmit_q(husb, HCI_ACLDATA_PKT);
8435 +               while (atomic_read(__pending_tx(husb, HCI_ACLDATA_PKT)) < HCI_MAX_BULK_TX &&
8436 +                               (skb = skb_dequeue(q))) {
8437 +                       if (hci_usb_send_bulk(husb, skb) < 0) {
8438 +                               skb_queue_head(q, skb);
8439 +                               break;
8440 +                       }
8441 +               }
8442 +       } while(test_bit(HCI_USB_TX_WAKEUP, &husb->state));
8443 +}
8444  
8445 -       /* Wake up device */
8446 -       hci_usb_ctrl_wakeup(husb);
8447 +static inline void hci_usb_tx_wakeup(struct hci_usb *husb)
8448 +{
8449 +       /* Serialize TX queue processing to avoid data reordering */
8450 +       if (!test_and_set_bit(HCI_USB_TX_PROCESS, &husb->state)) {
8451 +               hci_usb_tx_process(husb);
8452 +               clear_bit(HCI_USB_TX_PROCESS, &husb->state);
8453 +       } else
8454 +               set_bit(HCI_USB_TX_WAKEUP, &husb->state);
8455  }
8456  
8457 -static void hci_usb_bulk_write(struct urb *urb)
8458 +/* Send frames from HCI layer */
8459 +static int hci_usb_send_frame(struct sk_buff *skb)
8460  {
8461 -       struct sk_buff *skb = (struct sk_buff *) urb->context;
8462 -       struct hci_dev *hdev;
8463 +       struct hci_dev *hdev = (struct hci_dev *) skb->dev;
8464         struct hci_usb *husb;
8465  
8466 -       if (!skb)
8467 -               return;
8468 -       hdev = (struct hci_dev *) skb->dev;
8469 -       husb = (struct hci_usb *) hdev->driver_data;
8470 +       if (!hdev) {
8471 +               BT_ERR("frame for uknown device (hdev=NULL)");
8472 +               return -ENODEV;
8473 +       }
8474  
8475 -       DBG("%s", hdev->name);
8476 +       if (!test_bit(HCI_RUNNING, &hdev->flags))
8477 +               return -EBUSY;
8478  
8479 -       if (urb->status)
8480 -               DBG("%s bulk write status: %d", hdev->name, urb->status);
8481 +       BT_DBG("%s type %d len %d", hdev->name, skb->pkt_type, skb->len);
8482  
8483 -       clear_bit(HCI_TX_WRITE, &husb->tx_state);
8484 -       kfree_skb(skb);
8485 +       husb = (struct hci_usb *) hdev->driver_data;
8486  
8487 -       /* Wake up device */
8488 -       hci_usb_write_wakeup(husb);
8489 +       switch (skb->pkt_type) {
8490 +       case HCI_COMMAND_PKT:
8491 +               hdev->stat.cmd_tx++;
8492 +               break;
8493 +
8494 +       case HCI_ACLDATA_PKT:
8495 +               hdev->stat.acl_tx++;
8496 +               break;
8497 +
8498 +#ifdef CONFIG_BLUEZ_HCIUSB_SCO
8499 +       case HCI_SCODATA_PKT:
8500 +               hdev->stat.sco_tx++;
8501 +               break;
8502 +#endif
8503  
8504 -       return;
8505 -}
8506 +       default:
8507 +               kfree_skb(skb);
8508 +               return 0;
8509 +       }
8510  
8511 -static void hci_usb_intr(struct urb *urb)
8512 -{
8513 -       struct hci_usb *husb = (struct hci_usb *) urb->context;
8514 -       unsigned char *data = urb->transfer_buffer;
8515 -       register int count  = urb->actual_length;
8516 -       register struct sk_buff *skb = husb->intr_skb;
8517 -       hci_event_hdr *eh;
8518 -       register int len;
8519 +       read_lock(&husb->completion_lock);
8520  
8521 -       if (!husb)
8522 -               return;
8523 +       skb_queue_tail(__transmit_q(husb, skb->pkt_type), skb);
8524 +       hci_usb_tx_wakeup(husb);
8525  
8526 -       DBG("%s count %d", husb->hdev.name, count);
8527 +       read_unlock(&husb->completion_lock);
8528 +       return 0;
8529 +}
8530  
8531 -       if (urb->status || !count) {
8532 -               DBG("%s intr status %d, count %d", husb->hdev.name, urb->status, count);
8533 -               return;
8534 -       }
8535 +static inline int __recv_frame(struct hci_usb *husb, int type, void *data, int count)
8536 +{
8537 +       BT_DBG("%s type %d data %p count %d", husb->hdev.name, type, data, count);
8538  
8539 -       /* Do we really have to handle continuations here ? */
8540 -       if (!skb) {
8541 -               /* New frame */
8542 -               if (count < HCI_EVENT_HDR_SIZE) {
8543 -                       DBG("%s bad frame len %d", husb->hdev.name, count);
8544 -                       return;
8545 -               }
8546 +       husb->hdev.stat.byte_rx += count;
8547  
8548 -               eh = (hci_event_hdr *) data;
8549 -               len = eh->plen + HCI_EVENT_HDR_SIZE;
8550 +       while (count) {
8551 +               struct sk_buff *skb = __reassembly(husb, type);
8552 +               struct { int expect; } *scb;
8553 +               int len = 0;
8554 +       
8555 +               if (!skb) {
8556 +                       /* Start of the frame */
8557 +
8558 +                       switch (type) {
8559 +                       case HCI_EVENT_PKT:
8560 +                               if (count >= HCI_EVENT_HDR_SIZE) {
8561 +                                       hci_event_hdr *h = data;
8562 +                                       len = HCI_EVENT_HDR_SIZE + h->plen;
8563 +                               } else
8564 +                                       return -EILSEQ;
8565 +                               break;
8566  
8567 -               if (count > len) {
8568 -                       DBG("%s corrupted frame, len %d", husb->hdev.name, count);
8569 -                       return;
8570 -               }
8571 +                       case HCI_ACLDATA_PKT:
8572 +                               if (count >= HCI_ACL_HDR_SIZE) {
8573 +                                       hci_acl_hdr *h = data;
8574 +                                       len = HCI_ACL_HDR_SIZE + __le16_to_cpu(h->dlen);
8575 +                               } else
8576 +                                       return -EILSEQ;
8577 +                               break;
8578 +#ifdef CONFIG_BLUEZ_HCIUSB_SCO
8579 +                       case HCI_SCODATA_PKT:
8580 +                               if (count >= HCI_SCO_HDR_SIZE) {
8581 +                                       hci_sco_hdr *h = data;
8582 +                                       len = HCI_SCO_HDR_SIZE + h->dlen;
8583 +                               } else 
8584 +                                       return -EILSEQ;
8585 +                               break;
8586 +#endif
8587 +                       }
8588 +                       BT_DBG("new packet len %d", len);
8589  
8590 -               /* Allocate skb */
8591 -               if (!(skb = bluez_skb_alloc(len, GFP_ATOMIC))) {
8592 -                       ERR("Can't allocate mem for new packet");
8593 -                       return;
8594 +                       skb = bluez_skb_alloc(len, GFP_ATOMIC);
8595 +                       if (!skb) {
8596 +                               BT_ERR("%s no memory for the packet", husb->hdev.name);
8597 +                               return -ENOMEM;
8598 +                       }
8599 +                       skb->dev = (void *) &husb->hdev;
8600 +                       skb->pkt_type = type;
8601 +       
8602 +                       __reassembly(husb, type) = skb;
8603 +
8604 +                       scb = (void *) skb->cb;
8605 +                       scb->expect = len;
8606 +               } else {
8607 +                       /* Continuation */
8608 +                       scb = (void *) skb->cb;
8609 +                       len = scb->expect;
8610                 }
8611 -               skb->dev = (void *) &husb->hdev;
8612 -               skb->pkt_type = HCI_EVENT_PKT;
8613 -
8614 -               husb->intr_skb = skb;
8615 -               husb->intr_count = len;
8616 -       } else {
8617 -               /* Continuation */
8618 -               if (count > husb->intr_count) {
8619 -                       ERR("%s bad frame len %d (expected %d)", husb->hdev.name, count, husb->intr_count);
8620  
8621 -                       kfree_skb(skb);
8622 -                       husb->intr_skb = NULL;
8623 -                       husb->intr_count = 0;
8624 -                       return;
8625 +               len = min(len, count);
8626 +               
8627 +               memcpy(skb_put(skb, len), data, len);
8628 +
8629 +               scb->expect -= len;
8630 +               if (!scb->expect) {
8631 +                       /* Complete frame */
8632 +                       __reassembly(husb, type) = NULL;
8633 +                       hci_recv_frame(skb);
8634                 }
8635 -       }
8636 -
8637 -       memcpy(skb_put(skb, count), data, count);
8638 -       husb->intr_count -= count;
8639 -
8640 -       DMP(data, count);
8641 -
8642 -       if (!husb->intr_count) {
8643 -               /* Got complete frame */
8644  
8645 -               husb->hdev.stat.byte_rx += skb->len;
8646 -               hci_recv_frame(skb);
8647 -
8648 -               husb->intr_skb = NULL;
8649 +               count -= len; data += len;
8650         }
8651 +       return 0;
8652  }
8653  
8654 -static void hci_usb_bulk_read(struct urb *urb)
8655 +static void hci_usb_rx_complete(struct urb *urb)
8656  {
8657 -       struct hci_usb *husb = (struct hci_usb *) urb->context;
8658 -       unsigned char *data = urb->transfer_buffer;
8659 -       int count = urb->actual_length, status;
8660 -       struct sk_buff *skb;
8661 -       hci_acl_hdr *ah;
8662 -       register __u16 dlen;
8663 -
8664 -       if (!husb)
8665 -               return;
8666 +       struct _urb *_urb = container_of(urb, struct _urb, urb);
8667 +       struct hci_usb *husb = (void *) urb->context;
8668 +       struct hci_dev *hdev = &husb->hdev;
8669 +       int    err, count = urb->actual_length;
8670  
8671 -       DBG("%s status %d, count %d, flags %x", husb->hdev.name, urb->status, count, urb->transfer_flags);
8672 +       BT_DBG("%s urb %p type %d status %d count %d flags %x", hdev->name, urb,
8673 +                       _urb->type, urb->status, count, urb->transfer_flags);
8674  
8675 -       if (urb->status) {
8676 -               /* Do not re-submit URB on critical errors */
8677 -               switch (urb->status) {
8678 -                       case -ENOENT:
8679 -                               return;
8680 -                       default:
8681 -                               goto resubmit;
8682 -               };
8683 -       }
8684 -       if (!count)
8685 -               goto resubmit;
8686 -
8687 -       DMP(data, count);
8688 +       read_lock(&husb->completion_lock);
8689  
8690 -       ah = (hci_acl_hdr *) data;
8691 -       dlen = le16_to_cpu(ah->dlen);
8692 +       if (!test_bit(HCI_RUNNING, &hdev->flags))
8693 +               goto unlock;
8694  
8695 -       /* Verify frame len and completeness */
8696 -       if ((count - HCI_ACL_HDR_SIZE) != dlen) {
8697 -               ERR("%s corrupted ACL packet: count %d, plen %d", husb->hdev.name, count, dlen);
8698 +       if (urb->status || !count)
8699                 goto resubmit;
8700 -       }
8701  
8702 -       /* Allocate packet */
8703 -       if (!(skb = bluez_skb_alloc(count, GFP_ATOMIC))) {
8704 -               ERR("Can't allocate mem for new packet");
8705 -               goto resubmit;
8706 +       if (_urb->type == HCI_SCODATA_PKT) {
8707 +#ifdef CONFIG_BLUEZ_HCIUSB_SCO
8708 +               int i;
8709 +               for (i=0; i < urb->number_of_packets; i++) {
8710 +                       BT_DBG("desc %d status %d offset %d len %d", i,
8711 +                                       urb->iso_frame_desc[i].status,
8712 +                                       urb->iso_frame_desc[i].offset,
8713 +                                       urb->iso_frame_desc[i].actual_length);
8714 +       
8715 +                       if (!urb->iso_frame_desc[i].status)
8716 +                               __recv_frame(husb, _urb->type, 
8717 +                                       urb->transfer_buffer + urb->iso_frame_desc[i].offset,
8718 +                                       urb->iso_frame_desc[i].actual_length);
8719 +               }
8720 +#else
8721 +               ;
8722 +#endif
8723 +       } else {
8724 +               err = __recv_frame(husb, _urb->type, urb->transfer_buffer, count);
8725 +               if (err < 0) { 
8726 +                       BT_ERR("%s corrupted packet: type %d count %d",
8727 +                                       husb->hdev.name, _urb->type, count);
8728 +                       hdev->stat.err_rx++;
8729 +               }
8730         }
8731  
8732 -       memcpy(skb_put(skb, count), data, count);
8733 -       skb->dev = (void *) &husb->hdev;
8734 -       skb->pkt_type = HCI_ACLDATA_PKT;
8735 -
8736 -       husb->hdev.stat.byte_rx += skb->len;
8737 -
8738 -       hci_recv_frame(skb);
8739 -
8740  resubmit:
8741 -       husb->read_urb->dev = husb->udev;
8742 -       if ((status = usb_submit_urb(husb->read_urb)))
8743 -               DBG("%s read URB submit failed %d", husb->hdev.name, status);
8744 +       if (_urb->type != HCI_EVENT_PKT) {
8745 +               urb->dev = husb->udev;
8746 +               err      = usb_submit_urb(urb);
8747 +               BT_DBG("%s urb %p type %d resubmit status %d", hdev->name, urb,
8748 +                               _urb->type, err);
8749 +       }
8750  
8751 -       DBG("%s read URB re-submited", husb->hdev.name);
8752 +unlock:
8753 +       read_unlock(&husb->completion_lock);
8754  }
8755  
8756 -static int hci_usb_ctrl_msg(struct hci_usb *husb, struct sk_buff *skb)
8757 +static void hci_usb_tx_complete(struct urb *urb)
8758  {
8759 -       struct urb *urb = husb->ctrl_urb;
8760 -       devrequest *dr  = &husb->dev_req;
8761 -       int pipe, status;
8762 +       struct _urb *_urb = container_of(urb, struct _urb, urb);
8763 +       struct hci_usb *husb = (void *) urb->context;
8764 +       struct hci_dev *hdev = &husb->hdev;
8765  
8766 -       DBG("%s len %d", husb->hdev.name, skb->len);
8767 +       BT_DBG("%s urb %p status %d flags %x", hdev->name, urb,
8768 +                       urb->status, urb->transfer_flags);
8769  
8770 -       pipe = usb_sndctrlpipe(husb->udev, 0);
8771 +       atomic_dec(__pending_tx(husb, _urb->type));
8772  
8773 -       dr->requesttype = HCI_CTRL_REQ;
8774 -       dr->request = 0;
8775 -       dr->index   = 0;
8776 -       dr->value   = 0;
8777 -       dr->length  = cpu_to_le16(skb->len);
8778 -
8779 -       FILL_CONTROL_URB(urb, husb->udev, pipe, (void*)dr, skb->data, skb->len,
8780 -                        hci_usb_ctrl, skb);
8781 +       urb->transfer_buffer = NULL;
8782 +       kfree_skb((struct sk_buff *) _urb->priv);
8783  
8784 -       if ((status = usb_submit_urb(urb))) {
8785 -               DBG("%s control URB submit failed %d", husb->hdev.name, status);
8786 -               return status;
8787 -       }
8788 +       if (!test_bit(HCI_RUNNING, &hdev->flags))
8789 +               return;
8790  
8791 -       return 0;
8792 -}
8793 +       if (!urb->status)
8794 +               hdev->stat.byte_tx += urb->transfer_buffer_length;
8795 +       else
8796 +               hdev->stat.err_tx++;
8797  
8798 -static int hci_usb_write_msg(struct hci_usb *husb, struct sk_buff *skb)
8799 -{
8800 -       struct urb *urb = husb->write_urb;
8801 -       int pipe, status;
8802 +       read_lock(&husb->completion_lock);
8803  
8804 -       DBG("%s len %d", husb->hdev.name, skb->len);
8805 +       _urb_unlink(_urb);
8806 +       _urb_queue_tail(__completed_q(husb, _urb->type), _urb);
8807  
8808 -       pipe = usb_sndbulkpipe(husb->udev, husb->bulk_out_ep_addr);
8809 +       hci_usb_tx_wakeup(husb);
8810 +       
8811 +       read_unlock(&husb->completion_lock);
8812 +}
8813  
8814 -       FILL_BULK_URB(urb, husb->udev, pipe, skb->data, skb->len,
8815 -                     hci_usb_bulk_write, skb);
8816 -       urb->transfer_flags |= USB_QUEUE_BULK;
8817 +static void hci_usb_destruct(struct hci_dev *hdev)
8818 +{
8819 +       struct hci_usb *husb = (struct hci_usb *) hdev->driver_data;
8820  
8821 -       if ((status = usb_submit_urb(urb))) {
8822 -               DBG("%s write URB submit failed %d", husb->hdev.name, status);
8823 -               return status;
8824 -       }
8825 +       BT_DBG("%s", hdev->name);
8826  
8827 -       return 0;
8828 +       kfree(husb);
8829  }
8830  
8831 -static void * hci_usb_probe(struct usb_device *udev, unsigned int ifnum, const struct usb_device_id *id)
8832 +static void *hci_usb_probe(struct usb_device *udev, unsigned int ifnum, const struct usb_device_id *id)
8833  {
8834 -       struct usb_endpoint_descriptor *bulk_out_ep, *intr_in_ep, *bulk_in_ep;
8835 +       struct usb_endpoint_descriptor *bulk_out_ep[HCI_MAX_IFACE_NUM];
8836 +       struct usb_endpoint_descriptor *isoc_out_ep[HCI_MAX_IFACE_NUM];
8837 +       struct usb_endpoint_descriptor *bulk_in_ep[HCI_MAX_IFACE_NUM];
8838 +       struct usb_endpoint_descriptor *isoc_in_ep[HCI_MAX_IFACE_NUM];
8839 +       struct usb_endpoint_descriptor *intr_in_ep[HCI_MAX_IFACE_NUM];
8840         struct usb_interface_descriptor *uif;
8841         struct usb_endpoint_descriptor *ep;
8842 +       struct usb_interface *iface, *isoc_iface;
8843         struct hci_usb *husb;
8844         struct hci_dev *hdev;
8845 -       int i, size, pipe;
8846 -       __u8 * buf;
8847 +       int i, a, e, size, ifn, isoc_ifnum, isoc_alts;
8848  
8849 -       DBG("udev %p ifnum %d", udev, ifnum);
8850 +       BT_DBG("udev %p ifnum %d", udev, ifnum);
8851  
8852 -       /* Check device signature */
8853 -       if ((udev->descriptor.bDeviceClass    != HCI_DEV_CLASS)   ||
8854 -           (udev->descriptor.bDeviceSubClass != HCI_DEV_SUBCLASS)||
8855 -           (udev->descriptor.bDeviceProtocol != HCI_DEV_PROTOCOL) )
8856 -               return NULL;
8857 +       iface = &udev->actconfig->interface[0];
8858  
8859 -       MOD_INC_USE_COUNT;
8860 -
8861 -       uif = &udev->actconfig->interface[ifnum].altsetting[0];
8862 +       if (!id->driver_info) {
8863 +               const struct usb_device_id *match;
8864 +               match = usb_match_id(udev, iface, blacklist_ids);
8865 +               if (match)
8866 +                       id = match;
8867 +       }
8868  
8869 -       if (uif->bNumEndpoints != 3) {
8870 -               DBG("Wrong number of endpoints %d", uif->bNumEndpoints);
8871 -               MOD_DEC_USE_COUNT;
8872 +       if (id->driver_info & HCI_IGNORE)
8873                 return NULL;
8874 -       }
8875  
8876 -       bulk_out_ep = intr_in_ep = bulk_in_ep = NULL;
8877 +       /* Check number of endpoints */
8878 +       if (udev->actconfig->interface[ifnum].altsetting[0].bNumEndpoints < 3)
8879 +               return NULL;
8880  
8881 +       memset(bulk_out_ep, 0, sizeof(bulk_out_ep));
8882 +       memset(isoc_out_ep, 0, sizeof(isoc_out_ep));
8883 +       memset(bulk_in_ep,  0, sizeof(bulk_in_ep));
8884 +       memset(isoc_in_ep,  0, sizeof(isoc_in_ep));
8885 +       memset(intr_in_ep,  0, sizeof(intr_in_ep));
8886 +
8887 +       size = 0; 
8888 +       isoc_iface = NULL;
8889 +       isoc_alts  = isoc_ifnum = 0;
8890 +       
8891         /* Find endpoints that we need */
8892 -       for ( i = 0; i < uif->bNumEndpoints; ++i) {
8893 -               ep = &uif->endpoint[i];
8894  
8895 -               switch (ep->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) {
8896 -                       case USB_ENDPOINT_XFER_BULK:
8897 -                               if (ep->bEndpointAddress & USB_DIR_IN)
8898 -                                       bulk_in_ep  = ep;
8899 -                               else
8900 -                                       bulk_out_ep = ep;
8901 -                               break;
8902 +       ifn = MIN(udev->actconfig->bNumInterfaces, HCI_MAX_IFACE_NUM);
8903 +       for (i = 0; i < ifn; i++) {
8904 +               iface = &udev->actconfig->interface[i];
8905 +               for (a = 0; a < iface->num_altsetting; a++) {
8906 +                       uif = &iface->altsetting[a];
8907 +                       for (e = 0; e < uif->bNumEndpoints; e++) {
8908 +                               ep = &uif->endpoint[e];
8909 +
8910 +                               switch (ep->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) {
8911 +                               case USB_ENDPOINT_XFER_INT:
8912 +                                       if (ep->bEndpointAddress & USB_DIR_IN)
8913 +                                               intr_in_ep[i] = ep;
8914 +                                       break;
8915 +
8916 +                               case USB_ENDPOINT_XFER_BULK:
8917 +                                       if (ep->bEndpointAddress & USB_DIR_IN)
8918 +                                               bulk_in_ep[i]  = ep;
8919 +                                       else
8920 +                                               bulk_out_ep[i] = ep;
8921 +                                       break;
8922 +
8923 +#ifdef CONFIG_BLUEZ_HCIUSB_SCO
8924 +                               case USB_ENDPOINT_XFER_ISOC:
8925 +                                       if (ep->wMaxPacketSize < size || a > 2)
8926 +                                               break;
8927 +                                       size = ep->wMaxPacketSize;
8928 +
8929 +                                       isoc_iface = iface;
8930 +                                       isoc_alts  = a;
8931 +                                       isoc_ifnum = i;
8932 +
8933 +                                       if (ep->bEndpointAddress & USB_DIR_IN)
8934 +                                               isoc_in_ep[i]  = ep;
8935 +                                       else
8936 +                                               isoc_out_ep[i] = ep;
8937 +                                       break;
8938 +#endif
8939 +                               }
8940 +                       }
8941 +               }
8942 +       }
8943  
8944 -                       case USB_ENDPOINT_XFER_INT:
8945 -                               intr_in_ep = ep;
8946 -                               break;
8947 -               };
8948 +       if (!bulk_in_ep[0] || !bulk_out_ep[0] || !intr_in_ep[0]) {
8949 +               BT_DBG("Bulk endpoints not found");
8950 +               goto done;
8951         }
8952  
8953 -       if (!bulk_in_ep || !bulk_out_ep || !intr_in_ep) {
8954 -               DBG("Endpoints not found: %p %p %p", bulk_in_ep, bulk_out_ep, intr_in_ep);
8955 -               MOD_DEC_USE_COUNT;
8956 -               return NULL;
8957 +#ifdef CONFIG_BLUEZ_HCIUSB_SCO
8958 +       if (id->driver_info & HCI_BROKEN_ISOC || !isoc_in_ep[1] || !isoc_out_ep[1]) {
8959 +               BT_DBG("Isoc endpoints not found");
8960 +               isoc_iface = NULL;
8961         }
8962 +#endif
8963  
8964         if (!(husb = kmalloc(sizeof(struct hci_usb), GFP_KERNEL))) {
8965 -               ERR("Can't allocate: control structure");
8966 -               MOD_DEC_USE_COUNT;
8967 -               return NULL;
8968 +               BT_ERR("Can't allocate: control structure");
8969 +               goto done;
8970         }
8971  
8972         memset(husb, 0, sizeof(struct hci_usb));
8973  
8974         husb->udev = udev;
8975 -       husb->bulk_out_ep_addr = bulk_out_ep->bEndpointAddress;
8976 -
8977 -       if (!(husb->ctrl_urb = usb_alloc_urb(0))) {
8978 -               ERR("Can't allocate: control URB");
8979 -               goto probe_error;
8980 -       }
8981 -
8982 -       if (!(husb->write_urb = usb_alloc_urb(0))) {
8983 -               ERR("Can't allocate: write URB");
8984 -               goto probe_error;
8985 -       }
8986 -
8987 -       if (!(husb->read_urb = usb_alloc_urb(0))) {
8988 -               ERR("Can't allocate: read URB");
8989 -               goto probe_error;
8990 -       }
8991 -
8992 -       ep = bulk_in_ep;
8993 -       pipe = usb_rcvbulkpipe(udev, ep->bEndpointAddress);
8994 -       size = HCI_MAX_FRAME_SIZE;
8995 -
8996 -       if (!(buf = kmalloc(size, GFP_KERNEL))) {
8997 -               ERR("Can't allocate: read buffer");
8998 -               goto probe_error;
8999 -       }
9000 -
9001 -       FILL_BULK_URB(husb->read_urb, udev, pipe, buf, size, hci_usb_bulk_read, husb);
9002 -       husb->read_urb->transfer_flags |= USB_QUEUE_BULK;
9003 -
9004 -       ep = intr_in_ep;
9005 -       pipe = usb_rcvintpipe(udev, ep->bEndpointAddress);
9006 -       size = usb_maxpacket(udev, pipe, usb_pipeout(pipe));
9007 -
9008 -       if (!(husb->intr_urb = usb_alloc_urb(0))) {
9009 -               ERR("Can't allocate: interrupt URB");
9010 -               goto probe_error;
9011 +       husb->bulk_out_ep = bulk_out_ep[0];
9012 +       husb->bulk_in_ep  = bulk_in_ep[0];
9013 +       husb->intr_in_ep  = intr_in_ep[0];
9014 +
9015 +       if (id->driver_info & HCI_DIGIANSWER)
9016 +               husb->ctrl_req = HCI_DIGI_REQ;
9017 +       else
9018 +               husb->ctrl_req = HCI_CTRL_REQ;
9019 +
9020 +#ifdef CONFIG_BLUEZ_HCIUSB_SCO
9021 +       if (isoc_iface) {
9022 +               BT_DBG("isoc ifnum %d alts %d", isoc_ifnum, isoc_alts);
9023 +               if (usb_set_interface(udev, isoc_ifnum, isoc_alts)) {
9024 +                       BT_ERR("Can't set isoc interface settings");
9025 +                       isoc_iface = NULL;
9026 +               }
9027 +               usb_driver_claim_interface(&hci_usb_driver, isoc_iface, husb);
9028 +               husb->isoc_iface  = isoc_iface;
9029 +               husb->isoc_in_ep  = isoc_in_ep[isoc_ifnum];
9030 +               husb->isoc_out_ep = isoc_out_ep[isoc_ifnum];
9031         }
9032 +#endif
9033 +       
9034 +       husb->completion_lock = RW_LOCK_UNLOCKED;
9035  
9036 -       if (!(buf = kmalloc(size, GFP_KERNEL))) {
9037 -               ERR("Can't allocate: interrupt buffer");
9038 -               goto probe_error;
9039 +       for (i = 0; i < 4; i++) {       
9040 +               skb_queue_head_init(&husb->transmit_q[i]);
9041 +               _urb_queue_init(&husb->pending_q[i]);
9042 +               _urb_queue_init(&husb->completed_q[i]);
9043         }
9044  
9045 -       FILL_INT_URB(husb->intr_urb, udev, pipe, buf, size, hci_usb_intr, husb, ep->bInterval);
9046 -
9047 -       skb_queue_head_init(&husb->tx_ctrl_q);
9048 -       skb_queue_head_init(&husb->tx_write_q);
9049 -
9050         /* Initialize and register HCI device */
9051         hdev = &husb->hdev;
9052  
9053 -       hdev->type = HCI_USB;
9054 +       hdev->type  = HCI_USB;
9055         hdev->driver_data = husb;
9056  
9057         hdev->open  = hci_usb_open;
9058         hdev->close = hci_usb_close;
9059         hdev->flush = hci_usb_flush;
9060 -       hdev->send      = hci_usb_send_frame;
9061 +       hdev->send  = hci_usb_send_frame;
9062 +       hdev->destruct = hci_usb_destruct;
9063 +
9064 +       if (id->driver_info & HCI_RESET)
9065 +               set_bit(HCI_QUIRK_RESET_ON_INIT, &hdev->quirks);
9066  
9067         if (hci_register_dev(hdev) < 0) {
9068 -               ERR("Can't register HCI device %s", hdev->name);
9069 +               BT_ERR("Can't register HCI device");
9070                 goto probe_error;
9071         }
9072  
9073         return husb;
9074  
9075  probe_error:
9076 -       hci_usb_free_bufs(husb);
9077         kfree(husb);
9078 -       MOD_DEC_USE_COUNT;
9079 +
9080 +done:
9081         return NULL;
9082  }
9083  
9084 @@ -626,38 +967,34 @@
9085         if (!husb)
9086                 return;
9087  
9088 -       DBG("%s", hdev->name);
9089 +       BT_DBG("%s", hdev->name);
9090  
9091         hci_usb_close(hdev);
9092  
9093 -       if (hci_unregister_dev(hdev) < 0) {
9094 -               ERR("Can't unregister HCI device %s", hdev->name);
9095 -       }
9096 +       if (husb->isoc_iface)
9097 +               usb_driver_release_interface(&hci_usb_driver, husb->isoc_iface);
9098  
9099 -       hci_usb_free_bufs(husb);
9100 -       kfree(husb);
9101 -
9102 -       MOD_DEC_USE_COUNT;
9103 +       if (hci_unregister_dev(hdev) < 0)
9104 +               BT_ERR("Can't unregister HCI device %s", hdev->name);
9105  }
9106  
9107 -static struct usb_driver hci_usb_driver =
9108 -{
9109 +static struct usb_driver hci_usb_driver = {
9110         name:           "hci_usb",
9111         probe:          hci_usb_probe,
9112         disconnect:     hci_usb_disconnect,
9113 -       id_table:       usb_bluetooth_ids,
9114 +       id_table:       bluetooth_ids,
9115  };
9116  
9117  int hci_usb_init(void)
9118  {
9119         int err;
9120  
9121 -       INF("BlueZ HCI USB driver ver %s Copyright (C) 2000,2001 Qualcomm Inc",  
9122 +       BT_INFO("BlueZ HCI USB driver ver %s Copyright (C) 2000,2001 Qualcomm Inc",  
9123                 VERSION);
9124 -       INF("Written 2000,2001 by Maxim Krasnyansky <maxk@qualcomm.com>");
9125 +       BT_INFO("Written 2000,2001 by Maxim Krasnyansky <maxk@qualcomm.com>");
9126  
9127         if ((err = usb_register(&hci_usb_driver)) < 0)
9128 -               ERR("Failed to register HCI USB driver");
9129 +               BT_ERR("Failed to register HCI USB driver");
9130  
9131         return err;
9132  }
9133 @@ -670,6 +1007,6 @@
9134  module_init(hci_usb_init);
9135  module_exit(hci_usb_cleanup);
9136  
9137 -MODULE_AUTHOR("Maxim Krasnyansky <maxk@qualcomm.com>");
9138 +MODULE_AUTHOR("Maxim Krasnyansky <maxk@qualcomm.com>, Marcel Holtmann <marcel@holtmann.org>");
9139  MODULE_DESCRIPTION("BlueZ HCI USB driver ver " VERSION);
9140  MODULE_LICENSE("GPL");
9141 diff -urN linux-2.4.18/drivers/bluetooth/hci_usb.h linux-2.4.18-mh15/drivers/bluetooth/hci_usb.h
9142 --- linux-2.4.18/drivers/bluetooth/hci_usb.h    1970-01-01 01:00:00.000000000 +0100
9143 +++ linux-2.4.18-mh15/drivers/bluetooth/hci_usb.h       2004-08-01 16:26:23.000000000 +0200
9144 @@ -0,0 +1,147 @@
9145 +/* 
9146 +   HCI USB driver for Linux Bluetooth protocol stack (BlueZ)
9147 +   Copyright (C) 2000-2001 Qualcomm Incorporated
9148 +   Written 2000,2001 by Maxim Krasnyansky <maxk@qualcomm.com>
9149 +
9150 +   Copyright (C) 2003 Maxim Krasnyansky <maxk@qualcomm.com>
9151 +
9152 +   This program is free software; you can redistribute it and/or modify
9153 +   it under the terms of the GNU General Public License version 2 as
9154 +   published by the Free Software Foundation;
9155 +
9156 +   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
9157 +   OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
9158 +   FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS.
9159 +   IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) AND AUTHOR(S) BE LIABLE FOR ANY
9160 +   CLAIM, OR ANY SPECIAL INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES 
9161 +   WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 
9162 +   ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 
9163 +   OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
9164 +
9165 +   ALL LIABILITY, INCLUDING LIABILITY FOR INFRINGEMENT OF ANY PATENTS, 
9166 +   COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS, RELATING TO USE OF THIS 
9167 +   SOFTWARE IS DISCLAIMED.
9168 +*/
9169 +
9170 +/*
9171 + * $Id: hci_usb.h,v 1.2 2002/03/18 19:10:04 maxk Exp $
9172 + */
9173 +
9174 +#ifdef __KERNEL__
9175 +
9176 +/* Class, SubClass, and Protocol codes that describe a Bluetooth device */
9177 +#define HCI_DEV_CLASS        0xe0      /* Wireless class */
9178 +#define HCI_DEV_SUBCLASS     0x01      /* RF subclass */
9179 +#define HCI_DEV_PROTOCOL     0x01      /* Bluetooth programming protocol */
9180 +
9181 +#define HCI_CTRL_REQ        0x20
9182 +#define HCI_DIGI_REQ        0x40
9183 +
9184 +#define HCI_IGNORE           0x01
9185 +#define HCI_RESET            0x02
9186 +#define HCI_DIGIANSWER       0x04
9187 +#define HCI_BROKEN_ISOC      0x08
9188 +
9189 +#define HCI_MAX_IFACE_NUM      3 
9190 +
9191 +#define HCI_MAX_BULK_TX        4
9192 +#define HCI_MAX_BULK_RX        1
9193 +
9194 +#define HCI_MAX_ISOC_RX                2
9195 +#define HCI_MAX_ISOC_TX                2
9196 +
9197 +#define HCI_MAX_ISOC_FRAMES     10
9198 +
9199 +struct _urb_queue {
9200 +       struct list_head head;
9201 +       spinlock_t       lock;
9202 +};
9203 +
9204 +struct _urb {
9205 +       struct list_head  list;
9206 +       struct _urb_queue *queue;
9207 +       int               type;
9208 +       void              *priv;
9209 +       struct urb        urb;
9210 +};
9211 +
9212 +struct _urb *_urb_alloc(int isoc, int gfp);
9213 +
9214 +static inline void _urb_free(struct _urb *_urb)
9215 +{
9216 +       kfree(_urb);
9217 +}
9218 +
9219 +static inline void _urb_queue_init(struct _urb_queue *q)
9220 +{
9221 +       INIT_LIST_HEAD(&q->head);
9222 +       spin_lock_init(&q->lock);
9223 +}
9224 +
9225 +static inline void _urb_queue_head(struct _urb_queue *q, struct _urb *_urb)
9226 +{
9227 +        unsigned long flags;
9228 +        spin_lock_irqsave(&q->lock, flags);
9229 +       list_add(&_urb->list, &q->head); _urb->queue = q;
9230 +       spin_unlock_irqrestore(&q->lock, flags);
9231 +}
9232 +
9233 +static inline void _urb_queue_tail(struct _urb_queue *q, struct _urb *_urb)
9234 +{
9235 +        unsigned long flags;
9236 +        spin_lock_irqsave(&q->lock, flags);
9237 +       list_add_tail(&_urb->list, &q->head); _urb->queue = q;
9238 +       spin_unlock_irqrestore(&q->lock, flags);
9239 +}
9240 +
9241 +static inline void _urb_unlink(struct _urb *_urb)
9242 +{
9243 +       struct _urb_queue *q = _urb->queue;
9244 +        unsigned long flags;
9245 +       if (q) {
9246 +               spin_lock_irqsave(&q->lock, flags);
9247 +               list_del(&_urb->list); _urb->queue = NULL;
9248 +               spin_unlock_irqrestore(&q->lock, flags);
9249 +       }
9250 +}
9251 +
9252 +struct _urb *_urb_dequeue(struct _urb_queue *q);
9253 +
9254 +#ifndef container_of
9255 +#define container_of(ptr, type, member) ({                      \
9256 +                       const typeof( ((type *)0)->member ) *__mptr = (ptr);    \
9257 +                               (type *)( (char *)__mptr - offsetof(type,member) );})
9258 +#endif
9259 +
9260 +struct hci_usb {
9261 +       struct hci_dev          hdev;
9262 +
9263 +       unsigned long           state;
9264 +       
9265 +       struct usb_device       *udev;
9266 +       
9267 +       struct usb_endpoint_descriptor  *bulk_in_ep;
9268 +       struct usb_endpoint_descriptor  *bulk_out_ep;
9269 +       struct usb_endpoint_descriptor  *intr_in_ep;
9270 +
9271 +       struct usb_interface            *isoc_iface;
9272 +       struct usb_endpoint_descriptor  *isoc_out_ep;
9273 +       struct usb_endpoint_descriptor  *isoc_in_ep;
9274 +
9275 +       __u8                    ctrl_req;
9276 +
9277 +       struct sk_buff_head     transmit_q[4];
9278 +       struct sk_buff          *reassembly[4]; // Reassembly buffers
9279 +
9280 +       rwlock_t                completion_lock;
9281 +
9282 +       atomic_t                pending_tx[4];  // Number of pending requests 
9283 +       struct _urb_queue       pending_q[4];   // Pending requests
9284 +       struct _urb_queue       completed_q[4]; // Completed requests
9285 +};
9286 +
9287 +/* States  */
9288 +#define HCI_USB_TX_PROCESS     1
9289 +#define HCI_USB_TX_WAKEUP      2
9290 +
9291 +#endif /* __KERNEL__ */
9292 diff -urN linux-2.4.18/drivers/bluetooth/hci_vhci.c linux-2.4.18-mh15/drivers/bluetooth/hci_vhci.c
9293 --- linux-2.4.18/drivers/bluetooth/hci_vhci.c   2001-09-07 18:28:38.000000000 +0200
9294 +++ linux-2.4.18-mh15/drivers/bluetooth/hci_vhci.c      2004-08-01 16:26:23.000000000 +0200
9295 @@ -25,9 +25,9 @@
9296  /*
9297   * BlueZ HCI virtual device driver.
9298   *
9299 - * $Id: hci_vhci.c,v 1.3 2001/08/03 04:19:50 maxk Exp $ 
9300 + * $Id: hci_vhci.c,v 1.3 2002/04/17 17:37:20 maxk Exp $ 
9301   */
9302 -#define VERSION "1.0"
9303 +#define VERSION "1.1"
9304  
9305  #include <linux/config.h>
9306  #include <linux/module.h>
9307 @@ -49,43 +49,56 @@
9308  #include <asm/uaccess.h>
9309  
9310  #include <net/bluetooth/bluetooth.h>
9311 -#include <net/bluetooth/bluez.h>
9312  #include <net/bluetooth/hci_core.h>
9313 -#include <net/bluetooth/hci_vhci.h>
9314 +#include "hci_vhci.h"
9315  
9316  /* HCI device part */
9317  
9318 -int hci_vhci_open(struct hci_dev *hdev)
9319 +static int hci_vhci_open(struct hci_dev *hdev)
9320  {
9321 -       hdev->flags |= HCI_RUNNING;
9322 +       set_bit(HCI_RUNNING, &hdev->flags);
9323         return 0;
9324  }
9325  
9326 -int hci_vhci_flush(struct hci_dev *hdev)
9327 +static int hci_vhci_flush(struct hci_dev *hdev)
9328  {
9329         struct hci_vhci_struct *hci_vhci = (struct hci_vhci_struct *) hdev->driver_data;
9330         skb_queue_purge(&hci_vhci->readq);
9331         return 0;
9332  }
9333  
9334 -int hci_vhci_close(struct hci_dev *hdev)
9335 +static int hci_vhci_close(struct hci_dev *hdev)
9336  {
9337 -       hdev->flags &= ~HCI_RUNNING;
9338 +       if (!test_and_clear_bit(HCI_RUNNING, &hdev->flags))
9339 +               return 0;
9340 +
9341         hci_vhci_flush(hdev);
9342         return 0;
9343  }
9344  
9345 -int hci_vhci_send_frame(struct sk_buff *skb)
9346 +static void hci_vhci_destruct(struct hci_dev *hdev)
9347 +{
9348 +       struct hci_vhci_struct *vhci;
9349 +
9350 +       if (!hdev) return;
9351 +
9352 +       vhci = (struct hci_vhci_struct *) hdev->driver_data;
9353 +       kfree(vhci);
9354 +
9355 +       MOD_DEC_USE_COUNT;
9356 +}
9357 +
9358 +static int hci_vhci_send_frame(struct sk_buff *skb)
9359  {
9360         struct hci_dev* hdev = (struct hci_dev *) skb->dev;
9361         struct hci_vhci_struct *hci_vhci;
9362  
9363         if (!hdev) {
9364 -               ERR("Frame for uknown device (hdev=NULL)");
9365 +               BT_ERR("Frame for uknown device (hdev=NULL)");
9366                 return -ENODEV;
9367         }
9368  
9369 -       if (!(hdev->flags & HCI_RUNNING))
9370 +       if (!test_bit(HCI_RUNNING, &hdev->flags))
9371                 return -EBUSY;
9372  
9373         hci_vhci = (struct hci_vhci_struct *) hdev->driver_data;
9374 @@ -188,7 +201,7 @@
9375  
9376         add_wait_queue(&hci_vhci->read_wait, &wait);
9377         while (count) {
9378 -               current->state = TASK_INTERRUPTIBLE;
9379 +               set_current_state(TASK_INTERRUPTIBLE);
9380  
9381                 /* Read frames from device queue */
9382                 if (!(skb = skb_dequeue(&hci_vhci->readq))) {
9383 @@ -214,8 +227,7 @@
9384                 kfree_skb(skb);
9385                 break;
9386         }
9387 -
9388 -       current->state = TASK_RUNNING;
9389 +       set_current_state(TASK_RUNNING);
9390         remove_wait_queue(&hci_vhci->read_wait, &wait);
9391  
9392         return ret;
9393 @@ -270,11 +282,13 @@
9394         hdev->close = hci_vhci_close;
9395         hdev->flush = hci_vhci_flush;
9396         hdev->send  = hci_vhci_send_frame;
9397 +       hdev->destruct = hci_vhci_destruct;
9398  
9399         if (hci_register_dev(hdev) < 0) {
9400                 kfree(hci_vhci);
9401                 return -EBUSY;
9402         }
9403 +       MOD_INC_USE_COUNT;
9404  
9405         file->private_data = hci_vhci;
9406         return 0;   
9407 @@ -285,12 +299,10 @@
9408         struct hci_vhci_struct *hci_vhci = (struct hci_vhci_struct *) file->private_data;
9409  
9410         if (hci_unregister_dev(&hci_vhci->hdev) < 0) {
9411 -               ERR("Can't unregister HCI device %s", hci_vhci->hdev.name);
9412 +               BT_ERR("Can't unregister HCI device %s", hci_vhci->hdev.name);
9413         }
9414  
9415 -       kfree(hci_vhci);
9416         file->private_data = NULL;
9417 -
9418         return 0;
9419  }
9420  
9421 @@ -315,12 +327,12 @@
9422  
9423  int __init hci_vhci_init(void)
9424  {
9425 -       INF("BlueZ VHCI driver ver %s Copyright (C) 2000,2001 Qualcomm Inc",  
9426 +       BT_INFO("BlueZ VHCI driver ver %s Copyright (C) 2000,2001 Qualcomm Inc",  
9427                 VERSION);
9428 -       INF("Written 2000,2001 by Maxim Krasnyansky <maxk@qualcomm.com>");
9429 +       BT_INFO("Written 2000,2001 by Maxim Krasnyansky <maxk@qualcomm.com>");
9430  
9431         if (misc_register(&hci_vhci_miscdev)) {
9432 -               ERR("Can't register misc device %d\n", VHCI_MINOR);
9433 +               BT_ERR("Can't register misc device %d\n", VHCI_MINOR);
9434                 return -EIO;
9435         }
9436  
9437 @@ -337,4 +349,4 @@
9438  
9439  MODULE_AUTHOR("Maxim Krasnyansky <maxk@qualcomm.com>");
9440  MODULE_DESCRIPTION("BlueZ VHCI driver ver " VERSION);
9441 -MODULE_LICENSE("GPL");
9442 +MODULE_LICENSE("GPL"); 
9443 diff -urN linux-2.4.18/drivers/bluetooth/hci_vhci.h linux-2.4.18-mh15/drivers/bluetooth/hci_vhci.h
9444 --- linux-2.4.18/drivers/bluetooth/hci_vhci.h   1970-01-01 01:00:00.000000000 +0100
9445 +++ linux-2.4.18-mh15/drivers/bluetooth/hci_vhci.h      2004-08-01 16:26:23.000000000 +0200
9446 @@ -0,0 +1,50 @@
9447 +/* 
9448 +   BlueZ - Bluetooth protocol stack for Linux
9449 +   Copyright (C) 2000-2001 Qualcomm Incorporated
9450 +
9451 +   Written 2000,2001 by Maxim Krasnyansky <maxk@qualcomm.com>
9452 +
9453 +   This program is free software; you can redistribute it and/or modify
9454 +   it under the terms of the GNU General Public License version 2 as
9455 +   published by the Free Software Foundation;
9456 +
9457 +   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
9458 +   OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
9459 +   FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS.
9460 +   IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) AND AUTHOR(S) BE LIABLE FOR ANY
9461 +   CLAIM, OR ANY SPECIAL INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES 
9462 +   WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 
9463 +   ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 
9464 +   OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
9465 +
9466 +   ALL LIABILITY, INCLUDING LIABILITY FOR INFRINGEMENT OF ANY PATENTS, 
9467 +   COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS, RELATING TO USE OF THIS 
9468 +   SOFTWARE IS DISCLAIMED.
9469 +*/
9470 +
9471 +/*
9472 + * $Id: hci_vhci.h,v 1.1.1.1 2002/03/08 21:03:15 maxk Exp $
9473 + */
9474 +
9475 +#ifndef __HCI_VHCI_H
9476 +#define __HCI_VHCI_H
9477 +
9478 +#ifdef __KERNEL__
9479 +
9480 +struct hci_vhci_struct {
9481 +       struct hci_dev       hdev;
9482 +       __u32                flags;
9483 +       wait_queue_head_t    read_wait;
9484 +       struct sk_buff_head  readq;
9485 +       struct fasync_struct *fasync;
9486 +};
9487 +
9488 +/* VHCI device flags */
9489 +#define VHCI_FASYNC            0x0010
9490 +
9491 +#endif /* __KERNEL__ */
9492 +
9493 +#define VHCI_DEV       "/dev/vhci"
9494 +#define VHCI_MINOR     250
9495 +
9496 +#endif /* __HCI_VHCI_H */
9497 diff -urN linux-2.4.18/drivers/bluetooth/Makefile linux-2.4.18-mh15/drivers/bluetooth/Makefile
9498 --- linux-2.4.18/drivers/bluetooth/Makefile     2001-09-07 18:28:38.000000000 +0200
9499 +++ linux-2.4.18-mh15/drivers/bluetooth/Makefile        2004-08-01 16:26:23.000000000 +0200
9500 @@ -1,11 +1,27 @@
9501  #
9502 -# Makefile for Bluetooth HCI device drivers.
9503 +# Makefile for the Linux Bluetooth HCI device drivers
9504  #
9505  
9506  O_TARGET       := bluetooth.o
9507  
9508 +list-multi     := hci_uart.o
9509 +
9510  obj-$(CONFIG_BLUEZ_HCIUSB)     += hci_usb.o
9511 -obj-$(CONFIG_BLUEZ_HCIUART)    += hci_uart.o
9512  obj-$(CONFIG_BLUEZ_HCIVHCI)    += hci_vhci.o
9513  
9514 +obj-$(CONFIG_BLUEZ_HCIUART)            += hci_uart.o
9515 +uart-y                                 := hci_ldisc.o
9516 +uart-$(CONFIG_BLUEZ_HCIUART_H4)                += hci_h4.o
9517 +uart-$(CONFIG_BLUEZ_HCIUART_BCSP)      += hci_bcsp.o
9518 +
9519 +obj-$(CONFIG_BLUEZ_HCIBFUSB)   += bfusb.o
9520 +
9521 +obj-$(CONFIG_BLUEZ_HCIDTL1)    += dtl1_cs.o
9522 +obj-$(CONFIG_BLUEZ_HCIBT3C)    += bt3c_cs.o
9523 +obj-$(CONFIG_BLUEZ_HCIBLUECARD)        += bluecard_cs.o
9524 +obj-$(CONFIG_BLUEZ_HCIBTUART)  += btuart_cs.o
9525 +
9526  include $(TOPDIR)/Rules.make
9527 +
9528 +hci_uart.o: $(uart-y)
9529 +       $(LD) -r -o $@ $(uart-y)
9530 diff -urN linux-2.4.18/drivers/bluetooth/Makefile.lib linux-2.4.18-mh15/drivers/bluetooth/Makefile.lib
9531 --- linux-2.4.18/drivers/bluetooth/Makefile.lib 1970-01-01 01:00:00.000000000 +0100
9532 +++ linux-2.4.18-mh15/drivers/bluetooth/Makefile.lib    2004-08-01 16:26:23.000000000 +0200
9533 @@ -0,0 +1,2 @@
9534 +obj-$(CONFIG_BLUEZ_HCIBFUSB)   += firmware_class.o
9535 +obj-$(CONFIG_BLUEZ_HCIBT3C)    += firmware_class.o
9536 diff -urN linux-2.4.18/drivers/char/pcmcia/serial_cs.c linux-2.4.18-mh15/drivers/char/pcmcia/serial_cs.c
9537 --- linux-2.4.18/drivers/char/pcmcia/serial_cs.c        2001-12-21 18:41:54.000000000 +0100
9538 +++ linux-2.4.18-mh15/drivers/char/pcmcia/serial_cs.c   2004-08-01 16:26:23.000000000 +0200
9539 @@ -2,7 +2,7 @@
9540  
9541      A driver for PCMCIA serial devices
9542  
9543 -    serial_cs.c 1.128 2001/10/18 12:18:35
9544 +    serial_cs.c 1.138 2002/10/25 06:24:52
9545  
9546      The contents of this file are subject to the Mozilla Public
9547      License Version 1.1 (the "License"); you may not use this file
9548 @@ -69,14 +69,14 @@
9549  static int irq_list[4] = { -1 };
9550  MODULE_PARM(irq_list, "1-4i");
9551  
9552 -/* Enable the speaker? */
9553 -INT_MODULE_PARM(do_sound, 1);
9554 +INT_MODULE_PARM(do_sound, 1);          /* Enable the speaker? */
9555 +INT_MODULE_PARM(buggy_uart, 0);                /* Skip strict UART tests? */
9556  
9557  #ifdef PCMCIA_DEBUG
9558  INT_MODULE_PARM(pc_debug, PCMCIA_DEBUG);
9559  #define DEBUG(n, args...) if (pc_debug>(n)) printk(KERN_DEBUG args)
9560  static char *version =
9561 -"serial_cs.c 1.128 2001/10/18 12:18:35 (David Hinds)";
9562 +"serial_cs.c 1.138 2002/10/25 06:24:52 (David Hinds)";
9563  #else
9564  #define DEBUG(n, args...)
9565  #endif
9566 @@ -95,6 +95,7 @@
9567      { MANFID_OMEGA, PRODID_OMEGA_QSP_100, 4 },
9568      { MANFID_QUATECH, PRODID_QUATECH_DUAL_RS232, 2 },
9569      { MANFID_QUATECH, PRODID_QUATECH_DUAL_RS232_D1, 2 },
9570 +    { MANFID_QUATECH, PRODID_QUATECH_DUAL_RS232_D2, 2 },
9571      { MANFID_QUATECH, PRODID_QUATECH_QUAD_RS232, 4 },
9572      { MANFID_QUATECH, PRODID_QUATECH_DUAL_RS422, 2 },
9573      { MANFID_QUATECH, PRODID_QUATECH_QUAD_RS422, 4 },
9574 @@ -148,7 +149,7 @@
9575      client_reg_t client_reg;
9576      dev_link_t *link;
9577      int i, ret;
9578 -    
9579 +
9580      DEBUG(0, "serial_attach()\n");
9581  
9582      /* Create new serial device */
9583 @@ -160,7 +161,7 @@
9584      link->release.function = &serial_release;
9585      link->release.data = (u_long)link;
9586      link->io.Attributes1 = IO_DATA_PATH_WIDTH_8;
9587 -    link->io.NumPorts1 = 8;
9588 +    link->io.Attributes2 = IO_DATA_PATH_WIDTH_8;
9589      link->irq.Attributes = IRQ_TYPE_EXCLUSIVE;
9590      link->irq.IRQInfo1 = IRQ_INFO2_VALID|IRQ_LEVEL_ID;
9591      if (irq_list[0] == -1)
9592 @@ -169,13 +170,12 @@
9593         for (i = 0; i < 4; i++)
9594             link->irq.IRQInfo2 |= 1 << irq_list[i];
9595      link->conf.Attributes = CONF_ENABLE_IRQ;
9596 -    link->conf.Vcc = 50;
9597      if (do_sound) {
9598         link->conf.Attributes |= CONF_ENABLE_SPKR;
9599         link->conf.Status = CCSR_AUDIO_ENA;
9600      }
9601      link->conf.IntType = INT_MEMORY_AND_IO;
9602 -    
9603 +
9604      /* Register with Card Services */
9605      link->next = dev_list;
9606      dev_list = link;
9607 @@ -194,7 +194,7 @@
9608         serial_detach(link);
9609         return NULL;
9610      }
9611 -    
9612 +
9613      return link;
9614  } /* serial_attach */
9615  
9616 @@ -214,7 +214,7 @@
9617      int ret;
9618  
9619      DEBUG(0, "serial_detach(0x%p)\n", link);
9620 -    
9621 +
9622      /* Locate device structure */
9623      for (linkp = &dev_list; *linkp; linkp = &(*linkp)->next)
9624         if (*linkp == link) break;
9625 @@ -224,17 +224,17 @@
9626      del_timer(&link->release);
9627      if (link->state & DEV_CONFIG)
9628         serial_release((u_long)link);
9629 -    
9630 +
9631      if (link->handle) {
9632         ret = CardServices(DeregisterClient, link->handle);
9633         if (ret != CS_SUCCESS)
9634             cs_error(link->handle, DeregisterClient, ret);
9635      }
9636 -    
9637 +
9638      /* Unlink device structure, free bits */
9639      *linkp = link->next;
9640      kfree(info);
9641 -    
9642 +
9643  } /* serial_detach */
9644  
9645  /*====================================================================*/
9646 @@ -243,18 +243,20 @@
9647  {
9648      struct serial_struct serial;
9649      int line;
9650 -    
9651 +
9652      memset(&serial, 0, sizeof(serial));
9653      serial.port = port;
9654      serial.irq = irq;
9655      serial.flags = ASYNC_SKIP_TEST | ASYNC_SHARE_IRQ;
9656 +    if (buggy_uart)
9657 +       serial.flags |= ASYNC_BUGGY_UART;
9658      line = register_serial(&serial);
9659      if (line < 0) {
9660         printk(KERN_NOTICE "serial_cs: register_serial() at 0x%04lx,"
9661                " irq %d failed\n", (u_long)serial.port, serial.irq);
9662         return -1;
9663      }
9664 -    
9665 +
9666      info->line[info->ndev] = line;
9667      sprintf(info->node[info->ndev].dev_name, "ttyS%d", line);
9668      info->node[info->ndev].major = TTY_MAJOR;
9669 @@ -262,7 +264,7 @@
9670      if (info->ndev > 0)
9671         info->node[info->ndev-1].next = &info->node[info->ndev];
9672      info->ndev++;
9673 -    
9674 +
9675      return 0;
9676  }
9677  
9678 @@ -313,7 +315,10 @@
9679             return setup_serial(info, port, config.AssignedIRQ);
9680      }
9681      link->conf.Vcc = config.Vcc;
9682 -    
9683 +
9684 +    link->io.NumPorts1 = 8;
9685 +    link->io.NumPorts2 = 0;
9686 +
9687      /* First pass: look for a config entry that looks normal. */
9688      tuple.TupleData = (cisdata_t *)buf;
9689      tuple.TupleOffset = 0; tuple.TupleDataMax = 255;
9690 @@ -340,7 +345,7 @@
9691             i = next_tuple(handle, &tuple, &parse);
9692         }
9693      }
9694 -    
9695 +
9696      /* Second pass: try to find an entry that isn't picky about
9697         its base address, then try to grab any standard serial port
9698         address, and finally try to get any free port. */
9699 @@ -352,8 +357,7 @@
9700             for (j = 0; j < 5; j++) {
9701                 link->io.BasePort1 = base[j];
9702                 link->io.IOAddrLines = base[j] ? 16 : 3;
9703 -               i = CardServices(RequestIO, link->handle,
9704 -                                &link->io);
9705 +               i = CardServices(RequestIO, link->handle, &link->io);
9706                 if (i == CS_SUCCESS) goto found_port;
9707             }
9708         }
9709 @@ -365,7 +369,7 @@
9710         cs_error(link->handle, RequestIO, i);
9711         return -1;
9712      }
9713 -    
9714 +
9715      i = CardServices(RequestIRQ, link->handle, &link->irq);
9716      if (i != CS_SUCCESS) {
9717         cs_error(link->handle, RequestIRQ, i);
9718 @@ -390,8 +394,12 @@
9719      u_char buf[256];
9720      cisparse_t parse;
9721      cistpl_cftable_entry_t *cf = &parse.cftable_entry;
9722 +    config_info_t config;
9723      int i, base2 = 0;
9724  
9725 +    CardServices(GetConfigurationInfo, handle, &config);
9726 +    link->conf.Vcc = config.Vcc;
9727 +
9728      tuple.TupleData = (cisdata_t *)buf;
9729      tuple.TupleOffset = 0; tuple.TupleDataMax = 255;
9730      tuple.Attributes = 0;
9731 @@ -433,12 +441,12 @@
9732             i = next_tuple(handle, &tuple, &parse);
9733         }
9734      }
9735 -    
9736 +
9737      if (i != CS_SUCCESS) {
9738 -       cs_error(link->handle, RequestIO, i);
9739 -       return -1;
9740 +       /* At worst, try to configure as a single port */
9741 +       return simple_config(link);
9742      }
9743 -    
9744 +
9745      i = CardServices(RequestIRQ, link->handle, &link->irq);
9746      if (i != CS_SUCCESS) {
9747         cs_error(link->handle, RequestIRQ, i);
9748 @@ -454,14 +462,27 @@
9749         cs_error(link->handle, RequestConfiguration, i);
9750         return -1;
9751      }
9752 -    
9753 +
9754 +    /* The Oxford Semiconductor OXCF950 cards are in fact single-port:
9755 +       8 registers are for the UART, the others are extra registers */
9756 +    if (info->manfid == MANFID_OXSEMI) {
9757 +       if (cf->index == 1 || cf->index == 3) {
9758 +           setup_serial(info, base2, link->irq.AssignedIRQ);
9759 +           outb(12,link->io.BasePort1+1);
9760 +       } else {
9761 +           setup_serial(info, link->io.BasePort1, link->irq.AssignedIRQ);
9762 +           outb(12,base2+1);
9763 +       }
9764 +       return 0;
9765 +    }
9766 +
9767      setup_serial(info, link->io.BasePort1, link->irq.AssignedIRQ);
9768      /* The Nokia cards are not really multiport cards */
9769      if (info->manfid == MANFID_NOKIA)
9770         return 0;
9771      for (i = 0; i < info->multi-1; i++)
9772         setup_serial(info, base2+(8*i), link->irq.AssignedIRQ);
9773 -    
9774 +
9775      return 0;
9776  }
9777  
9778 @@ -500,7 +521,7 @@
9779      }
9780      link->conf.ConfigBase = parse.config.base;
9781      link->conf.Present = parse.config.rmask[0];
9782 -    
9783 +
9784      /* Configure card */
9785      link->state |= DEV_CONFIG;
9786  
9787 @@ -508,8 +529,8 @@
9788      tuple.DesiredTuple = CISTPL_LONGLINK_MFC;
9789      tuple.Attributes = TUPLE_RETURN_COMMON | TUPLE_RETURN_LINK;
9790      info->multi = (first_tuple(handle, &tuple, &parse) == CS_SUCCESS);
9791 -    
9792 -    /* Is this a multiport card? */
9793 +
9794 +    /* Scan list of known multiport card ID's */
9795      tuple.DesiredTuple = CISTPL_MANFID;
9796      if (first_tuple(handle, &tuple, &parse) == CS_SUCCESS) {
9797         info->manfid = le16_to_cpu(buf[0]);
9798 @@ -537,15 +558,15 @@
9799                 info->multi = 2;
9800         }
9801      }
9802 -    
9803 +
9804      if (info->multi > 1)
9805         multi_config(link);
9806      else
9807         simple_config(link);
9808 -    
9809 +
9810      if (info->ndev == 0)
9811         goto failed;
9812 -    
9813 +
9814      if (info->manfid == MANFID_IBM) {
9815         conf_reg_t reg = { 0, CS_READ, 0x800, 0 };
9816         CS_CHECK(AccessConfigurationRegister, link->handle, &reg);
9817 @@ -562,6 +583,7 @@
9818      cs_error(link->handle, last_fn, last_ret);
9819  failed:
9820      serial_release((u_long)link);
9821 +    link->state &= ~DEV_CONFIG_PENDING;
9822  
9823  } /* serial_config */
9824  
9825 @@ -569,7 +591,7 @@
9826  
9827      After a card is removed, serial_release() will unregister the net
9828      device, and release the PCMCIA configuration.
9829 -    
9830 +
9831  ======================================================================*/
9832  
9833  void serial_release(u_long arg)
9834 @@ -577,7 +599,7 @@
9835      dev_link_t *link = (dev_link_t *)arg;
9836      serial_info_t *info = link->priv;
9837      int i;
9838 -    
9839 +
9840      DEBUG(0, "serial_release(0x%p)\n", link);
9841  
9842      for (i = 0; i < info->ndev; i++) {
9843 @@ -590,7 +612,7 @@
9844         CardServices(ReleaseIO, link->handle, &link->io);
9845         CardServices(ReleaseIRQ, link->handle, &link->irq);
9846      }
9847 -    
9848 +
9849      link->state &= ~DEV_CONFIG;
9850  
9851  } /* serial_release */
9852 @@ -601,7 +623,7 @@
9853      stuff to run after an event is received.  A CARD_REMOVAL event
9854      also sets some flags to discourage the serial drivers from
9855      talking to the ports.
9856 -    
9857 +
9858  ======================================================================*/
9859  
9860  static int serial_event(event_t event, int priority,
9861 @@ -609,9 +631,9 @@
9862  {
9863      dev_link_t *link = args->client_data;
9864      serial_info_t *info = link->priv;
9865 -    
9866 +
9867      DEBUG(1, "serial_event(0x%06x)\n", event);
9868 -    
9869 +
9870      switch (event) {
9871      case CS_EVENT_CARD_REMOVAL:
9872         link->state &= ~DEV_PRESENT;
9873 @@ -650,7 +672,7 @@
9874      if (serv.Revision != CS_RELEASE_CODE) {
9875         printk(KERN_NOTICE "serial_cs: Card Services release "
9876                "does not match!\n");
9877 -       return -1;
9878 +       return -EINVAL;
9879      }
9880      register_pccard_driver(&dev_info, &serial_attach, &serial_detach);
9881      return 0;
9882 diff -urN linux-2.4.18/drivers/input/Config.in linux-2.4.18-mh15/drivers/input/Config.in
9883 --- linux-2.4.18/drivers/input/Config.in        2001-09-13 00:34:06.000000000 +0200
9884 +++ linux-2.4.18-mh15/drivers/input/Config.in   2004-08-01 16:26:23.000000000 +0200
9885 @@ -14,5 +14,6 @@
9886  fi
9887  dep_tristate '  Joystick support' CONFIG_INPUT_JOYDEV $CONFIG_INPUT
9888  dep_tristate '  Event interface support' CONFIG_INPUT_EVDEV $CONFIG_INPUT
9889 +dep_tristate '  User level driver support' CONFIG_INPUT_UINPUT $CONFIG_INPUT
9890  
9891  endmenu
9892 diff -urN linux-2.4.18/drivers/input/keybdev.c linux-2.4.18-mh15/drivers/input/keybdev.c
9893 --- linux-2.4.18/drivers/input/keybdev.c        2001-10-11 18:14:32.000000000 +0200
9894 +++ linux-2.4.18-mh15/drivers/input/keybdev.c   2004-08-01 16:26:23.000000000 +0200
9895 @@ -154,16 +154,18 @@
9896  
9897  static struct input_handler keybdev_handler;
9898  
9899 +static unsigned int ledstate = 0xff;
9900 +
9901  void keybdev_ledfunc(unsigned int led)
9902  {
9903         struct input_handle *handle;    
9904  
9905 -       for (handle = keybdev_handler.handle; handle; handle = handle->hnext) {
9906 +       ledstate = led;
9907  
9908 +       for (handle = keybdev_handler.handle; handle; handle = handle->hnext) {
9909                 input_event(handle->dev, EV_LED, LED_SCROLLL, !!(led & 0x01));
9910                 input_event(handle->dev, EV_LED, LED_NUML,    !!(led & 0x02));
9911                 input_event(handle->dev, EV_LED, LED_CAPSL,   !!(led & 0x04));
9912 -
9913         }
9914  }
9915  
9916 @@ -202,6 +204,12 @@
9917  
9918  //     printk(KERN_INFO "keybdev.c: Adding keyboard: input%d\n", dev->number);
9919  
9920 +       if (ledstate != 0xff) {
9921 +               input_event(dev, EV_LED, LED_SCROLLL, !!(ledstate & 0x01));
9922 +               input_event(dev, EV_LED, LED_NUML,    !!(ledstate & 0x02));
9923 +               input_event(dev, EV_LED, LED_CAPSL,   !!(ledstate & 0x04));
9924 +       }
9925 +
9926         return handle;
9927  }
9928  
9929 diff -urN linux-2.4.18/drivers/input/Makefile linux-2.4.18-mh15/drivers/input/Makefile
9930 --- linux-2.4.18/drivers/input/Makefile 2000-12-29 23:07:22.000000000 +0100
9931 +++ linux-2.4.18-mh15/drivers/input/Makefile    2004-08-01 16:26:23.000000000 +0200
9932 @@ -24,6 +24,7 @@
9933  obj-$(CONFIG_INPUT_MOUSEDEV)   += mousedev.o
9934  obj-$(CONFIG_INPUT_JOYDEV)     += joydev.o
9935  obj-$(CONFIG_INPUT_EVDEV)      += evdev.o
9936 +obj-$(CONFIG_INPUT_UINPUT)     += uinput.o
9937  
9938  # The global Rules.make.
9939  
9940 diff -urN linux-2.4.18/drivers/input/uinput.c linux-2.4.18-mh15/drivers/input/uinput.c
9941 --- linux-2.4.18/drivers/input/uinput.c 1970-01-01 01:00:00.000000000 +0100
9942 +++ linux-2.4.18-mh15/drivers/input/uinput.c    2004-08-01 16:26:23.000000000 +0200
9943 @@ -0,0 +1,428 @@
9944 +/*
9945 + *  User level driver support for input subsystem
9946 + *
9947 + * Heavily based on evdev.c by Vojtech Pavlik
9948 + *
9949 + * This program is free software; you can redistribute it and/or modify
9950 + * it under the terms of the GNU General Public License as published by
9951 + * the Free Software Foundation; either version 2 of the License, or
9952 + * (at your option) any later version.
9953 + *
9954 + * This program is distributed in the hope that it will be useful,
9955 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
9956 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
9957 + * GNU General Public License for more details.
9958 + *
9959 + * You should have received a copy of the GNU General Public License
9960 + * along with this program; if not, write to the Free Software
9961 + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
9962 + *
9963 + * Author: Aristeu Sergio Rozanski Filho <aris@cathedrallabs.org>
9964 + * 
9965 + * Changes/Revisions:
9966 + *     0.1     20/06/2002
9967 + *             - first public version
9968 + */
9969 +
9970 +#include <linux/poll.h>
9971 +#include <linux/slab.h>
9972 +#include <linux/module.h>
9973 +#include <linux/init.h>
9974 +#include <linux/input.h>
9975 +#include <linux/smp_lock.h>
9976 +#include <linux/fs.h>
9977 +#include <linux/miscdevice.h>
9978 +#include <linux/uinput.h>
9979 +
9980 +static int uinput_dev_open(struct input_dev *dev)
9981 +{
9982 +       return 0;
9983 +}
9984 +
9985 +static void uinput_dev_close(struct input_dev *dev)
9986 +{
9987 +}
9988 +
9989 +static int uinput_dev_event(struct input_dev *dev, unsigned int type, unsigned int code, int value)
9990 +{
9991 +       struct uinput_device    *udev;
9992 +
9993 +       udev = (struct uinput_device *)dev->private;
9994 +
9995 +       udev->buff[udev->head].type = type;
9996 +       udev->buff[udev->head].code = code;
9997 +       udev->buff[udev->head].value = value;
9998 +       do_gettimeofday(&udev->buff[udev->head].time);
9999 +       udev->head = (udev->head + 1) % UINPUT_BUFFER_SIZE;
10000 +
10001 +       wake_up_interruptible(&udev->waitq);
10002 +
10003 +       return 0;
10004 +}
10005 +
10006 +static int uinput_dev_upload_effect(struct input_dev *dev, struct ff_effect *effect)
10007 +{
10008 +       return 0;
10009 +}
10010 +
10011 +static int uinput_dev_erase_effect(struct input_dev *dev, int effect_id)
10012 +{
10013 +       return 0;
10014 +}                                      
10015 +
10016 +static int uinput_create_device(struct uinput_device *udev)
10017 +{
10018 +       if (!udev->dev->name) {
10019 +               printk(KERN_DEBUG "%s: write device info first\n", UINPUT_NAME);
10020 +               return -EINVAL;
10021 +       }
10022 +
10023 +       udev->dev->open = uinput_dev_open;
10024 +       udev->dev->close = uinput_dev_close;
10025 +       udev->dev->event = uinput_dev_event;
10026 +       udev->dev->upload_effect = uinput_dev_upload_effect;
10027 +       udev->dev->erase_effect = uinput_dev_erase_effect;
10028 +       udev->dev->private = udev;
10029 +
10030 +       init_waitqueue_head(&(udev->waitq));
10031 +
10032 +       input_register_device(udev->dev);
10033 +
10034 +       set_bit(UIST_CREATED, &(udev->state));
10035 +
10036 +       return 0;
10037 +}
10038 +
10039 +static int uinput_destroy_device(struct uinput_device *udev)
10040 +{
10041 +       if (!test_bit(UIST_CREATED, &(udev->state))) {
10042 +               printk(KERN_WARNING "%s: create the device first\n", UINPUT_NAME);
10043 +               return -EINVAL;
10044 +       }
10045 +
10046 +       input_unregister_device(udev->dev);
10047 +
10048 +       clear_bit(UIST_CREATED, &(udev->state));
10049 +
10050 +       return 0;
10051 +}
10052 +
10053 +static int uinput_open(struct inode *inode, struct file *file)
10054 +{
10055 +       struct uinput_device    *newdev;
10056 +       struct input_dev        *newinput;
10057 +
10058 +       newdev = kmalloc(sizeof(struct uinput_device), GFP_KERNEL);
10059 +       if (!newdev)
10060 +               goto error;
10061 +       memset(newdev, 0, sizeof(struct uinput_device));
10062 +
10063 +       newinput = kmalloc(sizeof(struct input_dev), GFP_KERNEL);
10064 +       if (!newinput)
10065 +               goto cleanup;
10066 +       memset(newinput, 0, sizeof(struct input_dev));
10067 +
10068 +       newdev->dev = newinput;
10069 +       
10070 +       file->private_data = newdev;
10071 +
10072 +       return 0;
10073 +cleanup:
10074 +       kfree(newdev);
10075 +error:
10076 +       return -ENOMEM;
10077 +}
10078 +
10079 +static int uinput_validate_absbits(struct input_dev *dev)
10080 +{
10081 +       unsigned int cnt;
10082 +       int retval = 0;
10083 +       
10084 +       for (cnt = 0; cnt < ABS_MAX; cnt++) {
10085 +               if (!test_bit(cnt, dev->absbit)) 
10086 +                       continue;
10087 +               
10088 +               if (/*!dev->absmin[cnt] || !dev->absmax[cnt] || */
10089 +                   (dev->absmax[cnt] <= dev->absmin[cnt])) {
10090 +                       printk(KERN_DEBUG 
10091 +                               "%s: invalid abs[%02x] min:%d max:%d\n",
10092 +                               UINPUT_NAME, cnt, 
10093 +                               dev->absmin[cnt], dev->absmax[cnt]);
10094 +                       retval = -EINVAL;
10095 +                       break;
10096 +               }
10097 +
10098 +               if ((dev->absflat[cnt] < dev->absmin[cnt]) ||
10099 +                   (dev->absflat[cnt] > dev->absmax[cnt])) {
10100 +                       printk(KERN_DEBUG 
10101 +                               "%s: absflat[%02x] out of range: %d "
10102 +                               "(min:%d/max:%d)\n",
10103 +                               UINPUT_NAME, cnt, dev->absflat[cnt],
10104 +                               dev->absmin[cnt], dev->absmax[cnt]);
10105 +                       retval = -EINVAL;
10106 +                       break;
10107 +               }
10108 +       }
10109 +       return retval;
10110 +}
10111 +
10112 +static int uinput_alloc_device(struct file *file, const char *buffer, size_t count)
10113 +{
10114 +       struct uinput_user_dev  *user_dev;
10115 +       struct input_dev        *dev;
10116 +       struct uinput_device    *udev;
10117 +       int                     size,
10118 +                               retval;
10119 +
10120 +       retval = count;
10121 +
10122 +       udev = (struct uinput_device *)file->private_data;
10123 +       dev = udev->dev;
10124 +
10125 +       user_dev = kmalloc(sizeof(*user_dev), GFP_KERNEL);
10126 +       if (!user_dev) {
10127 +               retval = -ENOMEM;
10128 +               goto exit;
10129 +       }
10130 +
10131 +       if (copy_from_user(user_dev, buffer, sizeof(struct uinput_user_dev))) {
10132 +               retval = -EFAULT;
10133 +               goto exit;
10134 +       }
10135 +
10136 +       if (NULL != dev->name) 
10137 +               kfree(dev->name);
10138 +
10139 +       size = strnlen(user_dev->name, UINPUT_MAX_NAME_SIZE) + 1;
10140 +       dev->name = kmalloc(size, GFP_KERNEL);
10141 +       if (!dev->name) {
10142 +               retval = -ENOMEM;
10143 +               goto exit;
10144 +       }
10145 +
10146 +       strncpy(dev->name, user_dev->name, size);
10147 +       dev->idbus      = user_dev->idbus;
10148 +       dev->idvendor   = user_dev->idvendor;
10149 +       dev->idproduct  = user_dev->idproduct;
10150 +       dev->idversion  = user_dev->idversion;
10151 +       dev->ff_effects_max = user_dev->ff_effects_max;
10152 +
10153 +       size = sizeof(int) * (ABS_MAX + 1);
10154 +       memcpy(dev->absmax, user_dev->absmax, size);
10155 +       memcpy(dev->absmin, user_dev->absmin, size);
10156 +       memcpy(dev->absfuzz, user_dev->absfuzz, size);
10157 +       memcpy(dev->absflat, user_dev->absflat, size);
10158 +
10159 +       /* check if absmin/absmax/absfuzz/absflat are filled as
10160 +        * told in Documentation/input/input-programming.txt */
10161 +       if (test_bit(EV_ABS, dev->evbit)) {
10162 +               retval = uinput_validate_absbits(dev);
10163 +               if (retval < 0)
10164 +                       kfree(dev->name);
10165 +       }
10166 +
10167 +exit:
10168 +       kfree(user_dev);
10169 +       return retval;
10170 +}
10171 +
10172 +static ssize_t uinput_write(struct file *file, const char *buffer, size_t count, loff_t *ppos)
10173 +{
10174 +       struct uinput_device    *udev = file->private_data;
10175 +       
10176 +       if (test_bit(UIST_CREATED, &(udev->state))) {
10177 +               struct input_event      ev;
10178 +
10179 +               if (copy_from_user(&ev, buffer, sizeof(struct input_event)))
10180 +                       return -EFAULT;
10181 +               input_event(udev->dev, ev.type, ev.code, ev.value);
10182 +       }
10183 +       else
10184 +               count = uinput_alloc_device(file, buffer, count);
10185 +
10186 +       return count;
10187 +}
10188 +
10189 +static ssize_t uinput_read(struct file *file, char *buffer, size_t count, loff_t *ppos)
10190 +{
10191 +       struct uinput_device *udev = file->private_data;
10192 +       int retval = 0;
10193 +       
10194 +       if (!test_bit(UIST_CREATED, &(udev->state)))
10195 +               return -ENODEV;
10196 +
10197 +       if ((udev->head == udev->tail) && (file->f_flags & O_NONBLOCK))
10198 +               return -EAGAIN;
10199 +
10200 +       retval = wait_event_interruptible(udev->waitq,
10201 +                       (udev->head != udev->tail) || 
10202 +                       !test_bit(UIST_CREATED, &(udev->state)));
10203 +       
10204 +       if (retval)
10205 +               return retval;
10206 +
10207 +       if (!test_bit(UIST_CREATED, &(udev->state)))
10208 +               return -ENODEV;
10209 +
10210 +       while ((udev->head != udev->tail) && 
10211 +           (retval + sizeof(struct input_event) <= count)) {
10212 +               if (copy_to_user(buffer + retval, &(udev->buff[udev->tail]),
10213 +                   sizeof(struct input_event))) return -EFAULT;
10214 +               udev->tail = (udev->tail + 1) % UINPUT_BUFFER_SIZE;
10215 +               retval += sizeof(struct input_event);
10216 +       }
10217 +
10218 +       return retval;
10219 +}
10220 +
10221 +static unsigned int uinput_poll(struct file *file, poll_table *wait)
10222 +{
10223 +       struct uinput_device *udev = file->private_data;
10224 +
10225 +       poll_wait(file, &udev->waitq, wait);
10226 +
10227 +       if (udev->head != udev->tail)
10228 +               return POLLIN | POLLRDNORM;
10229 +
10230 +       return 0;                       
10231 +}
10232 +
10233 +static int uinput_burn_device(struct uinput_device *udev)
10234 +{
10235 +       if (test_bit(UIST_CREATED, &(udev->state)))
10236 +               uinput_destroy_device(udev);
10237 +
10238 +       kfree(udev->dev);
10239 +       kfree(udev);
10240 +
10241 +       return 0;
10242 +}
10243 +
10244 +static int uinput_close(struct inode *inode, struct file *file)
10245 +{
10246 +       return uinput_burn_device((struct uinput_device *)file->private_data);
10247 +}
10248 +
10249 +static int uinput_ioctl(struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg)
10250 +{
10251 +       int                     retval = 0;
10252 +       struct uinput_device    *udev;
10253 +
10254 +       udev = (struct uinput_device *)file->private_data;
10255 +
10256 +       /* device attributes can not be changed after the device is created */
10257 +       if (cmd >= UI_SET_EVBIT && test_bit(UIST_CREATED, &(udev->state)))
10258 +               return -EINVAL;
10259 +
10260 +       switch (cmd) {
10261 +               case UI_DEV_CREATE:
10262 +                       retval = uinput_create_device(udev);
10263 +                       break;
10264 +                       
10265 +               case UI_DEV_DESTROY:
10266 +                       retval = uinput_destroy_device(udev);
10267 +                       break;
10268 +
10269 +               case UI_SET_EVBIT:
10270 +                       if (arg > EV_MAX) {
10271 +                               retval = -EINVAL;
10272 +                               break;
10273 +                       }
10274 +                       set_bit(arg, udev->dev->evbit);
10275 +                       break;
10276 +                       
10277 +               case UI_SET_KEYBIT:
10278 +                       if (arg > KEY_MAX) {
10279 +                               retval = -EINVAL;
10280 +                               break;
10281 +                       }
10282 +                       set_bit(arg, udev->dev->keybit);
10283 +                       break;
10284 +                       
10285 +               case UI_SET_RELBIT:
10286 +                       if (arg > REL_MAX) {
10287 +                               retval = -EINVAL;
10288 +                               break;
10289 +                       }
10290 +                       set_bit(arg, udev->dev->relbit);
10291 +                       break;
10292 +                       
10293 +               case UI_SET_ABSBIT:
10294 +                       if (arg > ABS_MAX) {
10295 +                               retval = -EINVAL;
10296 +                               break;
10297 +                       }
10298 +                       set_bit(arg, udev->dev->absbit);
10299 +                       break;
10300 +                       
10301 +               case UI_SET_MSCBIT:
10302 +                       if (arg > MSC_MAX) {
10303 +                               retval = -EINVAL;
10304 +                               break;
10305 +                       }
10306 +                       set_bit(arg, udev->dev->mscbit);
10307 +                       break;
10308 +                       
10309 +               case UI_SET_LEDBIT:
10310 +                       if (arg > LED_MAX) {
10311 +                               retval = -EINVAL;
10312 +                               break;
10313 +                       }
10314 +                       set_bit(arg, udev->dev->ledbit);
10315 +                       break;
10316 +                       
10317 +               case UI_SET_SNDBIT:
10318 +                       if (arg > SND_MAX) {
10319 +                               retval = -EINVAL;
10320 +                               break;
10321 +                       }
10322 +                       set_bit(arg, udev->dev->sndbit);
10323 +                       break;
10324 +                       
10325 +               case UI_SET_FFBIT:
10326 +                       if (arg > FF_MAX) {
10327 +                               retval = -EINVAL;
10328 +                               break;
10329 +                       }
10330 +                       set_bit(arg, udev->dev->ffbit);
10331 +                       break;
10332 +                       
10333 +               default:
10334 +                       retval = -EFAULT;
10335 +       }
10336 +       return retval;
10337 +}
10338 +
10339 +struct file_operations uinput_fops = {
10340 +       owner:          THIS_MODULE,
10341 +       open:           uinput_open,
10342 +       release:        uinput_close,
10343 +       read:           uinput_read,
10344 +       write:          uinput_write,
10345 +       poll:           uinput_poll,
10346 +       ioctl:          uinput_ioctl,
10347 +};
10348 +
10349 +static struct miscdevice uinput_misc = {
10350 +       fops:           &uinput_fops,
10351 +       minor:          UINPUT_MINOR,
10352 +       name:           UINPUT_NAME,
10353 +};
10354 +
10355 +static int __init uinput_init(void)
10356 +{
10357 +       return misc_register(&uinput_misc);
10358 +}
10359 +
10360 +static void __exit uinput_exit(void)
10361 +{
10362 +       misc_deregister(&uinput_misc);
10363 +}
10364 +
10365 +MODULE_AUTHOR("Aristeu Sergio Rozanski Filho");
10366 +MODULE_DESCRIPTION("User level driver support for input subsystem");
10367 +MODULE_LICENSE("GPL");
10368 +
10369 +module_init(uinput_init);
10370 +module_exit(uinput_exit);
10371 +
10372 diff -urN linux-2.4.18/drivers/isdn/avmb1/capidrv.c linux-2.4.18-mh15/drivers/isdn/avmb1/capidrv.c
10373 --- linux-2.4.18/drivers/isdn/avmb1/capidrv.c   2001-12-21 18:41:54.000000000 +0100
10374 +++ linux-2.4.18-mh15/drivers/isdn/avmb1/capidrv.c      2004-08-01 16:26:23.000000000 +0200
10375 @@ -514,13 +514,25 @@
10376  
10377  static void send_message(capidrv_contr * card, _cmsg * cmsg)
10378  {
10379 -       struct sk_buff *skb;
10380 -       size_t len;
10381 +       struct sk_buff  *skb;
10382 +       size_t          len;
10383 +       u16             err;
10384 +
10385         capi_cmsg2message(cmsg, cmsg->buf);
10386         len = CAPIMSG_LEN(cmsg->buf);
10387         skb = alloc_skb(len, GFP_ATOMIC);
10388 +       if(!skb) {
10389 +               printk(KERN_ERR "no skb len(%d) memory\n", len);
10390 +               return;
10391 +       }
10392         memcpy(skb_put(skb, len), cmsg->buf, len);
10393 -       (*capifuncs->capi_put_message) (global.appid, skb);
10394 +       err = (*capifuncs->capi_put_message) (global.appid, skb);
10395 +       if (err) {
10396 +               printk(KERN_WARNING "%s: capi_put_message error: %04x\n",
10397 +                       __FUNCTION__, err);
10398 +               kfree_skb(skb);
10399 +               return;
10400 +       }
10401         global.nsentctlpkt++;
10402  }
10403  
10404 @@ -2179,10 +2191,10 @@
10405                         free_ncci(card, card->bchans[card->nbchan-1].nccip);
10406                 if (card->bchans[card->nbchan-1].plcip)
10407                         free_plci(card, card->bchans[card->nbchan-1].plcip);
10408 -               if (card->plci_list)
10409 -                       printk(KERN_ERR "capidrv: bug in free_plci()\n");
10410                 card->nbchan--;
10411         }
10412 +       if (card->plci_list)
10413 +               printk(KERN_ERR "capidrv: bug in free_plci()\n");
10414         kfree(card->bchans);
10415         card->bchans = 0;
10416  
10417 diff -urN linux-2.4.18/drivers/isdn/avmb1/kcapi.c linux-2.4.18-mh15/drivers/isdn/avmb1/kcapi.c
10418 --- linux-2.4.18/drivers/isdn/avmb1/kcapi.c     2001-12-21 18:41:54.000000000 +0100
10419 +++ linux-2.4.18-mh15/drivers/isdn/avmb1/kcapi.c        2004-08-01 16:26:23.000000000 +0200
10420 @@ -545,7 +545,13 @@
10421  static void notify_up(__u32 contr)
10422  {
10423         struct capi_interface_user *p;
10424 +       __u16 appl;
10425  
10426 +       for (appl = 1; appl <= CAPI_MAXAPPL; appl++) {
10427 +               if (!VALID_APPLID(appl)) continue;
10428 +               if (APPL(appl)->releasing) continue;
10429 +               CARD(contr)->driver->register_appl(CARD(contr), appl, &APPL(appl)->rparam);
10430 +       }
10431          printk(KERN_NOTICE "kcapi: notify up contr %d\n", contr);
10432         spin_lock(&capi_users_lock);
10433         for (p = capi_users; p; p = p->next) {
10434 @@ -705,12 +711,16 @@
10435                         nextpp = &(*pp)->next;
10436                 }
10437         }
10438 -       APPL(appl)->releasing--;
10439 -       if (APPL(appl)->releasing <= 0) {
10440 -               APPL(appl)->signal = 0;
10441 -               APPL_MARK_FREE(appl);
10442 -               printk(KERN_INFO "kcapi: appl %d down\n", appl);
10443 -       }
10444 +       if (APPL(appl)->releasing) { /* only release if the application was marked for release */
10445 +               printk(KERN_DEBUG "kcapi: appl %d releasing(%d)\n", appl, APPL(appl)->releasing);
10446 +               APPL(appl)->releasing--;
10447 +               if (APPL(appl)->releasing <= 0) {
10448 +                       APPL(appl)->signal = 0;
10449 +                       APPL_MARK_FREE(appl);
10450 +                       printk(KERN_INFO "kcapi: appl %d down\n", appl);
10451 +               }
10452 +       } else
10453 +               printk(KERN_WARNING "kcapi: appl %d card%d released without request\n", appl, card->cnr);
10454  }
10455  /*
10456   * ncci management
10457 @@ -863,16 +873,7 @@
10458  
10459  static void controllercb_ready(struct capi_ctr * card)
10460  {
10461 -       __u16 appl;
10462 -
10463         card->cardstate = CARD_RUNNING;
10464 -
10465 -       for (appl = 1; appl <= CAPI_MAXAPPL; appl++) {
10466 -               if (!VALID_APPLID(appl)) continue;
10467 -               if (APPL(appl)->releasing) continue;
10468 -               card->driver->register_appl(card, appl, &APPL(appl)->rparam);
10469 -       }
10470 -
10471          printk(KERN_NOTICE "kcapi: card %d \"%s\" ready.\n",
10472                 CARDNR(card), card->name);
10473  
10474 diff -urN linux-2.4.18/drivers/usb/Config.in linux-2.4.18-mh15/drivers/usb/Config.in
10475 --- linux-2.4.18/drivers/usb/Config.in  2002-02-25 20:38:07.000000000 +0100
10476 +++ linux-2.4.18-mh15/drivers/usb/Config.in     2004-08-01 16:26:23.000000000 +0200
10477 @@ -31,7 +31,13 @@
10478  
10479  comment 'USB Device Class drivers'
10480  dep_tristate '  USB Audio support' CONFIG_USB_AUDIO $CONFIG_USB $CONFIG_SOUND
10481 -dep_tristate '  USB Bluetooth support (EXPERIMENTAL)' CONFIG_USB_BLUETOOTH $CONFIG_USB $CONFIG_EXPERIMENTAL
10482 +if [ "$CONFIG_EXPERIMENTAL" = "y" ]; then
10483 +   if [ "$CONFIG_BLUEZ" = "n" ]; then
10484 +      dep_tristate '  USB Bluetooth support (EXPERIMENTAL)' CONFIG_USB_BLUETOOTH $CONFIG_USB
10485 +   else
10486 +      comment '  USB Bluetooth can only be used with disabled Bluetooth subsystem'
10487 +   fi
10488 +fi
10489  if [ "$CONFIG_SCSI" = "n" ]; then
10490     comment '  SCSI support is needed for USB Storage'
10491  fi
10492 diff -urN linux-2.4.18/drivers/usb/hid-core.c linux-2.4.18-mh15/drivers/usb/hid-core.c
10493 --- linux-2.4.18/drivers/usb/hid-core.c 2001-12-21 18:41:55.000000000 +0100
10494 +++ linux-2.4.18-mh15/drivers/usb/hid-core.c    2004-08-01 16:26:23.000000000 +0200
10495 @@ -217,6 +217,8 @@
10496  
10497         offset = report->size;
10498         report->size += parser->global.report_size * parser->global.report_count;
10499 +       if (usages < parser->global.report_count)
10500 +               usages = parser->global.report_count;
10501  
10502         if (usages == 0)
10503                 return 0; /* ignore padding fields */
10504 diff -urN linux-2.4.18/include/linux/firmware.h linux-2.4.18-mh15/include/linux/firmware.h
10505 --- linux-2.4.18/include/linux/firmware.h       1970-01-01 01:00:00.000000000 +0100
10506 +++ linux-2.4.18-mh15/include/linux/firmware.h  2004-08-01 16:26:23.000000000 +0200
10507 @@ -0,0 +1,20 @@
10508 +#ifndef _LINUX_FIRMWARE_H
10509 +#define _LINUX_FIRMWARE_H
10510 +#include <linux/module.h>
10511 +#include <linux/types.h>
10512 +#define FIRMWARE_NAME_MAX 30 
10513 +struct firmware {
10514 +       size_t size;
10515 +       u8 *data;
10516 +};
10517 +int request_firmware (const struct firmware **fw, const char *name,
10518 +                     const char *device);
10519 +int request_firmware_nowait (
10520 +       struct module *module,
10521 +       const char *name, const char *device, void *context,
10522 +       void (*cont)(const struct firmware *fw, void *context));
10523 +/* On 2.5 'device' is 'struct device *' */
10524 +
10525 +void release_firmware (const struct firmware *fw);
10526 +void register_firmware (const char *name, const u8 *data, size_t size);
10527 +#endif
10528 diff -urN linux-2.4.18/include/linux/input.h linux-2.4.18-mh15/include/linux/input.h
10529 --- linux-2.4.18/include/linux/input.h  2001-09-13 00:34:06.000000000 +0200
10530 +++ linux-2.4.18-mh15/include/linux/input.h     2004-08-01 16:26:23.000000000 +0200
10531 @@ -468,6 +468,8 @@
10532  #define BUS_PCI                        0x01
10533  #define BUS_ISAPNP             0x02
10534  #define BUS_USB                        0x03
10535 +#define BUS_HIL                        0x04
10536 +#define BUS_BLUETOOTH          0x05
10537  
10538  #define BUS_ISA                        0x10
10539  #define BUS_I8042              0x11
10540 diff -urN linux-2.4.18/include/linux/kernel.h linux-2.4.18-mh15/include/linux/kernel.h
10541 --- linux-2.4.18/include/linux/kernel.h 2002-02-25 20:38:13.000000000 +0100
10542 +++ linux-2.4.18-mh15/include/linux/kernel.h    2004-08-01 16:26:23.000000000 +0200
10543 @@ -11,6 +11,7 @@
10544  #include <linux/linkage.h>
10545  #include <linux/stddef.h>
10546  #include <linux/types.h>
10547 +#include <linux/compiler.h>
10548  
10549  /* Optimization barrier */
10550  /* The "volatile" is due to gcc bugs */
10551 @@ -181,4 +182,6 @@
10552         char _f[20-2*sizeof(long)-sizeof(int)]; /* Padding: libc5 uses this.. */
10553  };
10554  
10555 -#endif
10556 +#define BUG_ON(condition) do { if (unlikely((condition)!=0)) BUG(); } while(0)
10557 +
10558 +#endif /* _LINUX_KERNEL_H */
10559 diff -urN linux-2.4.18/include/linux/net.h linux-2.4.18-mh15/include/linux/net.h
10560 --- linux-2.4.18/include/linux/net.h    2001-11-22 20:46:19.000000000 +0100
10561 +++ linux-2.4.18-mh15/include/linux/net.h       2004-08-01 16:26:23.000000000 +0200
10562 @@ -139,6 +139,7 @@
10563  extern int     sock_recvmsg(struct socket *, struct msghdr *m, int len, int flags);
10564  extern int     sock_readv_writev(int type, struct inode * inode, struct file * file,
10565                                   const struct iovec * iov, long count, long size);
10566 +extern struct socket *sockfd_lookup(int fd, int *err);
10567  
10568  extern int     net_ratelimit(void);
10569  extern unsigned long net_random(void);
10570 diff -urN linux-2.4.18/include/linux/uinput.h linux-2.4.18-mh15/include/linux/uinput.h
10571 --- linux-2.4.18/include/linux/uinput.h 1970-01-01 01:00:00.000000000 +0100
10572 +++ linux-2.4.18-mh15/include/linux/uinput.h    2004-08-01 16:26:23.000000000 +0200
10573 @@ -0,0 +1,79 @@
10574 +/*
10575 + *  User level driver support for input subsystem
10576 + *
10577 + * Heavily based on evdev.c by Vojtech Pavlik
10578 + *
10579 + * This program is free software; you can redistribute it and/or modify
10580 + * it under the terms of the GNU General Public License as published by
10581 + * the Free Software Foundation; either version 2 of the License, or
10582 + * (at your option) any later version.
10583 + *
10584 + * This program is distributed in the hope that it will be useful,
10585 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
10586 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10587 + * GNU General Public License for more details.
10588 + *
10589 + * You should have received a copy of the GNU General Public License
10590 + * along with this program; if not, write to the Free Software
10591 + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
10592 + *
10593 + * Author: Aristeu Sergio Rozanski Filho <aris@cathedrallabs.org>
10594 + * 
10595 + * Changes/Revisions:
10596 + *     0.1     20/06/2002
10597 + *             - first public version
10598 + */
10599 +
10600 +#ifndef __UINPUT_H_
10601 +#define __UINPUT_H_
10602 +
10603 +#ifdef __KERNEL__
10604 +#define UINPUT_MINOR           223
10605 +#define UINPUT_NAME            "uinput"
10606 +#define UINPUT_BUFFER_SIZE     16
10607 +
10608 +/* state flags => bit index for {set|clear|test}_bit ops */
10609 +#define UIST_CREATED           0
10610 +
10611 +struct uinput_device {
10612 +       struct input_dev        *dev;
10613 +       unsigned long           state;
10614 +       wait_queue_head_t       waitq;
10615 +       unsigned char           ready,
10616 +                               head,
10617 +                               tail;
10618 +       struct input_event      buff[UINPUT_BUFFER_SIZE];
10619 +};
10620 +#endif /* __KERNEL__ */
10621 +
10622 +/* ioctl */
10623 +#define UINPUT_IOCTL_BASE      'U'
10624 +#define UI_DEV_CREATE          _IO(UINPUT_IOCTL_BASE, 1)
10625 +#define UI_DEV_DESTROY         _IO(UINPUT_IOCTL_BASE, 2)
10626 +#define UI_SET_EVBIT           _IOW(UINPUT_IOCTL_BASE, 100, int)
10627 +#define UI_SET_KEYBIT          _IOW(UINPUT_IOCTL_BASE, 101, int)
10628 +#define UI_SET_RELBIT          _IOW(UINPUT_IOCTL_BASE, 102, int)
10629 +#define UI_SET_ABSBIT          _IOW(UINPUT_IOCTL_BASE, 103, int)
10630 +#define UI_SET_MSCBIT          _IOW(UINPUT_IOCTL_BASE, 104, int)
10631 +#define UI_SET_LEDBIT          _IOW(UINPUT_IOCTL_BASE, 105, int)
10632 +#define UI_SET_SNDBIT          _IOW(UINPUT_IOCTL_BASE, 106, int)
10633 +#define UI_SET_FFBIT           _IOW(UINPUT_IOCTL_BASE, 107, int)
10634 +
10635 +#ifndef NBITS
10636 +#define NBITS(x) ((((x)-1)/(sizeof(long)*8))+1)
10637 +#endif /* NBITS */
10638 +
10639 +#define UINPUT_MAX_NAME_SIZE   80
10640 +struct uinput_user_dev {
10641 +       char name[UINPUT_MAX_NAME_SIZE];
10642 +       unsigned short idbus;
10643 +       unsigned short idvendor;
10644 +       unsigned short idproduct;
10645 +       unsigned short idversion;
10646 +       int ff_effects_max;
10647 +       int absmax[ABS_MAX + 1];
10648 +       int absmin[ABS_MAX + 1];
10649 +       int absfuzz[ABS_MAX + 1];
10650 +       int absflat[ABS_MAX + 1];
10651 +};
10652 +#endif /* __UINPUT_H_ */
10653 diff -urN linux-2.4.18/include/net/bluetooth/bluetooth.h linux-2.4.18-mh15/include/net/bluetooth/bluetooth.h
10654 --- linux-2.4.18/include/net/bluetooth/bluetooth.h      2001-09-07 18:28:38.000000000 +0200
10655 +++ linux-2.4.18-mh15/include/net/bluetooth/bluetooth.h 2004-08-01 16:26:23.000000000 +0200
10656 @@ -23,7 +23,7 @@
10657  */
10658  
10659  /*
10660 - *  $Id: bluetooth.h,v 1.6 2001/08/03 04:19:49 maxk Exp $
10661 + *  $Id: bluetooth.h,v 1.9 2002/05/06 21:11:55 maxk Exp $
10662   */
10663  
10664  #ifndef __BLUETOOTH_H
10665 @@ -31,17 +31,64 @@
10666  
10667  #include <asm/types.h>
10668  #include <asm/byteorder.h>
10669 +#include <linux/poll.h>
10670 +#include <net/sock.h>
10671  
10672  #ifndef AF_BLUETOOTH
10673  #define AF_BLUETOOTH   31
10674  #define PF_BLUETOOTH   AF_BLUETOOTH
10675  #endif
10676  
10677 +/* Reserv for core and drivers use */
10678 +#define BLUEZ_SKB_RESERVE       8
10679 +
10680 +#ifndef MIN
10681 +#define MIN(a,b) ((a) < (b) ? (a) : (b))
10682 +#endif
10683 +
10684  #define BTPROTO_L2CAP   0
10685  #define BTPROTO_HCI     1
10686 +#define BTPROTO_SCO    2
10687 +#define BTPROTO_RFCOMM 3
10688 +#define BTPROTO_BNEP   4
10689 +#define BTPROTO_CMTP   5
10690 +#define BTPROTO_HIDP   6
10691  
10692  #define SOL_HCI     0
10693  #define SOL_L2CAP   6
10694 +#define SOL_SCO     17
10695 +#define SOL_RFCOMM  18
10696 +
10697 +/* Debugging */
10698 +#ifdef CONFIG_BLUEZ_DEBUG
10699 +
10700 +#define HCI_CORE_DEBUG         1
10701 +#define HCI_SOCK_DEBUG         1
10702 +#define HCI_UART_DEBUG         1
10703 +#define HCI_USB_DEBUG          1
10704 +//#define HCI_DATA_DUMP                1
10705 +
10706 +#define L2CAP_DEBUG            1
10707 +#define SCO_DEBUG              1
10708 +#define AF_BLUETOOTH_DEBUG     1
10709 +
10710 +#endif /* CONFIG_BLUEZ_DEBUG */
10711 +
10712 +extern void bluez_dump(char *pref, __u8 *buf, int count);
10713 +
10714 +#if __GNUC__ <= 2 && __GNUC_MINOR__ < 95
10715 +#define __func__ __FUNCTION__
10716 +#endif
10717 +
10718 +#define BT_INFO(fmt, arg...) printk(KERN_INFO fmt "\n" , ## arg)
10719 +#define BT_DBG(fmt, arg...)  printk(KERN_INFO "%s: " fmt "\n" , __func__ , ## arg)
10720 +#define BT_ERR(fmt, arg...)  printk(KERN_ERR  "%s: " fmt "\n" , __func__ , ## arg)
10721 +
10722 +#ifdef HCI_DATA_DUMP
10723 +#define BT_DMP(buf, len)    bluez_dump(__func__, buf, len)
10724 +#else
10725 +#define BT_DMP(D...)
10726 +#endif
10727  
10728  /* Connection and socket states */
10729  enum {
10730 @@ -50,6 +97,7 @@
10731         BT_BOUND,
10732         BT_LISTEN,
10733         BT_CONNECT,
10734 +       BT_CONNECT2,
10735         BT_CONFIG,
10736         BT_DISCONN,
10737         BT_CLOSED
10738 @@ -66,7 +114,8 @@
10739         __u8 b[6];
10740  } __attribute__((packed)) bdaddr_t;
10741  
10742 -#define BDADDR_ANY ((bdaddr_t *)"\000\000\000\000\000")
10743 +#define BDADDR_ANY   (&(bdaddr_t) {{0, 0, 0, 0, 0, 0}})
10744 +#define BDADDR_LOCAL (&(bdaddr_t) {{0, 0, 0, 0xff, 0xff, 0xff}})
10745  
10746  /* Copy, swap, convert BD Address */
10747  static inline int bacmp(bdaddr_t *ba1, bdaddr_t *ba2)
10748 @@ -82,6 +131,91 @@
10749  char *batostr(bdaddr_t *ba);
10750  bdaddr_t *strtoba(char *str);
10751  
10752 +/* Common socket structures and functions */
10753 +
10754 +#define bluez_pi(sk) ((struct bluez_pinfo *) &sk->protinfo)
10755 +#define bluez_sk(pi) ((struct sock *) \
10756 +       ((void *)pi - (unsigned long)(&((struct sock *)0)->protinfo)))
10757 +
10758 +struct bluez_pinfo {
10759 +       bdaddr_t        src;
10760 +       bdaddr_t        dst;
10761 +
10762 +       struct list_head accept_q;
10763 +       struct sock *parent;
10764 +};
10765 +
10766 +struct bluez_sock_list {
10767 +       struct sock *head;
10768 +       rwlock_t     lock;
10769 +};
10770 +
10771 +int  bluez_sock_register(int proto, struct net_proto_family *ops);
10772 +int  bluez_sock_unregister(int proto);
10773 +void bluez_sock_init(struct socket *sock, struct sock *sk);
10774 +void bluez_sock_link(struct bluez_sock_list *l, struct sock *s);
10775 +void bluez_sock_unlink(struct bluez_sock_list *l, struct sock *s);
10776 +int  bluez_sock_recvmsg(struct socket *sock, struct msghdr *msg, int len, int flags, struct scm_cookie *scm);
10777 +uint bluez_sock_poll(struct file * file, struct socket *sock, poll_table *wait);
10778 +int  bluez_sock_wait_state(struct sock *sk, int state, unsigned long timeo);
10779 +
10780 +void bluez_accept_enqueue(struct sock *parent, struct sock *sk);
10781 +struct sock * bluez_accept_dequeue(struct sock *parent, struct socket *newsock);
10782 +
10783 +/* Skb helpers */
10784 +struct bluez_skb_cb {
10785 +       int    incomming;
10786 +};
10787 +#define bluez_cb(skb)  ((struct bluez_skb_cb *)(skb->cb)) 
10788 +
10789 +static inline struct sk_buff *bluez_skb_alloc(unsigned int len, int how)
10790 +{
10791 +       struct sk_buff *skb;
10792 +
10793 +       if ((skb = alloc_skb(len + BLUEZ_SKB_RESERVE, how))) {
10794 +               skb_reserve(skb, BLUEZ_SKB_RESERVE);
10795 +               bluez_cb(skb)->incomming  = 0;
10796 +       }
10797 +       return skb;
10798 +}
10799 +
10800 +static inline struct sk_buff *bluez_skb_send_alloc(struct sock *sk, unsigned long len, 
10801 +                                                      int nb, int *err)
10802 +{
10803 +       struct sk_buff *skb;
10804 +
10805 +       if ((skb = sock_alloc_send_skb(sk, len + BLUEZ_SKB_RESERVE, nb, err))) {
10806 +               skb_reserve(skb, BLUEZ_SKB_RESERVE);
10807 +               bluez_cb(skb)->incomming  = 0;
10808 +       }
10809 +
10810 +       return skb;
10811 +}
10812 +
10813 +static inline int skb_frags_no(struct sk_buff *skb)
10814 +{
10815 +       register struct sk_buff *frag = skb_shinfo(skb)->frag_list;
10816 +       register int n = 1;
10817 +
10818 +       for (; frag; frag=frag->next, n++);
10819 +       return n;
10820 +}
10821 +
10822 +int hci_core_init(void);
10823 +int hci_core_cleanup(void);
10824 +int hci_sock_init(void);
10825 +int hci_sock_cleanup(void);
10826 +
10827  int bterr(__u16 code);
10828  
10829 +#ifndef MODULE_LICENSE
10830 +#define MODULE_LICENSE(x)
10831 +#endif
10832 +
10833 +#ifndef list_for_each_safe
10834 +#define list_for_each_safe(pos, n, head) \
10835 +       for (pos = (head)->next, n = pos->next; pos != (head); \
10836 +               pos = n, n = pos->next)
10837 +#endif
10838 +
10839  #endif /* __BLUETOOTH_H */
10840 diff -urN linux-2.4.18/include/net/bluetooth/bluez.h linux-2.4.18-mh15/include/net/bluetooth/bluez.h
10841 --- linux-2.4.18/include/net/bluetooth/bluez.h  2001-09-07 18:28:38.000000000 +0200
10842 +++ linux-2.4.18-mh15/include/net/bluetooth/bluez.h     1970-01-01 01:00:00.000000000 +0100
10843 @@ -1,124 +0,0 @@
10844 -/* 
10845 -   BlueZ - Bluetooth protocol stack for Linux
10846 -   Copyright (C) 2000-2001 Qualcomm Incorporated
10847 -
10848 -   Written 2000,2001 by Maxim Krasnyansky <maxk@qualcomm.com>
10849 -
10850 -   This program is free software; you can redistribute it and/or modify
10851 -   it under the terms of the GNU General Public License version 2 as
10852 -   published by the Free Software Foundation;
10853 -
10854 -   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
10855 -   OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
10856 -   FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS.
10857 -   IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) AND AUTHOR(S) BE LIABLE FOR ANY
10858 -   CLAIM, OR ANY SPECIAL INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES 
10859 -   WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 
10860 -   ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 
10861 -   OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
10862 -
10863 -   ALL LIABILITY, INCLUDING LIABILITY FOR INFRINGEMENT OF ANY PATENTS, 
10864 -   COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS, RELATING TO USE OF THIS 
10865 -   SOFTWARE IS DISCLAIMED.
10866 -*/
10867 -
10868 -/*
10869 - *  $Id: bluez.h,v 1.4 2001/08/03 04:19:49 maxk Exp $
10870 - */
10871 -
10872 -#ifndef __IF_BLUEZ_H
10873 -#define __IF_BLUEZ_H
10874 -
10875 -#include <net/sock.h>
10876 -
10877 -#define BLUEZ_MAX_PROTO        2
10878 -
10879 -/* Reserv for core and drivers use */
10880 -#define BLUEZ_SKB_RESERVE      8
10881 -
10882 -#ifndef MIN
10883 -#define MIN(a,b) ((a) < (b) ? (a) : (b))
10884 -#endif
10885 -
10886 -/* Debugging */
10887 -#ifdef BLUEZ_DEBUG
10888 -
10889 -#define HCI_CORE_DEBUG         1
10890 -#define HCI_SOCK_DEBUG         1
10891 -#define HCI_UART_DEBUG         1
10892 -#define HCI_USB_DEBUG          1
10893 -//#define HCI_DATA_DUMP                1
10894 -
10895 -#define L2CAP_DEBUG                    1
10896 -
10897 -#endif /* BLUEZ_DEBUG */
10898 -
10899 -extern void bluez_dump(char *pref, __u8 *buf, int count);
10900 -
10901 -#define INF(fmt, arg...) printk(KERN_INFO fmt "\n" , ## arg)
10902 -#define DBG(fmt, arg...) printk(KERN_INFO __FUNCTION__ ": " fmt "\n" , ## arg)
10903 -#define ERR(fmt, arg...) printk(KERN_ERR  __FUNCTION__ ": " fmt "\n" , ## arg)
10904 -
10905 -#ifdef HCI_DATA_DUMP
10906 -#define DMP(buf, len)    bluez_dump(__FUNCTION__, buf, len)
10907 -#else
10908 -#define DMP(D...)
10909 -#endif
10910 -
10911 -/* ----- Sockets ------ */
10912 -struct bluez_sock_list {
10913 -       struct sock *head;
10914 -       rwlock_t     lock;
10915 -};
10916 -
10917 -extern int  bluez_sock_register(int proto, struct net_proto_family *ops);
10918 -extern int  bluez_sock_unregister(int proto);
10919 -
10920 -extern void bluez_sock_link(struct bluez_sock_list *l, struct sock *s);
10921 -extern void bluez_sock_unlink(struct bluez_sock_list *l, struct sock *s);
10922 -
10923 -/* ----- SKB helpers ----- */
10924 -struct bluez_skb_cb {
10925 -       int    incomming;
10926 -};
10927 -#define bluez_cb(skb)  ((struct bluez_skb_cb *)(skb->cb)) 
10928 -
10929 -static inline struct sk_buff *bluez_skb_alloc(unsigned int len, int how)
10930 -{
10931 -       struct sk_buff *skb;
10932 -
10933 -       if ((skb = alloc_skb(len + BLUEZ_SKB_RESERVE, how))) {
10934 -               skb_reserve(skb, BLUEZ_SKB_RESERVE);
10935 -               bluez_cb(skb)->incomming  = 0;
10936 -       }
10937 -       return skb;
10938 -}
10939 -
10940 -static inline struct sk_buff *bluez_skb_send_alloc(struct sock *sk, unsigned long len, 
10941 -                                                      int nb, int *err)
10942 -{
10943 -       struct sk_buff *skb;
10944 -
10945 -       if ((skb = sock_alloc_send_skb(sk, len + BLUEZ_SKB_RESERVE, nb, err))) {
10946 -               skb_reserve(skb, BLUEZ_SKB_RESERVE);
10947 -               bluez_cb(skb)->incomming  = 0;
10948 -       }
10949 -
10950 -       return skb;
10951 -}
10952 -
10953 -static inline int skb_frags_no(struct sk_buff *skb)
10954 -{
10955 -       register struct sk_buff *frag = skb_shinfo(skb)->frag_list;
10956 -       register int n = 1;
10957 -
10958 -       for (; frag; frag=frag->next, n++);
10959 -       return n;
10960 -}
10961 -
10962 -extern int hci_core_init(void);
10963 -extern int hci_core_cleanup(void);
10964 -extern int hci_sock_init(void);
10965 -extern int hci_sock_cleanup(void);
10966 -
10967 -#endif /* __IF_BLUEZ_H */
10968 diff -urN linux-2.4.18/include/net/bluetooth/hci_core.h linux-2.4.18-mh15/include/net/bluetooth/hci_core.h
10969 --- linux-2.4.18/include/net/bluetooth/hci_core.h       2001-09-07 18:28:38.000000000 +0200
10970 +++ linux-2.4.18-mh15/include/net/bluetooth/hci_core.h  2004-08-01 16:26:23.000000000 +0200
10971 @@ -23,7 +23,7 @@
10972  */
10973  
10974  /* 
10975 - * $Id: hci_core.h,v 1.11 2001/08/05 06:02:15 maxk Exp $ 
10976 + * $Id: hci_core.h,v 1.5 2002/06/27 04:56:30 maxk Exp $ 
10977   */
10978  
10979  #ifndef __HCI_CORE_H
10980 @@ -32,14 +32,12 @@
10981  #include <net/bluetooth/hci.h>
10982  
10983  /* HCI upper protocols */
10984 -#define HCI_MAX_PROTO  1
10985  #define HCI_PROTO_L2CAP        0
10986 +#define HCI_PROTO_SCO  1
10987  
10988  #define HCI_INIT_TIMEOUT (HZ * 10)
10989  
10990 -/* ----- Inquiry cache ----- */
10991 -#define INQUIRY_CACHE_AGE_MAX   (HZ*5)    // 5 seconds
10992 -#define INQUIRY_ENTRY_AGE_MAX   (HZ*60)   // 60 seconds
10993 +/* HCI Core structures */
10994  
10995  struct inquiry_entry {
10996         struct inquiry_entry    *next;
10997 @@ -53,111 +51,188 @@
10998         struct inquiry_entry    *list;
10999  };
11000  
11001 -static inline void inquiry_cache_init(struct inquiry_cache *cache)
11002 -{
11003 -       spin_lock_init(&cache->lock);
11004 -       cache->list = NULL;
11005 -}
11006 +struct conn_hash {
11007 +       struct list_head list;
11008 +       spinlock_t       lock;
11009 +       unsigned int     num;
11010 +};
11011  
11012 -static inline void inquiry_cache_lock(struct inquiry_cache *cache)
11013 -{
11014 -       spin_lock(&cache->lock);
11015 -}
11016 +struct hci_dev {
11017 +       struct list_head list;
11018 +       spinlock_t      lock;
11019 +       atomic_t        refcnt;
11020  
11021 -static inline void inquiry_cache_unlock(struct inquiry_cache *cache)
11022 -{
11023 -       spin_unlock(&cache->lock);
11024 -}
11025 +       char            name[8];
11026 +       unsigned long   flags;
11027 +       __u16           id;
11028 +       __u8            type;
11029 +       bdaddr_t        bdaddr;
11030 +       __u8            features[8];
11031  
11032 -static inline void inquiry_cache_lock_bh(struct inquiry_cache *cache)
11033 -{
11034 -       spin_lock_bh(&cache->lock);
11035 -}
11036 +       __u16           pkt_type;
11037 +       __u16           link_policy;
11038 +       __u16           link_mode;
11039  
11040 -static inline void inquiry_cache_unlock_bh(struct inquiry_cache *cache)
11041 -{
11042 -       spin_unlock_bh(&cache->lock);
11043 -}
11044 +       unsigned long   quirks;
11045  
11046 -static inline long inquiry_cache_age(struct inquiry_cache *cache)
11047 -{
11048 -       return jiffies - cache->timestamp;
11049 -}
11050 +       atomic_t        cmd_cnt;
11051 +       unsigned int    acl_cnt;
11052 +       unsigned int    sco_cnt;
11053  
11054 -static inline long inquiry_entry_age(struct inquiry_entry *e)
11055 -{
11056 -       return jiffies - e->timestamp;
11057 -}
11058 -extern void inquiry_cache_flush(struct inquiry_cache *cache);
11059 +       unsigned int    acl_mtu;
11060 +       unsigned int    sco_mtu;
11061 +       unsigned int    acl_pkts;
11062 +       unsigned int    sco_pkts;
11063  
11064 -struct hci_dev;
11065 +       unsigned long   cmd_last_tx;
11066 +       unsigned long   acl_last_tx;
11067 +       unsigned long   sco_last_tx;
11068 +       
11069 +       struct tasklet_struct   cmd_task;
11070 +       struct tasklet_struct   rx_task;
11071 +       struct tasklet_struct   tx_task;
11072 +
11073 +       struct sk_buff_head     rx_q;
11074 +       struct sk_buff_head     raw_q;
11075 +       struct sk_buff_head     cmd_q;
11076 +
11077 +       struct sk_buff          *sent_cmd;
11078 +
11079 +       struct semaphore        req_lock;
11080 +       wait_queue_head_t       req_wait_q;
11081 +       __u32                   req_status;
11082 +       __u32                   req_result;
11083 +
11084 +       struct inquiry_cache    inq_cache;
11085 +       struct conn_hash        conn_hash;
11086 +
11087 +       struct hci_dev_stats    stat;
11088 +
11089 +       void                    *driver_data;
11090 +       void                    *core_data;
11091 +
11092 +       atomic_t                promisc;
11093 +
11094 +       int (*open)(struct hci_dev *hdev);
11095 +       int (*close)(struct hci_dev *hdev);
11096 +       int (*flush)(struct hci_dev *hdev);
11097 +       int (*send)(struct sk_buff *skb);
11098 +       void (*destruct)(struct hci_dev *hdev);
11099 +       int (*ioctl)(struct hci_dev *hdev, unsigned int cmd, unsigned long arg);
11100 +};
11101  
11102 -/* ----- HCI Connections ----- */
11103  struct hci_conn {
11104         struct list_head list;
11105 +
11106 +       atomic_t         refcnt;
11107 +       spinlock_t       lock;
11108 +
11109         bdaddr_t         dst;
11110         __u16            handle;
11111 +       __u16            state;
11112         __u8             type;
11113 -       unsigned int     sent;
11114 +       __u8             out;
11115 +       __u32            link_mode;
11116 +       unsigned long    pend;
11117 +       
11118 +       unsigned int     sent;
11119 +       
11120 +       struct sk_buff_head data_q;
11121  
11122 +       struct timer_list timer;
11123 +       
11124         struct hci_dev  *hdev;
11125         void            *l2cap_data;
11126 +       void            *sco_data;
11127         void            *priv;
11128  
11129 -       struct sk_buff_head data_q;
11130 +       struct hci_conn *link;
11131  };
11132  
11133 -struct conn_hash {
11134 -       struct list_head list;
11135 -       spinlock_t       lock;
11136 -       unsigned int     num;
11137 -};
11138 +extern struct hci_proto *hci_proto[];
11139 +extern struct list_head hdev_list;
11140 +extern rwlock_t hdev_list_lock;
11141 +
11142 +/* ----- Inquiry cache ----- */
11143 +#define INQUIRY_CACHE_AGE_MAX   (HZ*30)   // 30 seconds
11144 +#define INQUIRY_ENTRY_AGE_MAX   (HZ*60)   // 60 seconds
11145 +
11146 +#define inquiry_cache_lock(c)          spin_lock(&c->lock)
11147 +#define inquiry_cache_unlock(c)                spin_unlock(&c->lock)
11148 +#define inquiry_cache_lock_bh(c)       spin_lock_bh(&c->lock)
11149 +#define inquiry_cache_unlock_bh(c)     spin_unlock_bh(&c->lock)
11150  
11151 -static inline void conn_hash_init(struct conn_hash *h)
11152 +static inline void inquiry_cache_init(struct hci_dev *hdev)
11153  {
11154 -       INIT_LIST_HEAD(&h->list);
11155 -       spin_lock_init(&h->lock);
11156 -       h->num = 0;     
11157 +       struct inquiry_cache *c = &hdev->inq_cache;
11158 +       spin_lock_init(&c->lock);
11159 +       c->list = NULL;
11160  }
11161  
11162 -static inline void conn_hash_lock(struct conn_hash *h)
11163 +static inline int inquiry_cache_empty(struct hci_dev *hdev)
11164  {
11165 -       spin_lock(&h->lock);
11166 +       struct inquiry_cache *c = &hdev->inq_cache;
11167 +       return (c->list == NULL);
11168  }
11169  
11170 -static inline void conn_hash_unlock(struct conn_hash *h)
11171 +static inline long inquiry_cache_age(struct hci_dev *hdev)
11172  {
11173 -       spin_unlock(&h->lock);
11174 +       struct inquiry_cache *c = &hdev->inq_cache;
11175 +       return jiffies - c->timestamp;
11176  }
11177  
11178 -static inline void __conn_hash_add(struct conn_hash *h, __u16 handle, struct hci_conn *c)
11179 +static inline long inquiry_entry_age(struct inquiry_entry *e)
11180  {
11181 -       list_add(&c->list, &h->list);
11182 -       h->num++;
11183 +       return jiffies - e->timestamp;
11184  }
11185  
11186 -static inline void conn_hash_add(struct conn_hash *h, __u16 handle, struct hci_conn *c)
11187 +struct inquiry_entry *inquiry_cache_lookup(struct hci_dev *hdev, bdaddr_t *bdaddr);
11188 +void inquiry_cache_update(struct hci_dev *hdev, inquiry_info *info);
11189 +void inquiry_cache_flush(struct hci_dev *hdev);
11190 +int  inquiry_cache_dump(struct hci_dev *hdev, int num, __u8 *buf);
11191 +
11192 +/* ----- HCI Connections ----- */
11193 +enum {
11194 +       HCI_CONN_AUTH_PEND,
11195 +       HCI_CONN_ENCRYPT_PEND
11196 +};
11197 +
11198 +#define hci_conn_lock(c)       spin_lock(&c->lock)
11199 +#define hci_conn_unlock(c)     spin_unlock(&c->lock)
11200 +#define hci_conn_lock_bh(c)    spin_lock_bh(&c->lock)
11201 +#define hci_conn_unlock_bh(c)  spin_unlock_bh(&c->lock)
11202 +
11203 +#define conn_hash_lock(d)      spin_lock(&d->conn_hash->lock)
11204 +#define conn_hash_unlock(d)    spin_unlock(&d->conn_hash->lock)
11205 +#define conn_hash_lock_bh(d)   spin_lock_bh(&d->conn_hash->lock)
11206 +#define conn_hash_unlock_bh(d) spin_unlock_bh(&d->conn_hash->lock)
11207 +
11208 +static inline void conn_hash_init(struct hci_dev *hdev)
11209  {
11210 -       conn_hash_lock(h);
11211 -       __conn_hash_add(h, handle, c);
11212 -       conn_hash_unlock(h);
11213 +       struct conn_hash *h = &hdev->conn_hash;
11214 +       INIT_LIST_HEAD(&h->list);
11215 +       spin_lock_init(&h->lock);
11216 +       h->num = 0;     
11217  }
11218  
11219 -static inline void __conn_hash_del(struct conn_hash *h, struct hci_conn *c)
11220 +static inline void conn_hash_add(struct hci_dev *hdev, struct hci_conn *c)
11221  {
11222 -       list_del(&c->list);
11223 -       h->num--;
11224 +       struct conn_hash *h = &hdev->conn_hash;
11225 +       list_add(&c->list, &h->list);
11226 +       h->num++;
11227  }
11228  
11229 -static inline void conn_hash_del(struct conn_hash *h, struct hci_conn *c)
11230 +static inline void conn_hash_del(struct hci_dev *hdev, struct hci_conn *c)
11231  {
11232 -       conn_hash_lock(h);
11233 -       __conn_hash_del(h, c);
11234 -       conn_hash_unlock(h);
11235 +       struct conn_hash *h = &hdev->conn_hash;
11236 +       list_del(&c->list);
11237 +       h->num--;
11238  }
11239  
11240 -static inline  struct hci_conn *__conn_hash_lookup(struct conn_hash *h, __u16 handle)
11241 +static inline struct hci_conn *conn_hash_lookup_handle(struct hci_dev *hdev,
11242 +                                       __u16 handle)
11243  {
11244 +       register struct conn_hash *h = &hdev->conn_hash;
11245         register struct list_head *p;
11246         register struct hci_conn  *c;
11247  
11248 @@ -169,101 +244,97 @@
11249          return NULL;
11250  }
11251  
11252 -static inline struct hci_conn *conn_hash_lookup(struct conn_hash *h, __u16 handle)
11253 +static inline struct hci_conn *conn_hash_lookup_ba(struct hci_dev *hdev,
11254 +                                       __u8 type, bdaddr_t *ba)
11255  {
11256 -       struct hci_conn *conn;
11257 +       register struct conn_hash *h = &hdev->conn_hash;
11258 +       register struct list_head *p;
11259 +       register struct hci_conn  *c;
11260  
11261 -       conn_hash_lock(h);
11262 -       conn = __conn_hash_lookup(h, handle);
11263 -       conn_hash_unlock(h);
11264 -       return conn;
11265 +       list_for_each(p, &h->list) {
11266 +               c = list_entry(p, struct hci_conn, list);
11267 +               if (c->type == type && !bacmp(&c->dst, ba))
11268 +                       return c;
11269 +       }
11270 +        return NULL;
11271  }
11272  
11273 -/* ----- HCI Devices ----- */
11274 -struct hci_dev {
11275 -       atomic_t        refcnt;
11276 -
11277 -       char            name[8];
11278 -       __u32           flags;
11279 -       __u16           id;
11280 -       __u8            type;
11281 -       bdaddr_t        bdaddr;
11282 -       __u8            features[8];
11283 -
11284 -       __u16           pkt_type;
11285 -
11286 -       atomic_t        cmd_cnt;
11287 -       unsigned int    acl_cnt;
11288 -       unsigned int    sco_cnt;
11289 -
11290 -       unsigned int    acl_mtu;
11291 -       unsigned int    sco_mtu;
11292 -       unsigned int    acl_max;
11293 -       unsigned int    sco_max;
11294 -
11295 -       void            *driver_data;
11296 -       void            *l2cap_data;
11297 -       void            *priv;
11298 -
11299 -       struct tasklet_struct   cmd_task;
11300 -       struct tasklet_struct   rx_task;
11301 -       struct tasklet_struct   tx_task;
11302 -
11303 -       struct sk_buff_head     rx_q;
11304 -       struct sk_buff_head     raw_q;
11305 -       struct sk_buff_head     cmd_q;
11306 -
11307 -       struct sk_buff          *sent_cmd;
11308 +void hci_acl_connect(struct hci_conn *conn);
11309 +void hci_acl_disconn(struct hci_conn *conn, __u8 reason);
11310 +void hci_add_sco(struct hci_conn *conn, __u16 handle);
11311  
11312 -       struct semaphore        req_lock;
11313 -       wait_queue_head_t       req_wait_q;
11314 -       __u32                   req_status;
11315 -       __u32                   req_result;
11316 +struct hci_conn *hci_conn_add(struct hci_dev *hdev, int type, bdaddr_t *dst);
11317 +int    hci_conn_del(struct hci_conn *conn);
11318 +void   hci_conn_hash_flush(struct hci_dev *hdev);
11319  
11320 -       struct inquiry_cache    inq_cache;
11321 +struct hci_conn *hci_connect(struct hci_dev *hdev, int type, bdaddr_t *src);
11322 +int hci_conn_auth(struct hci_conn *conn);
11323 +int hci_conn_encrypt(struct hci_conn *conn);
11324  
11325 -       struct conn_hash        conn_hash;
11326 -
11327 -       struct hci_dev_stats    stat;
11328 -
11329 -       int (*open)(struct hci_dev *hdev);
11330 -       int (*close)(struct hci_dev *hdev);
11331 -       int (*flush)(struct hci_dev *hdev);
11332 -       int (*send)(struct sk_buff *skb);
11333 -};
11334 +static inline void hci_conn_set_timer(struct hci_conn *conn, long timeout)
11335 +{
11336 +       mod_timer(&conn->timer, jiffies + timeout);
11337 +}
11338  
11339 -static inline void hci_dev_hold(struct hci_dev *hdev)
11340 +static inline void hci_conn_del_timer(struct hci_conn *conn)
11341  {
11342 -       atomic_inc(&hdev->refcnt);
11343 +       del_timer(&conn->timer);
11344  }
11345  
11346 -static inline void hci_dev_put(struct hci_dev *hdev)
11347 +static inline void hci_conn_hold(struct hci_conn *conn)
11348  {
11349 -       atomic_dec(&hdev->refcnt);
11350 +       atomic_inc(&conn->refcnt);
11351 +       hci_conn_del_timer(conn);
11352  }
11353  
11354 -extern struct hci_dev *hci_dev_get(int index);
11355 -extern int hci_register_dev(struct hci_dev *hdev);
11356 -extern int hci_unregister_dev(struct hci_dev *hdev);
11357 -extern int hci_dev_open(__u16 dev);
11358 -extern int hci_dev_close(__u16 dev);
11359 -extern int hci_dev_reset(__u16 dev);
11360 -extern int hci_dev_reset_stat(__u16 dev);
11361 -extern int hci_dev_info(unsigned long arg);
11362 -extern int hci_dev_list(unsigned long arg);
11363 -extern int hci_dev_setscan(unsigned long arg);
11364 -extern int hci_dev_setauth(unsigned long arg);
11365 -extern int hci_dev_setptype(unsigned long arg);
11366 -extern int hci_conn_list(unsigned long arg);
11367 -extern int hci_inquiry(unsigned long arg);
11368 +static inline void hci_conn_put(struct hci_conn *conn)
11369 +{
11370 +       if (atomic_dec_and_test(&conn->refcnt)) {
11371 +               if (conn->type == ACL_LINK) {
11372 +                       unsigned long timeo = (conn->out) ?
11373 +                               HCI_DISCONN_TIMEOUT : HCI_DISCONN_TIMEOUT * 2;
11374 +                       hci_conn_set_timer(conn, timeo);
11375 +               } else
11376 +                       hci_conn_set_timer(conn, HZ / 100);
11377 +       }
11378 +}
11379  
11380 -extern __u32 hci_dev_setmode(struct hci_dev *hdev, __u32 mode);
11381 -extern __u32 hci_dev_getmode(struct hci_dev *hdev);
11382 +/* ----- HCI Devices ----- */
11383 +static inline void hci_dev_put(struct hci_dev *d)
11384 +{ 
11385 +       if (atomic_dec_and_test(&d->refcnt))
11386 +               d->destruct(d);
11387 +}
11388 +#define hci_dev_hold(d)                atomic_inc(&d->refcnt)
11389 +
11390 +#define hci_dev_lock(d)                spin_lock(&d->lock)
11391 +#define hci_dev_unlock(d)      spin_unlock(&d->lock)
11392 +#define hci_dev_lock_bh(d)     spin_lock_bh(&d->lock)
11393 +#define hci_dev_unlock_bh(d)   spin_unlock_bh(&d->lock)
11394 +
11395 +struct hci_dev *hci_dev_get(int index);
11396 +struct hci_dev *hci_get_route(bdaddr_t *src, bdaddr_t *dst);
11397 +int hci_register_dev(struct hci_dev *hdev);
11398 +int hci_unregister_dev(struct hci_dev *hdev);
11399 +int hci_suspend_dev(struct hci_dev *hdev);
11400 +int hci_resume_dev(struct hci_dev *hdev);
11401 +int hci_dev_open(__u16 dev);
11402 +int hci_dev_close(__u16 dev);
11403 +int hci_dev_reset(__u16 dev);
11404 +int hci_dev_reset_stat(__u16 dev);
11405 +int hci_dev_cmd(unsigned int cmd, unsigned long arg);
11406 +int hci_get_dev_list(unsigned long arg);
11407 +int hci_get_dev_info(unsigned long arg);
11408 +int hci_get_conn_list(unsigned long arg);
11409 +int hci_get_conn_info(struct hci_dev *hdev, unsigned long arg);
11410 +int hci_inquiry(unsigned long arg);
11411  
11412 -extern int hci_recv_frame(struct sk_buff *skb);
11413 +int  hci_recv_frame(struct sk_buff *skb);
11414 +void hci_event_packet(struct hci_dev *hdev, struct sk_buff *skb);
11415  
11416  /* ----- LMP capabilities ----- */
11417  #define lmp_rswitch_capable(dev) (dev->features[0] & LMP_RSWITCH)
11418 +#define lmp_encrypt_capable(dev) (dev->features[0] & LMP_ENCRYPT)
11419  
11420  /* ----- HCI tasks ----- */
11421  static inline void hci_sched_cmd(struct hci_dev *hdev)
11422 @@ -284,43 +355,130 @@
11423  /* ----- HCI protocols ----- */
11424  struct hci_proto {
11425         char            *name;
11426 -       __u32           id;
11427 -       __u32           flags;
11428 +       unsigned int    id;
11429 +       unsigned long   flags;
11430  
11431         void            *priv;
11432  
11433 -       int (*connect_ind)      (struct hci_dev *hdev, bdaddr_t *bdaddr);
11434 -       int (*connect_cfm)      (struct hci_dev *hdev, bdaddr_t *bdaddr, __u8 status, struct hci_conn *conn);
11435 +       int (*connect_ind)      (struct hci_dev *hdev, bdaddr_t *bdaddr, __u8 type);
11436 +       int (*connect_cfm)      (struct hci_conn *conn, __u8 status);
11437         int (*disconn_ind)      (struct hci_conn *conn, __u8 reason);
11438 -       int (*recv_acldata)     (struct hci_conn *conn, struct sk_buff *skb , __u16 flags);
11439 +       int (*recv_acldata)     (struct hci_conn *conn, struct sk_buff *skb, __u16 flags);
11440         int (*recv_scodata)     (struct hci_conn *conn, struct sk_buff *skb);
11441 +       int (*auth_cfm)         (struct hci_conn *conn, __u8 status);
11442 +       int (*encrypt_cfm)      (struct hci_conn *conn, __u8 status);
11443  };
11444  
11445 -extern int hci_register_proto(struct hci_proto *hproto);
11446 -extern int hci_unregister_proto(struct hci_proto *hproto);
11447 -extern int hci_register_notifier(struct notifier_block *nb);
11448 -extern int hci_unregister_notifier(struct notifier_block *nb);
11449 -extern int hci_connect(struct hci_dev * hdev, bdaddr_t * bdaddr);
11450 -extern int hci_disconnect(struct hci_conn *conn, __u8 reason);
11451 -extern int hci_send_cmd(struct hci_dev *hdev, __u16 ogf, __u16 ocf, __u32 plen, void * param);
11452 -extern int hci_send_raw(struct sk_buff *skb);
11453 -extern int hci_send_acl(struct hci_conn *conn, struct sk_buff *skb, __u16 flags);
11454 -extern int hci_send_sco(struct hci_conn *conn, struct sk_buff *skb);
11455 +static inline int hci_proto_connect_ind(struct hci_dev *hdev, bdaddr_t *bdaddr, __u8 type)
11456 +{
11457 +       register struct hci_proto *hp;
11458 +       int mask = 0;
11459 +       
11460 +       hp = hci_proto[HCI_PROTO_L2CAP];
11461 +       if (hp && hp->connect_ind)
11462 +               mask |= hp->connect_ind(hdev, bdaddr, type);
11463 +
11464 +       hp = hci_proto[HCI_PROTO_SCO];
11465 +       if (hp && hp->connect_ind)
11466 +               mask |= hp->connect_ind(hdev, bdaddr, type);
11467 +
11468 +       return mask;
11469 +}
11470 +
11471 +static inline void hci_proto_connect_cfm(struct hci_conn *conn, __u8 status)
11472 +{
11473 +       register struct hci_proto *hp;
11474 +
11475 +       hp = hci_proto[HCI_PROTO_L2CAP];
11476 +       if (hp && hp->connect_cfm)
11477 +               hp->connect_cfm(conn, status);
11478 +
11479 +       hp = hci_proto[HCI_PROTO_SCO];
11480 +       if (hp && hp->connect_cfm)
11481 +               hp->connect_cfm(conn, status);
11482 +}
11483 +
11484 +static inline void hci_proto_disconn_ind(struct hci_conn *conn, __u8 reason)
11485 +{
11486 +       register struct hci_proto *hp;
11487 +
11488 +       hp = hci_proto[HCI_PROTO_L2CAP];
11489 +       if (hp && hp->disconn_ind)
11490 +               hp->disconn_ind(conn, reason);
11491 +
11492 +       hp = hci_proto[HCI_PROTO_SCO];
11493 +       if (hp && hp->disconn_ind)
11494 +               hp->disconn_ind(conn, reason);
11495 +}
11496 +
11497 +static inline void hci_proto_auth_cfm(struct hci_conn *conn, __u8 status)
11498 +{
11499 +       register struct hci_proto *hp;
11500 +
11501 +       hp = hci_proto[HCI_PROTO_L2CAP];
11502 +       if (hp && hp->auth_cfm)
11503 +               hp->auth_cfm(conn, status);
11504 +
11505 +       hp = hci_proto[HCI_PROTO_SCO];
11506 +       if (hp && hp->auth_cfm)
11507 +               hp->auth_cfm(conn, status);
11508 +}
11509 +
11510 +static inline void hci_proto_encrypt_cfm(struct hci_conn *conn, __u8 status)
11511 +{
11512 +       register struct hci_proto *hp;
11513 +
11514 +       hp = hci_proto[HCI_PROTO_L2CAP];
11515 +       if (hp && hp->encrypt_cfm)
11516 +               hp->encrypt_cfm(conn, status);
11517 +
11518 +       hp = hci_proto[HCI_PROTO_SCO];
11519 +       if (hp && hp->encrypt_cfm)
11520 +               hp->encrypt_cfm(conn, status);
11521 +}
11522 +
11523 +int hci_register_proto(struct hci_proto *hproto);
11524 +int hci_unregister_proto(struct hci_proto *hproto);
11525 +int hci_register_notifier(struct notifier_block *nb);
11526 +int hci_unregister_notifier(struct notifier_block *nb);
11527 +
11528 +int hci_send_cmd(struct hci_dev *hdev, __u16 ogf, __u16 ocf, __u32 plen, void *param);
11529 +int hci_send_acl(struct hci_conn *conn, struct sk_buff *skb, __u16 flags);
11530 +int hci_send_sco(struct hci_conn *conn, struct sk_buff *skb);
11531 +
11532 +void *hci_sent_cmd_data(struct hci_dev *hdev, __u16 ogf, __u16 ocf);
11533 +
11534 +void hci_si_event(struct hci_dev *hdev, int type, int dlen, void *data);
11535  
11536  /* ----- HCI Sockets ----- */
11537 -extern void hci_send_to_sock(struct hci_dev *hdev, struct sk_buff *skb);
11538 +void hci_send_to_sock(struct hci_dev *hdev, struct sk_buff *skb);
11539  
11540  /* HCI info for socket */
11541 -#define hci_pi(sk)     ((struct hci_pinfo *) &sk->protinfo)
11542 +#define hci_pi(sk)     ((struct hci_pinfo *) &sk->tp_pinfo)
11543  struct hci_pinfo {
11544         struct hci_dev    *hdev;
11545         struct hci_filter filter;
11546         __u32             cmsg_mask;
11547  };
11548  
11549 +/* HCI security filter */
11550 +#define HCI_SFLT_MAX_OGF  5
11551 +
11552 +struct hci_sec_filter {
11553 +       __u32 type_mask;
11554 +       __u32 event_mask[2];
11555 +       __u32 ocf_mask[HCI_SFLT_MAX_OGF + 1][4];
11556 +};
11557 +
11558  /* ----- HCI requests ----- */
11559  #define HCI_REQ_DONE     0
11560  #define HCI_REQ_PEND     1
11561  #define HCI_REQ_CANCELED  2
11562  
11563 +#define hci_req_lock(d)                down(&d->req_lock)
11564 +#define hci_req_unlock(d)      up(&d->req_lock)
11565 +
11566 +void hci_req_complete(struct hci_dev *hdev, int result);
11567 +void hci_req_cancel(struct hci_dev *hdev, int err);
11568 +
11569  #endif /* __HCI_CORE_H */
11570 diff -urN linux-2.4.18/include/net/bluetooth/hci.h linux-2.4.18-mh15/include/net/bluetooth/hci.h
11571 --- linux-2.4.18/include/net/bluetooth/hci.h    2001-09-07 18:28:38.000000000 +0200
11572 +++ linux-2.4.18-mh15/include/net/bluetooth/hci.h       2004-08-01 16:26:23.000000000 +0200
11573 @@ -23,59 +23,80 @@
11574  */
11575  
11576  /*
11577 - *  $Id: hci.h,v 1.15 2001/08/05 06:02:15 maxk Exp $
11578 + *  $Id: hci.h,v 1.5 2002/06/27 17:29:30 maxk Exp $
11579   */
11580  
11581  #ifndef __HCI_H
11582  #define __HCI_H
11583  
11584 -#include <asm/byteorder.h>
11585 -
11586 -#define HCI_MAX_DEV    8
11587 -#define HCI_MAX_FRAME_SIZE     2048
11588 +#define HCI_MAX_ACL_SIZE       1024
11589 +#define HCI_MAX_SCO_SIZE       255
11590 +#define HCI_MAX_EVENT_SIZE     260
11591 +#define HCI_MAX_FRAME_SIZE     (HCI_MAX_ACL_SIZE + 4)
11592  
11593  /* HCI dev events */
11594  #define HCI_DEV_REG    1
11595  #define HCI_DEV_UNREG   2
11596  #define HCI_DEV_UP     3
11597  #define HCI_DEV_DOWN   4
11598 +#define HCI_DEV_SUSPEND        5
11599 +#define HCI_DEV_RESUME 6
11600  
11601  /* HCI device types */
11602 -#define HCI_UART       0
11603 +#define HCI_VHCI       0
11604  #define HCI_USB                1
11605 -#define HCI_VHCI       2
11606 -
11607 -/* HCI device modes */
11608 -#define HCI_NORMAL     0x0001
11609 -#define HCI_RAW                0x0002
11610 -#define HCI_MODE_MASK   (HCI_NORMAL | HCI_RAW)
11611 -#define HCI_SOCK       0x1000
11612 -
11613 -/* HCI device states */
11614 -#define HCI_INIT       0x0010
11615 -#define HCI_UP                 0x0020
11616 -#define HCI_RUNNING    0x0040
11617 +#define HCI_PCCARD     2
11618 +#define HCI_UART       3
11619 +#define HCI_RS232      4
11620 +#define HCI_PCI                5
11621 +
11622 +/* HCI device quirks */
11623 +enum {
11624 +       HCI_QUIRK_RESET_ON_INIT
11625 +};
11626  
11627  /* HCI device flags */
11628 -#define HCI_PSCAN      0x0100
11629 -#define HCI_ISCAN      0x0200
11630 -#define HCI_AUTH       0x0400
11631 +enum {
11632 +       HCI_UP,
11633 +       HCI_INIT,
11634 +       HCI_RUNNING,
11635 +
11636 +       HCI_PSCAN,
11637 +       HCI_ISCAN,
11638 +       HCI_AUTH,
11639 +       HCI_ENCRYPT,
11640 +       HCI_INQUIRY,
11641 +
11642 +       HCI_RAW
11643 +};
11644  
11645 -/* HCI Ioctl defines */
11646 +/* HCI ioctl defines */
11647  #define HCIDEVUP        _IOW('H', 201, int)
11648  #define HCIDEVDOWN      _IOW('H', 202, int)
11649  #define HCIDEVRESET     _IOW('H', 203, int)
11650 -#define HCIRESETSTAT    _IOW('H', 204, int)
11651 -#define HCIGETINFO      _IOR('H', 205, int)
11652 -#define HCIGETDEVLIST   _IOR('H', 206, int)
11653 -#define HCISETRAW       _IOW('H', 207, int)
11654 -#define HCISETSCAN      _IOW('H', 208, int)
11655 -#define HCISETAUTH      _IOW('H', 209, int)
11656 -#define HCIINQUIRY      _IOR('H', 210, int)
11657 -#define HCISETPTYPE     _IOW('H', 211, int)
11658 +#define HCIDEVRESTAT    _IOW('H', 204, int)
11659 +
11660 +#define HCIGETDEVLIST   _IOR('H', 210, int)
11661 +#define HCIGETDEVINFO   _IOR('H', 211, int)
11662  #define HCIGETCONNLIST  _IOR('H', 212, int)
11663 +#define HCIGETCONNINFO  _IOR('H', 213, int)
11664  
11665 -#ifndef __NO_HCI_DEFS
11666 +#define HCISETRAW       _IOW('H', 220, int)
11667 +#define HCISETSCAN      _IOW('H', 221, int)
11668 +#define HCISETAUTH      _IOW('H', 222, int)
11669 +#define HCISETENCRYPT   _IOW('H', 223, int)
11670 +#define HCISETPTYPE     _IOW('H', 224, int)
11671 +#define HCISETLINKPOL   _IOW('H', 225, int)
11672 +#define HCISETLINKMODE  _IOW('H', 226, int)
11673 +#define HCISETACLMTU    _IOW('H', 227, int)
11674 +#define HCISETSCOMTU    _IOW('H', 228, int)
11675 +
11676 +#define HCIINQUIRY      _IOR('H', 240, int)
11677 +
11678 +/* HCI timeouts */
11679 +#define HCI_CONN_TIMEOUT       (HZ * 40)
11680 +#define HCI_DISCONN_TIMEOUT    (HZ * 2)
11681 +#define HCI_CONN_IDLE_TIMEOUT  (HZ * 60)
11682  
11683  /* HCI Packet types */
11684  #define HCI_COMMAND_PKT                0x01
11685 @@ -92,11 +113,18 @@
11686  #define HCI_DH3        0x0800
11687  #define HCI_DH5        0x8000
11688  
11689 +#define HCI_HV1                0x0020
11690 +#define HCI_HV2                0x0040
11691 +#define HCI_HV3                0x0080
11692 +
11693 +#define SCO_PTYPE_MASK (HCI_HV1 | HCI_HV2 | HCI_HV3)
11694 +#define ACL_PTYPE_MASK (~SCO_PTYPE_MASK)
11695 +
11696  /* ACL flags */
11697 -#define ACL_CONT               0x0001
11698 -#define ACL_START              0x0002
11699 -#define ACL_ACTIVE_BCAST       0x0010
11700 -#define ACL_PICO_BCAST         0x0020
11701 +#define ACL_CONT               0x01
11702 +#define ACL_START              0x02
11703 +#define ACL_ACTIVE_BCAST       0x04
11704 +#define ACL_PICO_BCAST         0x08
11705  
11706  /* Baseband links */
11707  #define SCO_LINK       0x00
11708 @@ -125,6 +153,20 @@
11709  #define LMP_PSCHEME    0x02
11710  #define LMP_PCONTROL   0x04
11711  
11712 +/* Link policies */
11713 +#define HCI_LP_RSWITCH 0x0001
11714 +#define HCI_LP_HOLD    0x0002
11715 +#define HCI_LP_SNIFF   0x0004
11716 +#define HCI_LP_PARK    0x0008
11717 +
11718 +/* Link mode */
11719 +#define HCI_LM_ACCEPT  0x8000
11720 +#define HCI_LM_MASTER  0x0001
11721 +#define HCI_LM_AUTH    0x0002
11722 +#define HCI_LM_ENCRYPT 0x0004
11723 +#define HCI_LM_TRUSTED 0x0008
11724 +#define HCI_LM_RELIABLE        0x0010
11725 +
11726  /* -----  HCI Commands ----- */
11727  /* OGF & OCF values */
11728  
11729 @@ -137,9 +179,10 @@
11730         __u8  hci_ver;
11731         __u16 hci_rev;
11732         __u8  lmp_ver;
11733 -       __u16 man_name;
11734 -       __u16 lmp_sub;
11735 +       __u16 manufacturer;
11736 +       __u16 lmp_subver;
11737  } __attribute__ ((packed)) read_local_version_rp;
11738 +#define READ_LOCAL_VERSION_RP_SIZE 9
11739  
11740  #define OCF_READ_LOCAL_FEATURES        0x0003
11741  typedef struct {
11742 @@ -165,18 +208,24 @@
11743  /* Host Controller and Baseband */
11744  #define OGF_HOST_CTL   0x03
11745  #define OCF_RESET              0x0003
11746 +#define OCF_READ_AUTH_ENABLE   0x001F
11747  #define OCF_WRITE_AUTH_ENABLE  0x0020
11748 -       #define AUTH_DISABLED                   0x00
11749 -       #define AUTH_ENABLED                    0x01
11750 +       #define AUTH_DISABLED           0x00
11751 +       #define AUTH_ENABLED            0x01
11752 +
11753 +#define OCF_READ_ENCRYPT_MODE  0x0021
11754 +#define OCF_WRITE_ENCRYPT_MODE 0x0022
11755 +       #define ENCRYPT_DISABLED        0x00
11756 +       #define ENCRYPT_P2P             0x01
11757 +       #define ENCRYPT_BOTH            0x02
11758  
11759  #define OCF_WRITE_CA_TIMEOUT   0x0016  
11760  #define OCF_WRITE_PG_TIMEOUT   0x0018
11761  
11762  #define OCF_WRITE_SCAN_ENABLE  0x001A
11763 -       #define SCANS_DISABLED          0x00
11764 -       #define IS_ENA_PS_DIS           0x01
11765 -       #define IS_DIS_PS_ENA           0x02
11766 -       #define IS_ENA_PS_ENA           0x03
11767 +       #define SCAN_DISABLED           0x00
11768 +       #define SCAN_INQUIRY            0x01
11769 +       #define SCAN_PAGE               0x02
11770  
11771  #define OCF_SET_EVENT_FLT      0x0005
11772  typedef struct {
11773 @@ -226,9 +275,18 @@
11774  } __attribute__ ((packed)) write_class_of_dev_cp;
11775  #define WRITE_CLASS_OF_DEV_CP_SIZE 3
11776  
11777 +#define OCF_HOST_BUFFER_SIZE   0x0033
11778 +typedef struct {
11779 +       __u16   acl_mtu;
11780 +       __u8    sco_mtu;
11781 +       __u16   acl_max_pkt;
11782 +       __u16   sco_max_pkt;
11783 +} __attribute__ ((packed)) host_buffer_size_cp;
11784 +#define HOST_BUFFER_SIZE_CP_SIZE 7
11785 +
11786  /* Link Control */
11787  #define OGF_LINK_CTL   0x01 
11788 -#define OCF_CREATE_CONN        0x0005
11789 +#define OCF_CREATE_CONN                0x0005
11790  typedef struct {
11791         bdaddr_t bdaddr;
11792         __u16   pkt_type;
11793 @@ -246,6 +304,13 @@
11794  } __attribute__ ((packed)) accept_conn_req_cp;
11795  #define ACCEPT_CONN_REQ_CP_SIZE        7
11796  
11797 +#define OCF_REJECT_CONN_REQ    0x000a
11798 +typedef struct {
11799 +       bdaddr_t bdaddr;
11800 +       __u8    reason;
11801 +} __attribute__ ((packed)) reject_conn_req_cp;
11802 +#define REJECT_CONN_REQ_CP_SIZE        7
11803 +
11804  #define OCF_DISCONNECT 0x0006
11805  typedef struct {
11806         __u16   handle;
11807 @@ -253,17 +318,142 @@
11808  } __attribute__ ((packed)) disconnect_cp;
11809  #define DISCONNECT_CP_SIZE 3
11810  
11811 +#define OCF_ADD_SCO    0x0007
11812 +typedef struct {
11813 +       __u16   handle;
11814 +       __u16   pkt_type;
11815 +} __attribute__ ((packed)) add_sco_cp;
11816 +#define ADD_SCO_CP_SIZE 4
11817 +
11818  #define OCF_INQUIRY            0x0001
11819  typedef struct {
11820         __u8    lap[3];
11821 -       __u8    lenght;
11822 +       __u8    length;
11823         __u8    num_rsp;
11824  } __attribute__ ((packed)) inquiry_cp;
11825  #define INQUIRY_CP_SIZE 5
11826  
11827 -#define OGF_LINK_POLICY                0x02   /* Link Policy */
11828 +typedef struct {
11829 +       __u8     status;
11830 +       bdaddr_t bdaddr;
11831 +} __attribute__ ((packed)) status_bdaddr_rp;
11832 +#define STATUS_BDADDR_RP_SIZE 7
11833 +
11834 +#define OCF_INQUIRY_CANCEL     0x0002
11835 +
11836 +#define OCF_LINK_KEY_REPLY     0x000B
11837 +#define OCF_LINK_KEY_NEG_REPLY 0x000C
11838 +typedef struct {
11839 +       bdaddr_t bdaddr;
11840 +       __u8     link_key[16];
11841 +} __attribute__ ((packed)) link_key_reply_cp;
11842 +#define LINK_KEY_REPLY_CP_SIZE 22
11843 +
11844 +#define OCF_PIN_CODE_REPLY     0x000D
11845 +#define OCF_PIN_CODE_NEG_REPLY 0x000E
11846 +typedef struct {
11847 +       bdaddr_t bdaddr;
11848 +       __u8     pin_len;
11849 +       __u8     pin_code[16];
11850 +} __attribute__ ((packed)) pin_code_reply_cp;
11851 +#define PIN_CODE_REPLY_CP_SIZE 23
11852 +
11853 +#define OCF_CHANGE_CONN_PTYPE  0x000F
11854 +typedef struct {
11855 +       __u16    handle;
11856 +       __u16    pkt_type;
11857 +} __attribute__ ((packed)) change_conn_ptype_cp;
11858 +#define CHANGE_CONN_PTYPE_CP_SIZE 4
11859 +
11860 +#define OCF_AUTH_REQUESTED     0x0011
11861 +typedef struct {
11862 +       __u16    handle;
11863 +} __attribute__ ((packed)) auth_requested_cp;
11864 +#define AUTH_REQUESTED_CP_SIZE 2
11865 +
11866 +#define OCF_SET_CONN_ENCRYPT   0x0013
11867 +typedef struct {
11868 +       __u16    handle;
11869 +       __u8     encrypt;
11870 +} __attribute__ ((packed)) set_conn_encrypt_cp;
11871 +#define SET_CONN_ENCRYPT_CP_SIZE 3
11872 +
11873 +#define OCF_REMOTE_NAME_REQ    0x0019
11874 +typedef struct {
11875 +       bdaddr_t bdaddr;
11876 +       __u8     pscan_rep_mode;
11877 +       __u8     pscan_mode;
11878 +       __u16    clock_offset;
11879 +} __attribute__ ((packed)) remote_name_req_cp;
11880 +#define REMOTE_NAME_REQ_CP_SIZE 10
11881 +
11882 +#define OCF_READ_REMOTE_FEATURES 0x001B
11883 +typedef struct {
11884 +       __u16   handle;
11885 +} __attribute__ ((packed)) read_remote_features_cp;
11886 +#define READ_REMOTE_FEATURES_CP_SIZE 2
11887 +
11888 +#define OCF_READ_REMOTE_VERSION 0x001D
11889 +typedef struct {
11890 +       __u16   handle;
11891 +} __attribute__ ((packed)) read_remote_version_cp;
11892 +#define READ_REMOTE_VERSION_CP_SIZE 2
11893 +
11894 +/* Link Policy */
11895 +#define OGF_LINK_POLICY         0x02   
11896 +#define OCF_ROLE_DISCOVERY     0x0009
11897 +typedef struct {
11898 +       __u16   handle;
11899 +} __attribute__ ((packed)) role_discovery_cp;
11900 +#define ROLE_DISCOVERY_CP_SIZE 2
11901 +typedef struct {
11902 +       __u8    status;
11903 +       __u16   handle;
11904 +       __u8    role;
11905 +} __attribute__ ((packed)) role_discovery_rp;
11906 +#define ROLE_DISCOVERY_RP_SIZE 4
11907 +
11908 +#define OCF_READ_LINK_POLICY   0x000C
11909 +typedef struct {
11910 +       __u16   handle;
11911 +} __attribute__ ((packed)) read_link_policy_cp;
11912 +#define READ_LINK_POLICY_CP_SIZE 2
11913 +typedef struct {
11914 +       __u8    status;
11915 +       __u16   handle;
11916 +       __u16   policy;
11917 +} __attribute__ ((packed)) read_link_policy_rp;
11918 +#define READ_LINK_POLICY_RP_SIZE 5
11919  
11920 -/* --------- HCI Events --------- */
11921 +#define OCF_SWITCH_ROLE        0x000B
11922 +typedef struct {
11923 +       bdaddr_t bdaddr;
11924 +       __u8     role;
11925 +} __attribute__ ((packed)) switch_role_cp;
11926 +#define SWITCH_ROLE_CP_SIZE 7
11927 +
11928 +#define OCF_WRITE_LINK_POLICY  0x000D
11929 +typedef struct {
11930 +       __u16   handle;
11931 +       __u16   policy;
11932 +} __attribute__ ((packed)) write_link_policy_cp;
11933 +#define WRITE_LINK_POLICY_CP_SIZE 4
11934 +typedef struct {
11935 +       __u8    status;
11936 +       __u16   handle;
11937 +} __attribute__ ((packed)) write_link_policy_rp;
11938 +#define WRITE_LINK_POLICY_RP_SIZE 3
11939 +
11940 +/* Status params */
11941 +#define OGF_STATUS_PARAM       0x05
11942 +
11943 +/* Testing commands */
11944 +#define OGF_TESTING_CMD        0x3e
11945 +
11946 +/* Vendor specific commands */
11947 +#define OGF_VENDOR_CMD         0x3f
11948 +
11949 +/* ---- HCI Events ---- */
11950  #define EVT_INQUIRY_COMPLETE   0x01
11951  
11952  #define EVT_INQUIRY_RESULT     0x02
11953 @@ -272,11 +462,22 @@
11954         __u8    pscan_rep_mode;
11955         __u8    pscan_period_mode;
11956         __u8    pscan_mode;
11957 -       __u8    class[3];
11958 +       __u8    dev_class[3];
11959         __u16   clock_offset;
11960  } __attribute__ ((packed)) inquiry_info;
11961  #define INQUIRY_INFO_SIZE 14
11962  
11963 +#define EVT_INQUIRY_RESULT_WITH_RSSI   0x22
11964 +typedef struct {
11965 +       bdaddr_t        bdaddr;
11966 +       __u8    pscan_rep_mode;
11967 +       __u8    pscan_period_mode;
11968 +       __u8    dev_class[3];
11969 +       __u16   clock_offset;
11970 +       __s8    rssi;
11971 +} __attribute__ ((packed)) inquiry_info_with_rssi;
11972 +#define INQUIRY_INFO_WITH_RSSI_SIZE 14
11973 +
11974  #define EVT_CONN_COMPLETE      0x03
11975  typedef struct {
11976         __u8    status;
11977 @@ -303,6 +504,44 @@
11978  } __attribute__ ((packed)) evt_disconn_complete;
11979  #define EVT_DISCONN_COMPLETE_SIZE 4
11980  
11981 +#define EVT_AUTH_COMPLETE      0x06
11982 +typedef struct {
11983 +       __u8    status;
11984 +       __u16   handle;
11985 +} __attribute__ ((packed)) evt_auth_complete;
11986 +#define EVT_AUTH_COMPLETE_SIZE 3
11987 +
11988 +#define EVT_REMOTE_NAME_REQ_COMPLETE   0x07
11989 +typedef struct {
11990 +       __u8     status;
11991 +       bdaddr_t bdaddr;
11992 +       __u8     name[248];
11993 +} __attribute__ ((packed)) evt_remote_name_req_complete;
11994 +#define EVT_REMOTE_NAME_REQ_COMPLETE_SIZE 255
11995 +
11996 +#define EVT_ENCRYPT_CHANGE     0x08
11997 +typedef struct {
11998 +       __u8    status;
11999 +       __u16   handle;
12000 +       __u8    encrypt;
12001 +} __attribute__ ((packed)) evt_encrypt_change;
12002 +#define EVT_ENCRYPT_CHANGE_SIZE 5
12003 +
12004 +#define EVT_QOS_SETUP_COMPLETE 0x0D
12005 +typedef struct {
12006 +       __u8    service_type;
12007 +       __u32   token_rate;
12008 +       __u32   peak_bandwidth;
12009 +       __u32   latency;
12010 +       __u32   delay_variation;
12011 +} __attribute__ ((packed)) hci_qos;
12012 +typedef struct {
12013 +       __u8    status;
12014 +       __u16   handle;
12015 +       hci_qos qos;
12016 +} __attribute__ ((packed)) evt_qos_setup_complete;
12017 +#define EVT_QOS_SETUP_COMPLETE_SIZE 20
12018 +
12019  #define EVT_CMD_COMPLETE       0x0e
12020  typedef struct {
12021         __u8    ncmd;
12022 @@ -321,16 +560,78 @@
12023  #define EVT_NUM_COMP_PKTS      0x13
12024  typedef struct {
12025         __u8    num_hndl;
12026 -       /* variable lenght part */
12027 +       /* variable length part */
12028  } __attribute__ ((packed)) evt_num_comp_pkts;
12029  #define EVT_NUM_COMP_PKTS_SIZE 1
12030  
12031 -#define EVT_HCI_DEV_EVENT      0xfd
12032 +#define EVT_ROLE_CHANGE                0x12
12033 +typedef struct {
12034 +       __u8     status;
12035 +       bdaddr_t bdaddr;
12036 +       __u8     role;
12037 +} __attribute__ ((packed)) evt_role_change;
12038 +#define EVT_ROLE_CHANGE_SIZE 8
12039 +
12040 +#define EVT_PIN_CODE_REQ        0x16
12041 +typedef struct {
12042 +       bdaddr_t bdaddr;
12043 +} __attribute__ ((packed)) evt_pin_code_req;
12044 +#define EVT_PIN_CODE_REQ_SIZE 6
12045 +
12046 +#define EVT_LINK_KEY_REQ        0x17
12047 +typedef struct {
12048 +       bdaddr_t bdaddr;
12049 +} __attribute__ ((packed)) evt_link_key_req;
12050 +#define EVT_LINK_KEY_REQ_SIZE 6
12051 +
12052 +#define EVT_LINK_KEY_NOTIFY    0x18
12053 +typedef struct {
12054 +       bdaddr_t bdaddr;
12055 +       __u8     link_key[16];
12056 +       __u8     key_type;
12057 +} __attribute__ ((packed)) evt_link_key_notify;
12058 +#define EVT_LINK_KEY_NOTIFY_SIZE 23
12059 +
12060 +#define EVT_READ_REMOTE_FEATURES_COMPLETE 0x0B
12061 +typedef struct {
12062 +       __u8    status;
12063 +       __u16   handle;
12064 +       __u8    features[8];
12065 +} __attribute__ ((packed)) evt_read_remote_features_complete;
12066 +#define EVT_READ_REMOTE_FEATURES_COMPLETE_SIZE 11
12067 +
12068 +#define EVT_READ_REMOTE_VERSION_COMPLETE 0x0C
12069 +typedef struct {
12070 +       __u8    status;
12071 +       __u16   handle;
12072 +       __u8    lmp_ver;
12073 +       __u16   manufacturer;
12074 +       __u16   lmp_subver;
12075 +} __attribute__ ((packed)) evt_read_remote_version_complete;
12076 +#define EVT_READ_REMOTE_VERSION_COMPLETE_SIZE 8
12077 +
12078 +/* Internal events generated by BlueZ stack */
12079 +#define EVT_STACK_INTERNAL     0xfd
12080 +typedef struct {
12081 +       __u16   type;
12082 +       __u8    data[0];
12083 +} __attribute__ ((packed)) evt_stack_internal;
12084 +#define EVT_STACK_INTERNAL_SIZE 2
12085 +
12086 +#define EVT_SI_DEVICE          0x01
12087 +typedef struct {
12088 +       __u16   event;
12089 +       __u16   dev_id;
12090 +} __attribute__ ((packed)) evt_si_device;
12091 +#define EVT_SI_DEVICE_SIZE 4
12092 +
12093 +#define EVT_SI_SECURITY        0x02
12094  typedef struct {
12095         __u16   event;
12096 -       __u16   param;
12097 -} __attribute__ ((packed)) evt_hci_dev_event;
12098 -#define EVT_HCI_DEV_EVENT_SIZE 4
12099 +       __u16   proto;
12100 +       __u16   subproto;
12101 +       __u8    incomming;
12102 +} __attribute__ ((packed)) evt_si_security;
12103  
12104  /* --------  HCI Packet structures  -------- */
12105  #define HCI_TYPE_LEN   1
12106 @@ -369,14 +670,14 @@
12107  #define acl_handle(h)          (h & 0x0fff)
12108  #define acl_flags(h)           (h >> 12)
12109  
12110 -#endif /* _NO_HCI_DEFS */
12111 -
12112  /* HCI Socket options */
12113 -#define HCI_DATA_DIR   0x0001
12114 -#define HCI_FILTER     0x0002
12115 +#define HCI_DATA_DIR   1
12116 +#define HCI_FILTER     2
12117 +#define HCI_TIME_STAMP 3
12118  
12119  /* HCI CMSG flags */
12120  #define HCI_CMSG_DIR   0x0001
12121 +#define HCI_CMSG_TSTAMP        0x0002
12122  
12123  struct sockaddr_hci {
12124         sa_family_t    hci_family;
12125 @@ -387,27 +688,29 @@
12126  struct hci_filter {
12127         __u32 type_mask;
12128         __u32 event_mask[2];
12129 +       __u16 opcode;
12130  };
12131  
12132 -struct hci_dev_req {
12133 -       __u16 dev_id;
12134 -       __u32 dev_opt;
12135 -};
12136 -
12137 -struct hci_dev_list_req {
12138 -       __u16  dev_num;
12139 -       struct hci_dev_req dev_req[0];  /* hci_dev_req structures */
12140 -};
12141 -
12142 -struct hci_inquiry_req {
12143 -       __u16 dev_id;
12144 -       __u16 flags;
12145 -       __u8  lap[3];
12146 -       __u8  length;
12147 -       __u8  num_rsp;
12148 -};
12149 -#define IREQ_CACHE_FLUSH 0x0001
12150 +#define HCI_FLT_TYPE_BITS      31
12151 +#define HCI_FLT_EVENT_BITS     63
12152 +#define HCI_FLT_OGF_BITS       63
12153 +#define HCI_FLT_OCF_BITS       127
12154 +
12155 +#if BITS_PER_LONG == 64
12156 +static inline void hci_set_bit(int nr, void *addr)
12157 +{
12158 +       *((__u32 *) addr + (nr >> 5)) |= ((__u32) 1 << (nr & 31));
12159 +}
12160 +static inline int hci_test_bit(int nr, void *addr)
12161 +{
12162 +       return *((__u32 *) addr + (nr >> 5)) & ((__u32) 1 << (nr & 31));
12163 +}
12164 +#else
12165 +#define hci_set_bit    set_bit
12166 +#define hci_test_bit   test_bit
12167 +#endif
12168  
12169 +/* Ioctl requests structures */
12170  struct hci_dev_stats {
12171         __u32 err_rx;
12172         __u32 err_tx;
12173 @@ -433,11 +736,13 @@
12174         __u8  features[8];
12175  
12176         __u32 pkt_type;
12177 +       __u32 link_policy;
12178 +       __u32 link_mode;
12179  
12180         __u16 acl_mtu;
12181 -       __u16 acl_max;
12182 +       __u16 acl_pkts;
12183         __u16 sco_mtu;
12184 -       __u16 sco_max;
12185 +       __u16 sco_pkts;
12186  
12187         struct hci_dev_stats stat;
12188  };
12189 @@ -445,6 +750,20 @@
12190  struct hci_conn_info {
12191         __u16    handle;
12192         bdaddr_t bdaddr;
12193 +       __u8     type;
12194 +       __u8     out;
12195 +       __u16    state;
12196 +       __u32    link_mode;
12197 +};
12198 +
12199 +struct hci_dev_req {
12200 +       __u16 dev_id;
12201 +       __u32 dev_opt;
12202 +};
12203 +
12204 +struct hci_dev_list_req {
12205 +       __u16  dev_num;
12206 +       struct hci_dev_req dev_req[0];  /* hci_dev_req structures */
12207  };
12208  
12209  struct hci_conn_list_req {
12210 @@ -453,4 +772,26 @@
12211         struct hci_conn_info conn_info[0];
12212  };
12213  
12214 +struct hci_conn_info_req {
12215 +       bdaddr_t bdaddr;
12216 +       __u8     type;
12217 +       struct   hci_conn_info conn_info[0];
12218 +};
12219 +
12220 +struct hci_inquiry_req {
12221 +       __u16 dev_id;
12222 +       __u16 flags;
12223 +       __u8  lap[3];
12224 +       __u8  length;
12225 +       __u8  num_rsp;
12226 +};
12227 +#define IREQ_CACHE_FLUSH 0x0001
12228 +
12229 +struct hci_remotename_req {
12230 +       __u16 dev_id;
12231 +       __u16 flags;
12232 +       bdaddr_t bdaddr;
12233 +       __u8  name[248];
12234 +};
12235 +
12236  #endif /* __HCI_H */
12237 diff -urN linux-2.4.18/include/net/bluetooth/hci_uart.h linux-2.4.18-mh15/include/net/bluetooth/hci_uart.h
12238 --- linux-2.4.18/include/net/bluetooth/hci_uart.h       2001-09-07 18:28:38.000000000 +0200
12239 +++ linux-2.4.18-mh15/include/net/bluetooth/hci_uart.h  1970-01-01 01:00:00.000000000 +0100
12240 @@ -1,62 +0,0 @@
12241 -/* 
12242 -   BlueZ - Bluetooth protocol stack for Linux
12243 -   Copyright (C) 2000-2001 Qualcomm Incorporated
12244 -
12245 -   Written 2000,2001 by Maxim Krasnyansky <maxk@qualcomm.com>
12246 -
12247 -   This program is free software; you can redistribute it and/or modify
12248 -   it under the terms of the GNU General Public License version 2 as
12249 -   published by the Free Software Foundation;
12250 -
12251 -   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
12252 -   OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
12253 -   FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS.
12254 -   IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) AND AUTHOR(S) BE LIABLE FOR ANY
12255 -   CLAIM, OR ANY SPECIAL INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES 
12256 -   WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 
12257 -   ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 
12258 -   OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
12259 -
12260 -   ALL LIABILITY, INCLUDING LIABILITY FOR INFRINGEMENT OF ANY PATENTS, 
12261 -   COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS, RELATING TO USE OF THIS 
12262 -   SOFTWARE IS DISCLAIMED.
12263 -*/
12264 -
12265 -/*
12266 - * $Id: hci_uart.h,v 1.2 2001/06/02 01:40:08 maxk Exp $
12267 - */
12268 -
12269 -#ifndef N_HCI
12270 -#define N_HCI  15
12271 -#endif
12272 -
12273 -#ifdef __KERNEL__
12274 -
12275 -#define tty2n_hci(tty)  ((struct n_hci *)((tty)->disc_data))
12276 -#define n_hci2tty(n_hci) ((n_hci)->tty)
12277 -
12278 -struct n_hci {
12279 -       struct tty_struct *tty;
12280 -       struct hci_dev hdev;
12281 -
12282 -       struct sk_buff_head txq;
12283 -       unsigned long tx_state;
12284 -
12285 -       spinlock_t rx_lock;
12286 -       unsigned long rx_state;
12287 -       unsigned long rx_count;
12288 -       struct sk_buff *rx_skb;
12289 -};
12290 -
12291 -/* Transmit states  */
12292 -#define TRANS_SENDING          1
12293 -#define TRANS_WAKEUP           2
12294 -
12295 -/* Receiver States */
12296 -#define WAIT_PACKET_TYPE       0
12297 -#define WAIT_EVENT_HDR         1
12298 -#define WAIT_ACL_HDR           2
12299 -#define WAIT_SCO_HDR           3
12300 -#define WAIT_DATA              4
12301 -
12302 -#endif /* __KERNEL__ */
12303 diff -urN linux-2.4.18/include/net/bluetooth/hci_usb.h linux-2.4.18-mh15/include/net/bluetooth/hci_usb.h
12304 --- linux-2.4.18/include/net/bluetooth/hci_usb.h        2001-09-07 18:28:38.000000000 +0200
12305 +++ linux-2.4.18-mh15/include/net/bluetooth/hci_usb.h   1970-01-01 01:00:00.000000000 +0100
12306 @@ -1,68 +0,0 @@
12307 -/* 
12308 -   BlueZ - Bluetooth protocol stack for Linux
12309 -   Copyright (C) 2000-2001 Qualcomm Incorporated
12310 -
12311 -   Written 2000,2001 by Maxim Krasnyansky <maxk@qualcomm.com>
12312 -
12313 -   This program is free software; you can redistribute it and/or modify
12314 -   it under the terms of the GNU General Public License version 2 as
12315 -   published by the Free Software Foundation;
12316 -
12317 -   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
12318 -   OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
12319 -   FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS.
12320 -   IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) AND AUTHOR(S) BE LIABLE FOR ANY
12321 -   CLAIM, OR ANY SPECIAL INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES 
12322 -   WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 
12323 -   ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 
12324 -   OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
12325 -
12326 -   ALL LIABILITY, INCLUDING LIABILITY FOR INFRINGEMENT OF ANY PATENTS, 
12327 -   COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS, RELATING TO USE OF THIS 
12328 -   SOFTWARE IS DISCLAIMED.
12329 -*/
12330 -
12331 -/*
12332 - * $Id: hci_usb.h,v 1.3 2001/06/02 01:40:08 maxk Exp $
12333 - */
12334 -
12335 -#ifdef __KERNEL__
12336 -
12337 -/* Class, SubClass, and Protocol codes that describe a Bluetooth device */
12338 -#define HCI_DEV_CLASS        0xe0      /* Wireless class */
12339 -#define HCI_DEV_SUBCLASS     0x01      /* RF subclass */
12340 -#define HCI_DEV_PROTOCOL     0x01      /* Bluetooth programming protocol */
12341 -
12342 -#define HCI_CTRL_REQ        0x20
12343 -
12344 -struct hci_usb {
12345 -       struct usb_device       *udev;
12346 -
12347 -       devrequest              dev_req;
12348 -       struct urb              *ctrl_urb;
12349 -       struct urb              *intr_urb;
12350 -       struct urb              *read_urb;
12351 -       struct urb              *write_urb;
12352 -
12353 -       __u8                    *read_buf;
12354 -       __u8                    *intr_buf;
12355 -       struct sk_buff          *intr_skb;
12356 -       int                     intr_count;
12357 -
12358 -       __u8                    bulk_out_ep_addr;
12359 -       __u8                    bulk_in_ep_addr;
12360 -       __u8                    intr_in_ep_addr;
12361 -       __u8                    intr_in_interval;
12362 -
12363 -       struct hci_dev          hdev;
12364 -
12365 -       unsigned long           tx_state;
12366 -       struct sk_buff_head     tx_ctrl_q;
12367 -       struct sk_buff_head     tx_write_q;
12368 -};
12369 -
12370 -/* Transmit states  */
12371 -#define HCI_TX_CTRL    1
12372 -#define HCI_TX_WRITE   2
12373 -
12374 -#endif /* __KERNEL__ */
12375 diff -urN linux-2.4.18/include/net/bluetooth/hci_vhci.h linux-2.4.18-mh15/include/net/bluetooth/hci_vhci.h
12376 --- linux-2.4.18/include/net/bluetooth/hci_vhci.h       2001-09-07 18:28:38.000000000 +0200
12377 +++ linux-2.4.18-mh15/include/net/bluetooth/hci_vhci.h  1970-01-01 01:00:00.000000000 +0100
12378 @@ -1,50 +0,0 @@
12379 -/* 
12380 -   BlueZ - Bluetooth protocol stack for Linux
12381 -   Copyright (C) 2000-2001 Qualcomm Incorporated
12382 -
12383 -   Written 2000,2001 by Maxim Krasnyansky <maxk@qualcomm.com>
12384 -
12385 -   This program is free software; you can redistribute it and/or modify
12386 -   it under the terms of the GNU General Public License version 2 as
12387 -   published by the Free Software Foundation;
12388 -
12389 -   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
12390 -   OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
12391 -   FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS.
12392 -   IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) AND AUTHOR(S) BE LIABLE FOR ANY
12393 -   CLAIM, OR ANY SPECIAL INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES 
12394 -   WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 
12395 -   ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 
12396 -   OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
12397 -
12398 -   ALL LIABILITY, INCLUDING LIABILITY FOR INFRINGEMENT OF ANY PATENTS, 
12399 -   COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS, RELATING TO USE OF THIS 
12400 -   SOFTWARE IS DISCLAIMED.
12401 -*/
12402 -
12403 -/*
12404 - * $Id: hci_vhci.h,v 1.2 2001/08/01 01:02:20 maxk Exp $
12405 - */
12406 -
12407 -#ifndef __HCI_VHCI_H
12408 -#define __HCI_VHCI_H
12409 -
12410 -#ifdef __KERNEL__
12411 -
12412 -struct hci_vhci_struct {
12413 -       struct hci_dev       hdev;
12414 -       __u32                flags;
12415 -       wait_queue_head_t    read_wait;
12416 -       struct sk_buff_head  readq;
12417 -       struct fasync_struct *fasync;
12418 -};
12419 -
12420 -/* VHCI device flags */
12421 -#define VHCI_FASYNC            0x0010
12422 -
12423 -#endif /* __KERNEL__ */
12424 -
12425 -#define VHCI_DEV       "/dev/vhci"
12426 -#define VHCI_MINOR     250
12427 -
12428 -#endif /* __HCI_VHCI_H */
12429 diff -urN linux-2.4.18/include/net/bluetooth/l2cap_core.h linux-2.4.18-mh15/include/net/bluetooth/l2cap_core.h
12430 --- linux-2.4.18/include/net/bluetooth/l2cap_core.h     2001-09-07 18:28:38.000000000 +0200
12431 +++ linux-2.4.18-mh15/include/net/bluetooth/l2cap_core.h        1970-01-01 01:00:00.000000000 +0100
12432 @@ -1,144 +0,0 @@
12433 -/* 
12434 -   BlueZ - Bluetooth protocol stack for Linux
12435 -   Copyright (C) 2000-2001 Qualcomm Incorporated
12436 -
12437 -   Written 2000,2001 by Maxim Krasnyansky <maxk@qualcomm.com>
12438 -
12439 -   This program is free software; you can redistribute it and/or modify
12440 -   it under the terms of the GNU General Public License version 2 as
12441 -   published by the Free Software Foundation;
12442 -
12443 -   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
12444 -   OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
12445 -   FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS.
12446 -   IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) AND AUTHOR(S) BE LIABLE FOR ANY
12447 -   CLAIM, OR ANY SPECIAL INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES 
12448 -   WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 
12449 -   ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 
12450 -   OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
12451 -
12452 -   ALL LIABILITY, INCLUDING LIABILITY FOR INFRINGEMENT OF ANY PATENTS, 
12453 -   COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS, RELATING TO USE OF THIS 
12454 -   SOFTWARE IS DISCLAIMED.
12455 -*/
12456 -
12457 -/*
12458 - *  $Id: l2cap_core.h,v 1.6 2001/08/03 04:19:49 maxk Exp $
12459 - */
12460 -
12461 -#ifndef __L2CAP_CORE_H
12462 -#define __L2CAP_CORE_H
12463 -
12464 -#ifdef __KERNEL__
12465 -
12466 -/* ----- L2CAP interface ----- */
12467 -struct l2cap_iff {
12468 -       struct list_head list;
12469 -       struct hci_dev   *hdev;
12470 -       bdaddr_t         *bdaddr;
12471 -       __u16            mtu;
12472 -       spinlock_t       lock;
12473 -       struct list_head conn_list;
12474 -};
12475 -
12476 -static inline void l2cap_iff_lock(struct l2cap_iff *iff)
12477 -{
12478 -       spin_lock(&iff->lock);
12479 -}
12480 -
12481 -static inline void l2cap_iff_unlock(struct l2cap_iff *iff)
12482 -{
12483 -       spin_unlock(&iff->lock);
12484 -}
12485 -
12486 -/* ----- L2CAP connections ----- */
12487 -struct l2cap_chan_list {
12488 -       struct sock     *head;
12489 -       rwlock_t        lock;
12490 -       long            num;
12491 -};
12492 -
12493 -struct l2cap_conn {
12494 -       struct l2cap_iff *iff;
12495 -       struct list_head list;
12496 -
12497 -       struct hci_conn  *hconn;
12498 -
12499 -       __u16           state;
12500 -       __u8            out;
12501 -       bdaddr_t        src;
12502 -       bdaddr_t        dst;
12503 -
12504 -       spinlock_t      lock;
12505 -       atomic_t        refcnt;
12506 -
12507 -       struct sk_buff *rx_skb;
12508 -       __u32           rx_len;
12509 -       __u8            rx_ident;
12510 -       __u8            tx_ident;
12511 -
12512 -       struct l2cap_chan_list chan_list;
12513 -
12514 -       struct timer_list timer;
12515 -};
12516 -
12517 -static inline void __l2cap_conn_link(struct l2cap_iff *iff, struct l2cap_conn *c)
12518 -{
12519 -       list_add(&c->list, &iff->conn_list);
12520 -}
12521 -
12522 -static inline void __l2cap_conn_unlink(struct l2cap_iff *iff, struct l2cap_conn *c)
12523 -{
12524 -       list_del(&c->list);
12525 -}
12526 -
12527 -/* ----- L2CAP channel and socket info ----- */
12528 -#define l2cap_pi(sk)   ((struct l2cap_pinfo *) &sk->protinfo)
12529 -
12530 -struct l2cap_accept_q {
12531 -       struct sock     *head;
12532 -       struct sock     *tail;
12533 -};
12534 -
12535 -struct l2cap_pinfo {
12536 -       bdaddr_t        src;
12537 -       bdaddr_t        dst;
12538 -       __u16           psm;
12539 -       __u16           dcid;
12540 -       __u16           scid;
12541 -       __u32           flags;
12542 -
12543 -       __u16           imtu;
12544 -       __u16           omtu;
12545 -       __u16           flush_to;
12546 -
12547 -       __u8            conf_state;
12548 -       __u16           conf_mtu;
12549 -
12550 -       __u8            ident;
12551 -
12552 -       struct l2cap_conn       *conn;
12553 -       struct sock             *next_c;
12554 -       struct sock             *prev_c;
12555 -
12556 -       struct sock *parent;
12557 -       struct sock *next_q;
12558 -       struct sock *prev_q;
12559 -
12560 -       struct l2cap_accept_q accept_q;
12561 -};
12562 -
12563 -#define CONF_REQ_SENT    0x01
12564 -#define CONF_INPUT_DONE  0x02
12565 -#define CONF_OUTPUT_DONE 0x04
12566 -
12567 -extern struct bluez_sock_list l2cap_sk_list;
12568 -extern struct list_head  l2cap_iff_list;
12569 -extern rwlock_t l2cap_rt_lock;
12570 -
12571 -extern void l2cap_register_proc(void);
12572 -extern void l2cap_unregister_proc(void);
12573 -
12574 -#endif /* __KERNEL__ */
12575 -
12576 -#endif /* __L2CAP_CORE_H */
12577 diff -urN linux-2.4.18/include/net/bluetooth/l2cap.h linux-2.4.18-mh15/include/net/bluetooth/l2cap.h
12578 --- linux-2.4.18/include/net/bluetooth/l2cap.h  2001-09-07 18:28:38.000000000 +0200
12579 +++ linux-2.4.18-mh15/include/net/bluetooth/l2cap.h     2004-08-01 16:26:23.000000000 +0200
12580 @@ -23,22 +23,17 @@
12581  */
12582  
12583  /*
12584 - *  $Id: l2cap.h,v 1.5 2001/06/14 21:28:26 maxk Exp $
12585 + *  $Id: l2cap.h,v 1.1.1.1 2002/03/08 21:03:15 maxk Exp $
12586   */
12587  
12588  #ifndef __L2CAP_H
12589  #define __L2CAP_H
12590  
12591 -#include <asm/types.h>
12592 -#include <asm/byteorder.h>
12593 -
12594  /* L2CAP defaults */
12595  #define L2CAP_DEFAULT_MTU      672
12596  #define L2CAP_DEFAULT_FLUSH_TO 0xFFFF
12597  
12598  #define L2CAP_CONN_TIMEOUT     (HZ * 40)
12599 -#define L2CAP_DISCONN_TIMEOUT  (HZ * 2)
12600 -#define L2CAP_CONN_IDLE_TIMEOUT        (HZ * 60)
12601  
12602  /* L2CAP socket address */
12603  struct sockaddr_l2 {
12604 @@ -47,17 +42,12 @@
12605         bdaddr_t        l2_bdaddr;
12606  };
12607  
12608 -/* set/get sockopt defines */
12609 -#define L2CAP_OPTIONS  0x01
12610 +/* Socket options */
12611 +#define L2CAP_OPTIONS  0x01
12612  struct l2cap_options {
12613         __u16 omtu;
12614         __u16 imtu;
12615         __u16 flush_to;
12616 -       __u32 token_rate;
12617 -       __u32 bucket_size;
12618 -       __u32 pick_band;
12619 -       __u32 latency;
12620 -       __u32 delay_var;
12621  };
12622  
12623  #define L2CAP_CONNINFO  0x02
12624 @@ -65,6 +55,27 @@
12625         __u16 hci_handle;
12626  };
12627  
12628 +#define L2CAP_LM       0x03
12629 +#define L2CAP_LM_MASTER                0x0001
12630 +#define L2CAP_LM_AUTH          0x0002
12631 +#define L2CAP_LM_ENCRYPT       0x0004
12632 +#define L2CAP_LM_TRUSTED       0x0008
12633 +#define L2CAP_LM_RELIABLE      0x0010
12634 +
12635 +#define L2CAP_QOS      0x04
12636 +struct l2cap_qos {
12637 +       __u16 service_type;
12638 +       __u32 token_rate;
12639 +       __u32 token_bucket_size;
12640 +       __u32 peak_bandwidth;
12641 +       __u32 latency;
12642 +       __u32 delay_variation;
12643 +};
12644 +
12645 +#define L2CAP_SERV_NO_TRAFFIC  0x00
12646 +#define L2CAP_SERV_BEST_EFFORT 0x01
12647 +#define L2CAP_SERV_GUARANTEED  0x02
12648 +
12649  /* L2CAP command codes */
12650  #define L2CAP_COMMAND_REJ 0x01
12651  #define L2CAP_CONN_REQ    0x02
12652 @@ -79,7 +90,6 @@
12653  #define L2CAP_INFO_RSP    0x0b
12654  
12655  /* L2CAP structures */
12656 -
12657  typedef struct {
12658         __u16      len;
12659         __u16      cid;
12660 @@ -112,11 +122,17 @@
12661  } __attribute__ ((packed))     l2cap_conn_rsp;
12662  #define L2CAP_CONN_RSP_SIZE    8
12663  
12664 -#define L2CAP_CONN_SUCCESS    0x0000
12665 -#define L2CAP_CONN_PEND       0x0001
12666 -#define L2CAP_CONN_BAD_PSM    0x0002
12667 -#define L2CAP_CONN_SEC_BLOCK  0x0003
12668 -#define L2CAP_CONN_NO_MEM     0x0004
12669 +/* connect result */
12670 +#define L2CAP_CR_SUCCESS    0x0000
12671 +#define L2CAP_CR_PEND       0x0001
12672 +#define L2CAP_CR_BAD_PSM    0x0002
12673 +#define L2CAP_CR_SEC_BLOCK  0x0003
12674 +#define L2CAP_CR_NO_MEM     0x0004
12675 +
12676 +/* connect status */
12677 +#define L2CAP_CS_NO_INFO      0x0000
12678 +#define L2CAP_CS_AUTHEN_PEND  0x0001
12679 +#define L2CAP_CS_AUTHOR_PEND  0x0002
12680  
12681  typedef struct {
12682         __u16      dcid;
12683 @@ -147,6 +163,8 @@
12684  #define L2CAP_CONF_FLUSH_TO    0x02
12685  #define L2CAP_CONF_QOS         0x03
12686  
12687 +#define L2CAP_CONF_MAX_SIZE    22
12688 +
12689  typedef struct {
12690         __u16      dcid;
12691         __u16      scid;
12692 @@ -159,4 +177,82 @@
12693  } __attribute__ ((packed))     l2cap_disconn_rsp;
12694  #define L2CAP_DISCONN_RSP_SIZE 4
12695  
12696 +typedef struct {
12697 +       __u16       type;
12698 +       __u8        data[0];
12699 +} __attribute__ ((packed))     l2cap_info_req;
12700 +#define L2CAP_INFO_REQ_SIZE    2
12701 +
12702 +typedef struct {
12703 +       __u16       type;
12704 +       __u16       result;
12705 +       __u8        data[0];
12706 +} __attribute__ ((packed))     l2cap_info_rsp;
12707 +#define L2CAP_INFO_RSP_SIZE    4
12708 +
12709 +/* info type */
12710 +#define L2CAP_IT_CL_MTU     0x0001
12711 +#define L2CAP_IT_FEAT_MASK  0x0002
12712 +
12713 +/* info result */
12714 +#define L2CAP_IR_SUCCESS    0x0000
12715 +#define L2CAP_IR_NOTSUPP    0x0001
12716 +
12717 +/* ----- L2CAP connections ----- */
12718 +struct l2cap_chan_list {
12719 +       struct sock     *head;
12720 +       rwlock_t        lock;
12721 +       long            num;
12722 +};
12723 +
12724 +struct l2cap_conn {
12725 +       struct hci_conn *hcon;
12726 +
12727 +       bdaddr_t        *dst;
12728 +       bdaddr_t        *src;
12729 +       
12730 +       unsigned int    mtu;
12731 +
12732 +       spinlock_t      lock;
12733 +       
12734 +       struct sk_buff *rx_skb;
12735 +       __u32           rx_len;
12736 +       __u8            rx_ident;
12737 +       __u8            tx_ident;
12738 +
12739 +       struct l2cap_chan_list chan_list;
12740 +};
12741 +
12742 +/* ----- L2CAP channel and socket info ----- */
12743 +#define l2cap_pi(sk)   ((struct l2cap_pinfo *) &sk->tp_pinfo)
12744 +
12745 +struct l2cap_pinfo {
12746 +       __u16           psm;
12747 +       __u16           dcid;
12748 +       __u16           scid;
12749 +
12750 +       __u16           imtu;
12751 +       __u16           omtu;
12752 +       __u16           flush_to;
12753 +       
12754 +       __u32           link_mode;
12755 +
12756 +       __u8            conf_state;
12757 +       __u8            conf_retry;
12758 +       __u16           conf_mtu;
12759 +
12760 +       __u8            ident;
12761 +
12762 +       struct l2cap_conn       *conn;
12763 +       struct sock             *next_c;
12764 +       struct sock             *prev_c;
12765 +};
12766 +
12767 +#define L2CAP_CONF_REQ_SENT    0x01
12768 +#define L2CAP_CONF_INPUT_DONE  0x02
12769 +#define L2CAP_CONF_OUTPUT_DONE 0x04
12770 +#define L2CAP_CONF_MAX_RETRIES 2
12771 +
12772 +void l2cap_load(void);
12773 +
12774  #endif /* __L2CAP_H */
12775 diff -urN linux-2.4.18/include/net/bluetooth/rfcomm.h linux-2.4.18-mh15/include/net/bluetooth/rfcomm.h
12776 --- linux-2.4.18/include/net/bluetooth/rfcomm.h 1970-01-01 01:00:00.000000000 +0100
12777 +++ linux-2.4.18-mh15/include/net/bluetooth/rfcomm.h    2004-08-01 16:26:23.000000000 +0200
12778 @@ -0,0 +1,361 @@
12779 +/* 
12780 +   RFCOMM implementation for Linux Bluetooth stack (BlueZ).
12781 +   Copyright (C) 2002 Maxim Krasnyansky <maxk@qualcomm.com>
12782 +   Copyright (C) 2002 Marcel Holtmann <marcel@holtmann.org>
12783 +
12784 +   This program is free software; you can redistribute it and/or modify
12785 +   it under the terms of the GNU General Public License version 2 as
12786 +   published by the Free Software Foundation;
12787 +
12788 +   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
12789 +   OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
12790 +   FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS.
12791 +   IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) AND AUTHOR(S) BE LIABLE FOR ANY
12792 +   CLAIM, OR ANY SPECIAL INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES 
12793 +   WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 
12794 +   ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 
12795 +   OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
12796 +
12797 +   ALL LIABILITY, INCLUDING LIABILITY FOR INFRINGEMENT OF ANY PATENTS, 
12798 +   COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS, RELATING TO USE OF THIS 
12799 +   SOFTWARE IS DISCLAIMED.
12800 +*/
12801 +
12802 +/* 
12803 +   RPN support    -    Dirk Husemann <hud@zurich.ibm.com>
12804 +*/
12805 +
12806 +/*
12807 + * $Id: rfcomm.h,v 1.31 2002/10/18 20:12:11 maxk Exp $
12808 + */
12809 +
12810 +#ifndef __RFCOMM_H
12811 +#define __RFCOMM_H
12812 +
12813 +#define RFCOMM_PSM 3
12814 +
12815 +#define RFCOMM_CONN_TIMEOUT (HZ * 30)
12816 +#define RFCOMM_DISC_TIMEOUT (HZ * 20)
12817 +
12818 +#define RFCOMM_DEFAULT_MTU     127
12819 +#define RFCOMM_DEFAULT_CREDITS 7
12820 +
12821 +#define RFCOMM_MAX_L2CAP_MTU   1024
12822 +#define RFCOMM_MAX_CREDITS     40
12823 +
12824 +#define RFCOMM_SKB_HEAD_RESERVE        8
12825 +#define RFCOMM_SKB_TAIL_RESERVE        2
12826 +#define RFCOMM_SKB_RESERVE     (RFCOMM_SKB_HEAD_RESERVE + RFCOMM_SKB_TAIL_RESERVE)
12827 +
12828 +#define RFCOMM_SABM    0x2f
12829 +#define RFCOMM_DISC    0x43
12830 +#define RFCOMM_UA      0x63
12831 +#define RFCOMM_DM      0x0f
12832 +#define RFCOMM_UIH     0xef
12833 +
12834 +#define RFCOMM_TEST    0x08
12835 +#define RFCOMM_FCON    0x28
12836 +#define RFCOMM_FCOFF   0x18
12837 +#define RFCOMM_MSC     0x38
12838 +#define RFCOMM_RPN     0x24
12839 +#define RFCOMM_RLS     0x14
12840 +#define RFCOMM_PN      0x20
12841 +#define RFCOMM_NSC     0x04
12842 +
12843 +#define RFCOMM_V24_FC  0x02
12844 +#define RFCOMM_V24_RTC 0x04
12845 +#define RFCOMM_V24_RTR 0x08
12846 +#define RFCOMM_V24_IC  0x40
12847 +#define RFCOMM_V24_DV  0x80
12848 +
12849 +#define RFCOMM_RPN_BR_2400     0x0
12850 +#define RFCOMM_RPN_BR_4800     0x1
12851 +#define RFCOMM_RPN_BR_7200     0x2
12852 +#define RFCOMM_RPN_BR_9600     0x3
12853 +#define RFCOMM_RPN_BR_19200    0x4
12854 +#define RFCOMM_RPN_BR_38400    0x5
12855 +#define RFCOMM_RPN_BR_57600    0x6
12856 +#define RFCOMM_RPN_BR_115200   0x7
12857 +#define RFCOMM_RPN_BR_230400   0x8
12858 +
12859 +#define RFCOMM_RPN_DATA_5      0x0
12860 +#define RFCOMM_RPN_DATA_6      0x1
12861 +#define RFCOMM_RPN_DATA_7      0x2
12862 +#define RFCOMM_RPN_DATA_8      0x3
12863 +
12864 +#define RFCOMM_RPN_STOP_1      0
12865 +#define RFCOMM_RPN_STOP_15     1
12866 +
12867 +#define RFCOMM_RPN_PARITY_NONE 0x0
12868 +#define RFCOMM_RPN_PARITY_ODD  0x4
12869 +#define RFCOMM_RPN_PARITY_EVEN 0x5
12870 +#define RFCOMM_RPN_PARITY_MARK 0x6
12871 +#define RFCOMM_RPN_PARITY_SPACE        0x7
12872 +
12873 +#define RFCOMM_RPN_FLOW_NONE   0x00
12874 +
12875 +#define RFCOMM_RPN_XON_CHAR    0x11
12876 +#define RFCOMM_RPN_XOFF_CHAR   0x13
12877 +
12878 +#define RFCOMM_RPN_PM_BITRATE          0x0001
12879 +#define RFCOMM_RPN_PM_DATA             0x0002
12880 +#define RFCOMM_RPN_PM_STOP             0x0004
12881 +#define RFCOMM_RPN_PM_PARITY           0x0008
12882 +#define RFCOMM_RPN_PM_PARITY_TYPE      0x0010
12883 +#define RFCOMM_RPN_PM_XON              0x0020
12884 +#define RFCOMM_RPN_PM_XOFF             0x0040
12885 +#define RFCOMM_RPN_PM_FLOW             0x3F00
12886 +
12887 +#define RFCOMM_RPN_PM_ALL              0x3F7F
12888 +
12889 +struct rfcomm_hdr {
12890 +       u8 addr;
12891 +       u8 ctrl;
12892 +       u8 len;    // Actual size can be 2 bytes
12893 +} __attribute__ ((packed));
12894 +
12895 +struct rfcomm_cmd {
12896 +       u8 addr;
12897 +       u8 ctrl;
12898 +       u8 len;
12899 +       u8 fcs;
12900 +} __attribute__ ((packed));
12901 +
12902 +struct rfcomm_mcc {
12903 +       u8 type;
12904 +       u8 len;
12905 +} __attribute__ ((packed));
12906 +
12907 +struct rfcomm_pn {
12908 +       u8  dlci;
12909 +       u8  flow_ctrl;
12910 +       u8  priority;
12911 +       u8  ack_timer;
12912 +       u16 mtu;
12913 +       u8  max_retrans;
12914 +       u8  credits;
12915 +} __attribute__ ((packed));
12916 +
12917 +struct rfcomm_rpn {
12918 +       u8  dlci;
12919 +       u8  bit_rate;
12920 +       u8  line_settings;
12921 +       u8  flow_ctrl;
12922 +       u8  xon_char;
12923 +       u8  xoff_char;
12924 +       u16 param_mask;
12925 +} __attribute__ ((packed));
12926 +
12927 +struct rfcomm_rls {
12928 +       u8  dlci;
12929 +       u8  status;
12930 +} __attribute__ ((packed));
12931 +
12932 +struct rfcomm_msc {
12933 +       u8  dlci;
12934 +       u8  v24_sig;
12935 +} __attribute__ ((packed));
12936 +
12937 +/* ---- Core structures, flags etc ---- */
12938 +
12939 +struct rfcomm_session {
12940 +       struct list_head list;
12941 +       struct socket   *sock;
12942 +       unsigned long    state;
12943 +       unsigned long    flags;
12944 +       atomic_t         refcnt;
12945 +       int              initiator;
12946 +
12947 +       /* Default DLC parameters */
12948 +       int    cfc;
12949 +       uint   mtu;
12950 +
12951 +       struct list_head dlcs;
12952 +};
12953 +
12954 +struct rfcomm_dlc {
12955 +       struct list_head      list;
12956 +       struct rfcomm_session *session;
12957 +       struct sk_buff_head   tx_queue;
12958 +       struct timer_list     timer;
12959 +
12960 +       spinlock_t    lock;
12961 +       unsigned long state;
12962 +       unsigned long flags;
12963 +       atomic_t      refcnt;
12964 +       u8            dlci;
12965 +       u8            addr;
12966 +       u8            priority;
12967 +       u8            v24_sig;
12968 +       u8            mscex;
12969 +
12970 +       uint          mtu;
12971 +       uint          cfc;
12972 +       uint          rx_credits;
12973 +       uint          tx_credits;
12974 +
12975 +       void          *owner;
12976 +
12977 +       void (*data_ready)(struct rfcomm_dlc *d, struct sk_buff *skb);
12978 +       void (*state_change)(struct rfcomm_dlc *d, int err);
12979 +       void (*modem_status)(struct rfcomm_dlc *d, u8 v24_sig);
12980 +};
12981 +
12982 +/* DLC and session flags */
12983 +#define RFCOMM_RX_THROTTLED 0
12984 +#define RFCOMM_TX_THROTTLED 1
12985 +#define RFCOMM_MSC_PENDING  2
12986 +#define RFCOMM_TIMED_OUT    3
12987 +
12988 +/* Scheduling flags and events */
12989 +#define RFCOMM_SCHED_STATE  0
12990 +#define RFCOMM_SCHED_RX     1
12991 +#define RFCOMM_SCHED_TX     2
12992 +#define RFCOMM_SCHED_TIMEO  3
12993 +#define RFCOMM_SCHED_WAKEUP 31
12994 +
12995 +/* MSC exchange flags */
12996 +#define RFCOMM_MSCEX_TX     1
12997 +#define RFCOMM_MSCEX_RX     2
12998 +#define RFCOMM_MSCEX_OK     (RFCOMM_MSCEX_TX + RFCOMM_MSCEX_RX)
12999 +
13000 +/* CFC states */
13001 +#define RFCOMM_CFC_UNKNOWN  -1
13002 +#define RFCOMM_CFC_DISABLED 0
13003 +#define RFCOMM_CFC_ENABLED  RFCOMM_MAX_CREDITS
13004 +
13005 +extern struct task_struct *rfcomm_thread;
13006 +extern unsigned long rfcomm_event;
13007 +
13008 +static inline void rfcomm_schedule(uint event)
13009 +{
13010 +       if (!rfcomm_thread)
13011 +               return;
13012 +       set_bit(RFCOMM_SCHED_WAKEUP, &rfcomm_event);
13013 +       wake_up_process(rfcomm_thread);
13014 +}
13015 +
13016 +extern struct semaphore rfcomm_sem;
13017 +#define rfcomm_lock()  down(&rfcomm_sem);
13018 +#define rfcomm_unlock()        up(&rfcomm_sem);
13019 +
13020 +/* ---- RFCOMM DLCs (channels) ---- */
13021 +struct rfcomm_dlc *rfcomm_dlc_alloc(int prio);
13022 +void rfcomm_dlc_free(struct rfcomm_dlc *d);
13023 +int  rfcomm_dlc_open(struct rfcomm_dlc *d, bdaddr_t *src, bdaddr_t *dst, u8 channel);
13024 +int  rfcomm_dlc_close(struct rfcomm_dlc *d, int reason);
13025 +int  rfcomm_dlc_send(struct rfcomm_dlc *d, struct sk_buff *skb);
13026 +int  rfcomm_dlc_set_modem_status(struct rfcomm_dlc *d, u8 v24_sig);
13027 +int  rfcomm_dlc_get_modem_status(struct rfcomm_dlc *d, u8 *v24_sig);
13028 +
13029 +#define rfcomm_dlc_lock(d)     spin_lock(&d->lock)
13030 +#define rfcomm_dlc_unlock(d)   spin_unlock(&d->lock)
13031 +
13032 +static inline void rfcomm_dlc_hold(struct rfcomm_dlc *d)
13033 +{
13034 +       atomic_inc(&d->refcnt);
13035 +}
13036 +
13037 +static inline void rfcomm_dlc_put(struct rfcomm_dlc *d)
13038 +{
13039 +       if (atomic_dec_and_test(&d->refcnt))
13040 +               rfcomm_dlc_free(d);
13041 +}
13042 +
13043 +extern void FASTCALL(__rfcomm_dlc_throttle(struct rfcomm_dlc *d));
13044 +extern void FASTCALL(__rfcomm_dlc_unthrottle(struct rfcomm_dlc *d));
13045 +
13046 +static inline void rfcomm_dlc_throttle(struct rfcomm_dlc *d)
13047 +{
13048 +       if (!test_and_set_bit(RFCOMM_RX_THROTTLED, &d->flags))
13049 +               __rfcomm_dlc_throttle(d);
13050 +}
13051 +
13052 +static inline void rfcomm_dlc_unthrottle(struct rfcomm_dlc *d)
13053 +{
13054 +       if (test_and_clear_bit(RFCOMM_RX_THROTTLED, &d->flags))
13055 +               __rfcomm_dlc_unthrottle(d);
13056 +}
13057 +
13058 +/* ---- RFCOMM sessions ---- */
13059 +struct rfcomm_session *rfcomm_session_add(struct socket *sock, int state);
13060 +struct rfcomm_session *rfcomm_session_get(bdaddr_t *src, bdaddr_t *dst);
13061 +struct rfcomm_session *rfcomm_session_create(bdaddr_t *src, bdaddr_t *dst, int *err);
13062 +void   rfcomm_session_del(struct rfcomm_session *s);
13063 +void   rfcomm_session_close(struct rfcomm_session *s, int err);
13064 +void   rfcomm_session_getaddr(struct rfcomm_session *s, bdaddr_t *src, bdaddr_t *dst);
13065 +
13066 +static inline void rfcomm_session_hold(struct rfcomm_session *s)
13067 +{
13068 +       atomic_inc(&s->refcnt);
13069 +}
13070 +
13071 +static inline void rfcomm_session_put(struct rfcomm_session *s)
13072 +{
13073 +       if (atomic_dec_and_test(&s->refcnt))
13074 +               rfcomm_session_del(s);
13075 +}
13076 +
13077 +/* ---- RFCOMM chechsum ---- */
13078 +extern u8 rfcomm_crc_table[];
13079 +
13080 +/* ---- RFCOMM sockets ---- */
13081 +struct sockaddr_rc {
13082 +       sa_family_t rc_family;
13083 +       bdaddr_t    rc_bdaddr;
13084 +       u8          rc_channel;
13085 +};
13086 +
13087 +#define rfcomm_pi(sk)  ((struct rfcomm_pinfo *) &sk->tp_pinfo)
13088 +
13089 +struct rfcomm_pinfo {
13090 +       struct rfcomm_dlc *dlc;
13091 +       u8 channel;
13092 +};
13093 +
13094 +int  rfcomm_init_sockets(void);
13095 +void rfcomm_cleanup_sockets(void);
13096 +
13097 +int  rfcomm_connect_ind(struct rfcomm_session *s, u8 channel, struct rfcomm_dlc **d);
13098 +
13099 +/* ---- RFCOMM TTY ---- */
13100 +#define RFCOMM_MAX_DEV  256
13101 +
13102 +#define RFCOMMCREATEDEV                _IOW('R', 200, int)
13103 +#define RFCOMMRELEASEDEV       _IOW('R', 201, int)
13104 +#define RFCOMMGETDEVLIST       _IOR('R', 210, int)
13105 +#define RFCOMMGETDEVINFO       _IOR('R', 211, int)
13106 +#define RFCOMMSTEALDLC         _IOW('R', 220, int)
13107 +
13108 +#define RFCOMM_REUSE_DLC       0
13109 +#define RFCOMM_RELEASE_ONHUP   1
13110 +#define RFCOMM_HANGUP_NOW      2
13111 +#define RFCOMM_TTY_ATTACHED    3
13112 +
13113 +struct rfcomm_dev_req {
13114 +       s16      dev_id;
13115 +       u32      flags;
13116 +       bdaddr_t src;
13117 +       bdaddr_t dst;
13118 +       u8       channel;
13119 +};
13120 +
13121 +struct rfcomm_dev_info {
13122 +       s16      id;
13123 +       u32      flags;
13124 +       u16      state;
13125 +       bdaddr_t src;
13126 +       bdaddr_t dst;
13127 +       u8       channel;
13128 +};
13129 +
13130 +struct rfcomm_dev_list_req {
13131 +       u16      dev_num;
13132 +       struct   rfcomm_dev_info dev_info[0];
13133 +};
13134 +
13135 +int  rfcomm_dev_ioctl(struct sock *sk, unsigned int cmd, unsigned long arg);
13136 +int  rfcomm_init_ttys(void);
13137 +void rfcomm_cleanup_ttys(void);
13138 +
13139 +#endif /* __RFCOMM_H */
13140 diff -urN linux-2.4.18/include/net/bluetooth/sco.h linux-2.4.18-mh15/include/net/bluetooth/sco.h
13141 --- linux-2.4.18/include/net/bluetooth/sco.h    1970-01-01 01:00:00.000000000 +0100
13142 +++ linux-2.4.18-mh15/include/net/bluetooth/sco.h       2004-08-01 16:26:23.000000000 +0200
13143 @@ -0,0 +1,81 @@
13144 +/* 
13145 +   BlueZ - Bluetooth protocol stack for Linux
13146 +   Copyright (C) 2000-2001 Qualcomm Incorporated
13147 +
13148 +   Written 2000,2001 by Maxim Krasnyansky <maxk@qualcomm.com>
13149 +
13150 +   This program is free software; you can redistribute it and/or modify
13151 +   it under the terms of the GNU General Public License version 2 as
13152 +   published by the Free Software Foundation;
13153 +
13154 +   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
13155 +   OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13156 +   FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS.
13157 +   IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) AND AUTHOR(S) BE LIABLE FOR ANY
13158 +   CLAIM, OR ANY SPECIAL INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES 
13159 +   WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 
13160 +   ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 
13161 +   OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
13162 +
13163 +   ALL LIABILITY, INCLUDING LIABILITY FOR INFRINGEMENT OF ANY PATENTS, 
13164 +   COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS, RELATING TO USE OF THIS 
13165 +   SOFTWARE IS DISCLAIMED.
13166 +*/
13167 +
13168 +/*
13169 + *  $Id: sco.h,v 1.1.1.1 2002/03/08 21:03:15 maxk Exp $
13170 + */
13171 +
13172 +#ifndef __SCO_H
13173 +#define __SCO_H
13174 +
13175 +/* SCO defaults */
13176 +#define SCO_DEFAULT_MTU        500
13177 +#define SCO_DEFAULT_FLUSH_TO   0xFFFF
13178 +
13179 +#define SCO_CONN_TIMEOUT       (HZ * 40)
13180 +#define SCO_DISCONN_TIMEOUT    (HZ * 2)
13181 +#define SCO_CONN_IDLE_TIMEOUT  (HZ * 60)
13182 +
13183 +/* SCO socket address */
13184 +struct sockaddr_sco {
13185 +       sa_family_t     sco_family;
13186 +       bdaddr_t        sco_bdaddr;
13187 +};
13188 +
13189 +/* set/get sockopt defines */
13190 +#define SCO_OPTIONS  0x01
13191 +struct sco_options {
13192 +       __u16 mtu;
13193 +};
13194 +
13195 +#define SCO_CONNINFO  0x02
13196 +struct sco_conninfo {
13197 +       __u16 hci_handle;
13198 +};
13199 +
13200 +/* ---- SCO connections ---- */
13201 +struct sco_conn {
13202 +       struct hci_conn *hcon;
13203 +
13204 +       bdaddr_t        *dst;
13205 +       bdaddr_t        *src;
13206 +       
13207 +       spinlock_t      lock;
13208 +       struct sock     *sk;
13209 +
13210 +       unsigned int    mtu;
13211 +};
13212 +
13213 +#define sco_conn_lock(c)       spin_lock(&c->lock);
13214 +#define sco_conn_unlock(c)     spin_unlock(&c->lock);
13215 +
13216 +/* ----- SCO socket info ----- */
13217 +#define sco_pi(sk)   ((struct sco_pinfo *) &sk->tp_pinfo)
13218 +
13219 +struct sco_pinfo {
13220 +       __u32           flags;
13221 +       struct sco_conn *conn;
13222 +};
13223 +
13224 +#endif /* __SCO_H */
13225 diff -urN linux-2.4.18/include/pcmcia/ciscode.h linux-2.4.18-mh15/include/pcmcia/ciscode.h
13226 --- linux-2.4.18/include/pcmcia/ciscode.h       2001-12-21 18:42:04.000000000 +0100
13227 +++ linux-2.4.18-mh15/include/pcmcia/ciscode.h  2004-08-01 16:26:23.000000000 +0200
13228 @@ -1,5 +1,5 @@
13229  /*
13230 - * ciscode.h 1.48 2001/08/24 12:16:12
13231 + * ciscode.h 1.57 2002/11/03 20:38:14
13232   *
13233   * The contents of this file are subject to the Mozilla Public License
13234   * Version 1.1 (the "License"); you may not use this file except in
13235 @@ -60,6 +60,10 @@
13236  #define PRODID_INTEL_DUAL_RS232                0x0301
13237  #define PRODID_INTEL_2PLUS             0x8422
13238  
13239 +#define MANFID_KME                     0x0032
13240 +#define PRODID_KME_KXLC005_A           0x0704
13241 +#define PRODID_KME_KXLC005_B           0x2904
13242 +
13243  #define MANFID_LINKSYS                 0x0143
13244  #define PRODID_LINKSYS_PCMLM28         0xc0ab
13245  #define PRODID_LINKSYS_3400            0x3341
13246 @@ -94,6 +98,8 @@
13247  #define PRODID_OSITECH_JACK_336                0x0007
13248  #define PRODID_OSITECH_SEVEN           0x0008
13249  
13250 +#define MANFID_OXSEMI                  0x0279
13251 +
13252  #define MANFID_PIONEER                 0x000b
13253  
13254  #define MANFID_PSION                   0x016c
13255 @@ -103,6 +109,7 @@
13256  #define PRODID_QUATECH_SPP100          0x0003
13257  #define PRODID_QUATECH_DUAL_RS232      0x0012
13258  #define PRODID_QUATECH_DUAL_RS232_D1   0x0007
13259 +#define PRODID_QUATECH_DUAL_RS232_D2   0x0052
13260  #define PRODID_QUATECH_QUAD_RS232      0x001b
13261  #define PRODID_QUATECH_DUAL_RS422      0x000e
13262  #define PRODID_QUATECH_QUAD_RS422      0x0045
13263 @@ -120,9 +127,12 @@
13264  
13265  #define MANFID_TDK                     0x0105
13266  #define PRODID_TDK_CF010               0x0900
13267 +#define PRODID_TDK_GN3410              0x4815
13268  
13269  #define MANFID_TOSHIBA                 0x0098
13270  
13271 +#define MANFID_UNGERMANN               0x02c0
13272 +
13273  #define MANFID_XIRCOM                  0x0105
13274  
13275  #endif /* _LINUX_CISCODE_H */
13276 diff -urN linux-2.4.18/kernel/ksyms.c linux-2.4.18-mh15/kernel/ksyms.c
13277 --- linux-2.4.18/kernel/ksyms.c 2002-02-25 20:38:13.000000000 +0100
13278 +++ linux-2.4.18-mh15/kernel/ksyms.c    2004-08-01 16:26:23.000000000 +0200
13279 @@ -47,6 +47,7 @@
13280  #include <linux/in6.h>
13281  #include <linux/completion.h>
13282  #include <linux/seq_file.h>
13283 +#include <linux/firmware.h>
13284  #include <asm/checksum.h>
13285  
13286  #if defined(CONFIG_PROC_FS)
13287 @@ -538,6 +539,13 @@
13288  EXPORT_SYMBOL(strspn);
13289  EXPORT_SYMBOL(strsep);
13290  
13291 +#ifdef CONFIG_FW_LOADER
13292 +EXPORT_SYMBOL(release_firmware);
13293 +EXPORT_SYMBOL(request_firmware);
13294 +EXPORT_SYMBOL(request_firmware_nowait);
13295 +EXPORT_SYMBOL(register_firmware);
13296 +#endif
13297 +
13298  /* software interrupts */
13299  EXPORT_SYMBOL(tasklet_hi_vec);
13300  EXPORT_SYMBOL(tasklet_vec);
13301 diff -urN linux-2.4.18/lib/Config.in linux-2.4.18-mh15/lib/Config.in
13302 --- linux-2.4.18/lib/Config.in  1970-01-01 01:00:00.000000000 +0100
13303 +++ linux-2.4.18-mh15/lib/Config.in     2004-08-01 16:26:23.000000000 +0200
13304 @@ -0,0 +1,12 @@
13305 +#
13306 +# Library configuration
13307 +#
13308 +mainmenu_option next_comment
13309 +comment 'Library routines'
13310 +
13311 +if [ "$CONFIG_EXPERIMENTAL" = "y" -a \
13312 +     "$CONFIG_HOTPLUG" = "y" ]; then
13313 +   tristate 'Hotplug firmware loading support (EXPERIMENTAL)' CONFIG_FW_LOADER
13314 +fi
13315 +
13316 +endmenu
13317 diff -urN linux-2.4.18/lib/firmware_class.c linux-2.4.18-mh15/lib/firmware_class.c
13318 --- linux-2.4.18/lib/firmware_class.c   1970-01-01 01:00:00.000000000 +0100
13319 +++ linux-2.4.18-mh15/lib/firmware_class.c      2004-08-01 16:26:23.000000000 +0200
13320 @@ -0,0 +1,573 @@
13321 +/*
13322 + * firmware_class.c - Multi purpose firmware loading support
13323 + *
13324 + * Copyright (c) 2003 Manuel Estrada Sainz <ranty@debian.org>
13325 + *
13326 + * Please see Documentation/firmware_class/ for more information.
13327 + *
13328 + */
13329 +/*
13330 + * Based on kernel/kmod.c and drivers/usb/usb.c
13331 + */
13332 +/*
13333 +        kernel/kmod.c
13334 +        Kirk Petersen
13335 +
13336 +        Reorganized not to be a daemon by Adam Richter, with guidance
13337 +        from Greg Zornetzer.
13338 +
13339 +        Modified to avoid chroot and file sharing problems.
13340 +        Mikael Pettersson
13341 +
13342 +        Limit the concurrent number of kmod modprobes to catch loops from
13343 +        "modprobe needs a service that is in a module".
13344 +        Keith Owens <kaos@ocs.com.au> December 1999
13345 +
13346 +        Unblock all signals when we exec a usermode process.
13347 +        Shuu Yamaguchi <shuu@wondernetworkresources.com> December 2000
13348 +*/
13349 +/*
13350 + * drivers/usb/usb.c
13351 + *
13352 + * (C) Copyright Linus Torvalds 1999
13353 + * (C) Copyright Johannes Erdfelt 1999-2001
13354 + * (C) Copyright Andreas Gal 1999
13355 + * (C) Copyright Gregory P. Smith 1999
13356 + * (C) Copyright Deti Fliegl 1999 (new USB architecture)
13357 + * (C) Copyright Randy Dunlap 2000
13358 + * (C) Copyright David Brownell 2000 (kernel hotplug, usb_device_id)
13359 + * (C) Copyright Yggdrasil Computing, Inc. 2000
13360 + *     (usb_device_id matching changes by Adam J. Richter)
13361 + */
13362 +
13363 +#include <linux/config.h>
13364 +#include <linux/module.h>
13365 +#include <linux/string.h>
13366 +#include <linux/types.h>
13367 +#include <linux/init.h>
13368 +#include <linux/slab.h>
13369 +#include <linux/kmod.h>
13370 +#include <linux/proc_fs.h>
13371 +#include <linux/vmalloc.h>
13372 +#include <asm/hardirq.h>
13373 +
13374 +#include "linux/firmware.h"
13375 +
13376 +MODULE_AUTHOR("Manuel Estrada Sainz <ranty@debian.org>");
13377 +MODULE_DESCRIPTION("Multi purpose firmware loading support");
13378 +MODULE_LICENSE("GPL");
13379 +
13380 +#define err(format, arg...) \
13381 +     printk(KERN_ERR  "%s:%s: " format "\n",__FILE__, __FUNCTION__ , ## arg)
13382 +#define warn(format, arg...) \
13383 +     printk(KERN_WARNING "%s:%s: " format "\n",__FILE__, __FUNCTION__ , ## arg)
13384 +#define dbg(format, arg...) \
13385 +     printk(KERN_DEBUG "%s:%s: " format "\n",__FILE__, __FUNCTION__ , ## arg)
13386 +
13387 +static int loading_timeout = 10;       /* In seconds */
13388 +static struct proc_dir_entry *proc_dir_timeout;
13389 +static struct proc_dir_entry *proc_dir;
13390 +
13391 +#ifdef CONFIG_HOTPLUG
13392 +
13393 +static int
13394 +call_helper(char *verb, const char *name, const char *device)
13395 +{
13396 +       char *argv[3], **envp, *buf, *scratch;
13397 +       int i = 0;
13398 +
13399 +       int retval = 0;
13400 +
13401 +       if (!hotplug_path[0])
13402 +               return -ENOENT;
13403 +       if (in_interrupt()) {
13404 +               err("in_interrupt");
13405 +               return -EFAULT;
13406 +       }
13407 +       if (!current->fs->root) {
13408 +               warn("call_policy %s -- no FS yet", verb);
13409 +               return -EPERM;
13410 +       }
13411 +
13412 +       if (!(envp = (char **) kmalloc(20 * sizeof (char *), GFP_KERNEL))) {
13413 +               err("unable to allocate envp");
13414 +               return -ENOMEM;
13415 +       }
13416 +       if (!(buf = kmalloc(256, GFP_KERNEL))) {
13417 +               kfree(envp);
13418 +               err("unable to allocate buf");
13419 +               return -ENOMEM;
13420 +       }
13421 +
13422 +       /* only one standardized param to hotplug command: type */
13423 +       argv[0] = hotplug_path;
13424 +       argv[1] = "firmware";
13425 +       argv[2] = 0;
13426 +
13427 +       /* minimal command environment */
13428 +       envp[i++] = "HOME=/";
13429 +       envp[i++] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin";
13430 +
13431 +#ifdef  DEBUG
13432 +       /* hint that policy agent should enter no-stdout debug mode */
13433 +       envp[i++] = "DEBUG=kernel";
13434 +#endif
13435 +       scratch = buf;
13436 +
13437 +       if (device) {
13438 +               envp[i++] = scratch;
13439 +               scratch += snprintf(scratch, FIRMWARE_NAME_MAX+25,
13440 +                                   "DEVPATH=/driver/firmware/%s", device) + 1;
13441 +       }
13442 +
13443 +       envp[i++] = scratch;
13444 +       scratch += sprintf(scratch, "ACTION=%s", verb) + 1;
13445 +
13446 +       envp[i++] = scratch;
13447 +       scratch += snprintf(scratch, FIRMWARE_NAME_MAX,
13448 +                           "FIRMWARE=%s", name) + 1;
13449 +
13450 +       envp[i++] = 0;
13451 +
13452 +#ifdef  DEBUG
13453 +       dbg("firmware: %s %s %s", argv[0], argv[1], verb);
13454 +#endif
13455 +
13456 +       retval = call_usermodehelper(argv[0], argv, envp);
13457 +       if (retval) {
13458 +               printk("call_usermodehelper return %d\n", retval);
13459 +       }
13460 +
13461 +       kfree(buf);
13462 +       kfree(envp);
13463 +       return retval;
13464 +}
13465 +#else
13466 +
13467 +static inline int
13468 +call_helper(char *verb, const char *name, const char *device)
13469 +{
13470 +       return -ENOENT;
13471 +}
13472 +
13473 +#endif /* CONFIG_HOTPLUG */
13474 +
13475 +struct firmware_priv {
13476 +       struct completion completion;
13477 +       struct proc_dir_entry *proc_dir;
13478 +       struct proc_dir_entry *attr_data;
13479 +       struct proc_dir_entry *attr_loading;
13480 +       struct firmware *fw;
13481 +       int loading;
13482 +       int abort;
13483 +       int alloc_size;
13484 +       struct timer_list timeout;
13485 +};
13486 +
13487 +static int
13488 +firmware_timeout_show(char *buf, char **start, off_t off,
13489 +                     int count, int *eof, void *data)
13490 +{
13491 +       return sprintf(buf, "%d\n", loading_timeout);
13492 +}
13493 +
13494 +/**
13495 + * firmware_timeout_store:
13496 + * Description:
13497 + *     Sets the number of seconds to wait for the firmware.  Once
13498 + *     this expires an error will be return to the driver and no
13499 + *     firmware will be provided.
13500 + *
13501 + *     Note: zero means 'wait for ever'
13502 + *  
13503 + **/
13504 +static int
13505 +firmware_timeout_store(struct file *file, const char *buf,
13506 +                      unsigned long count, void *data)
13507 +{
13508 +       loading_timeout = simple_strtol(buf, NULL, 10);
13509 +       return count;
13510 +}
13511 +
13512 +static int
13513 +firmware_loading_show(char *buf, char **start, off_t off,
13514 +                     int count, int *eof, void *data)
13515 +{
13516 +       struct firmware_priv *fw_priv = data;
13517 +       return sprintf(buf, "%d\n", fw_priv->loading);
13518 +}
13519 +
13520 +/**
13521 + * firmware_loading_store: - loading control file
13522 + * Description:
13523 + *     The relevant values are: 
13524 + *
13525 + *      1: Start a load, discarding any previous partial load.
13526 + *      0: Conclude the load and handle the data to the driver code.
13527 + *     -1: Conclude the load with an error and discard any written data.
13528 + **/
13529 +static int
13530 +firmware_loading_store(struct file *file, const char *buf,
13531 +                      unsigned long count, void *data)
13532 +{
13533 +       struct firmware_priv *fw_priv = data;
13534 +       int prev_loading = fw_priv->loading;
13535 +
13536 +       fw_priv->loading = simple_strtol(buf, NULL, 10);
13537 +
13538 +       switch (fw_priv->loading) {
13539 +       case -1:
13540 +               fw_priv->abort = 1;
13541 +               wmb();
13542 +               complete(&fw_priv->completion);
13543 +               break;
13544 +       case 1:
13545 +               kfree(fw_priv->fw->data);
13546 +               fw_priv->fw->data = NULL;
13547 +               fw_priv->fw->size = 0;
13548 +               fw_priv->alloc_size = 0;
13549 +               break;
13550 +       case 0:
13551 +               if (prev_loading == 1)
13552 +                       complete(&fw_priv->completion);
13553 +               break;
13554 +       }
13555 +
13556 +       return count;
13557 +}
13558 +
13559 +static int
13560 +firmware_data_read(char *buffer, char **start, off_t offset,
13561 +                  int count, int *eof, void *data)
13562 +{
13563 +       struct firmware_priv *fw_priv = data;
13564 +       struct firmware *fw = fw_priv->fw;
13565 +
13566 +       if (offset > fw->size)
13567 +               return 0;
13568 +       if (offset + count > fw->size)
13569 +               count = fw->size - offset;
13570 +
13571 +       memcpy(buffer, fw->data + offset, count);
13572 +       *start = (void *) ((long) count);
13573 +       return count;
13574 +}
13575 +static int
13576 +fw_realloc_buffer(struct firmware_priv *fw_priv, int min_size)
13577 +{
13578 +       u8 *new_data;
13579 +       int new_size;
13580 +
13581 +       if (min_size <= fw_priv->alloc_size)
13582 +               return 0;
13583 +       if((min_size % PAGE_SIZE) == 0)
13584 +               new_size = min_size;
13585 +       else
13586 +               new_size = (min_size + PAGE_SIZE) & PAGE_MASK;
13587 +       new_data = vmalloc(new_size);
13588 +       if (!new_data) {
13589 +               printk(KERN_ERR "%s: unable to alloc buffer\n", __FUNCTION__);
13590 +               /* Make sure that we don't keep incomplete data */
13591 +               fw_priv->abort = 1;
13592 +               return -ENOMEM;
13593 +       }
13594 +       fw_priv->alloc_size = new_size;
13595 +       if (fw_priv->fw->data) {
13596 +               memcpy(new_data, fw_priv->fw->data, fw_priv->fw->size);
13597 +               vfree(fw_priv->fw->data);
13598 +       }
13599 +       fw_priv->fw->data = new_data;
13600 +       BUG_ON(min_size > fw_priv->alloc_size);
13601 +       return 0;
13602 +}
13603 +
13604 +/**
13605 + * firmware_data_write:
13606 + *
13607 + * Description:
13608 + *
13609 + *     Data written to the 'data' attribute will be later handled to
13610 + *     the driver as a firmware image.
13611 + **/
13612 +static int
13613 +firmware_data_write(struct file *file, const char *buffer,
13614 +                   unsigned long count, void *data)
13615 +{
13616 +       struct firmware_priv *fw_priv = data;
13617 +       struct firmware *fw = fw_priv->fw;
13618 +       int offset = file->f_pos;
13619 +       int retval;
13620 +
13621 +       retval = fw_realloc_buffer(fw_priv, offset + count);
13622 +       if (retval) {
13623 +               printk("%s: retval:%d\n", __FUNCTION__, retval);
13624 +               return retval;
13625 +       }
13626 +
13627 +       memcpy(fw->data + offset, buffer, count);
13628 +
13629 +       fw->size = max_t(size_t, offset + count, fw->size);
13630 +       file->f_pos += count;
13631 +       return count;
13632 +}
13633 +
13634 +static void
13635 +firmware_class_timeout(u_long data)
13636 +{
13637 +       struct firmware_priv *fw_priv = (struct firmware_priv *) data;
13638 +       fw_priv->abort = 1;
13639 +       wmb();
13640 +       complete(&fw_priv->completion);
13641 +}
13642 +static int
13643 +fw_setup_class_device(struct firmware_priv **fw_priv_p,
13644 +                     const char *fw_name, const char *device)
13645 +{
13646 +       int retval;
13647 +       struct firmware_priv *fw_priv = kmalloc(sizeof (struct firmware_priv),
13648 +                                               GFP_KERNEL);
13649 +       *fw_priv_p = fw_priv;
13650 +       if (!fw_priv) {
13651 +               retval = -ENOMEM;
13652 +               goto out;
13653 +       }
13654 +       memset(fw_priv, 0, sizeof (*fw_priv));
13655 +
13656 +       init_completion(&fw_priv->completion);
13657 +
13658 +       fw_priv->timeout.function = firmware_class_timeout;
13659 +       fw_priv->timeout.data = (u_long) fw_priv;
13660 +       init_timer(&fw_priv->timeout);
13661 +
13662 +       retval = -EAGAIN;
13663 +       fw_priv->proc_dir = create_proc_entry(device, 0644 | S_IFDIR, proc_dir);
13664 +       if (!fw_priv->proc_dir)
13665 +               goto err_free_fw_priv;
13666 +
13667 +       fw_priv->attr_data = create_proc_entry("data", 0644 | S_IFREG,
13668 +                                              fw_priv->proc_dir);
13669 +       if (!fw_priv->attr_data)
13670 +               goto err_remove_dir;
13671 +
13672 +       fw_priv->attr_data->read_proc = firmware_data_read;
13673 +       fw_priv->attr_data->write_proc = firmware_data_write;
13674 +       fw_priv->attr_data->data = fw_priv;
13675 +
13676 +       fw_priv->attr_loading = create_proc_entry("loading", 0644 | S_IFREG,
13677 +                                                 fw_priv->proc_dir);
13678 +       if (!fw_priv->attr_loading)
13679 +               goto err_remove_data;
13680 +
13681 +       fw_priv->attr_loading->read_proc = firmware_loading_show;
13682 +       fw_priv->attr_loading->write_proc = firmware_loading_store;
13683 +       fw_priv->attr_loading->data = fw_priv;
13684 +
13685 +       retval = 0;
13686 +       fw_priv->fw = kmalloc(sizeof (struct firmware), GFP_KERNEL);
13687 +       if (!fw_priv->fw) {
13688 +               printk(KERN_ERR "%s: kmalloc(struct firmware) failed\n",
13689 +                      __FUNCTION__);
13690 +               retval = -ENOMEM;
13691 +               goto err_remove_loading;
13692 +       }
13693 +       memset(fw_priv->fw, 0, sizeof (*fw_priv->fw));
13694 +
13695 +       goto out;
13696 +
13697 +err_remove_loading:
13698 +       remove_proc_entry("loading", fw_priv->proc_dir);
13699 +err_remove_data:
13700 +       remove_proc_entry("data", fw_priv->proc_dir);
13701 +err_remove_dir:
13702 +       remove_proc_entry(device, proc_dir);
13703 +err_free_fw_priv:
13704 +       kfree(fw_priv);
13705 +out:
13706 +       return retval;
13707 +}
13708 +static void
13709 +fw_remove_class_device(struct firmware_priv *fw_priv)
13710 +{
13711 +       remove_proc_entry("loading", fw_priv->proc_dir);
13712 +       remove_proc_entry("data", fw_priv->proc_dir);
13713 +       remove_proc_entry(fw_priv->proc_dir->name, proc_dir);
13714 +}
13715 +
13716 +/** 
13717 + * request_firmware: - request firmware to hotplug and wait for it
13718 + * Description:
13719 + *     @firmware will be used to return a firmware image by the name
13720 + *     of @name for device @device.
13721 + *
13722 + *     Should be called from user context where sleeping is allowed.
13723 + *
13724 + *     @name will be use as $FIRMWARE in the hotplug environment and
13725 + *     should be distinctive enough not to be confused with any other
13726 + *     firmware image for this or any other device.
13727 + **/
13728 +int
13729 +request_firmware(const struct firmware **firmware, const char *name,
13730 +                const char *device)
13731 +{
13732 +       struct firmware_priv *fw_priv;
13733 +       int retval;
13734 +
13735 +       if (!firmware) {
13736 +               retval = -EINVAL;
13737 +               goto out;
13738 +       }
13739 +       *firmware = NULL;
13740 +
13741 +       retval = fw_setup_class_device(&fw_priv, name, device);
13742 +       if (retval)
13743 +               goto out;
13744 +
13745 +       retval = call_helper("add", name, device);
13746 +       if (retval)
13747 +               goto out;
13748 +       if (loading_timeout) {
13749 +               fw_priv->timeout.expires = jiffies + loading_timeout * HZ;
13750 +               add_timer(&fw_priv->timeout);
13751 +       }
13752 +
13753 +       wait_for_completion(&fw_priv->completion);
13754 +
13755 +       del_timer(&fw_priv->timeout);
13756 +       fw_remove_class_device(fw_priv);
13757 +
13758 +       if (fw_priv->fw->size && !fw_priv->abort) {
13759 +               *firmware = fw_priv->fw;
13760 +       } else {
13761 +               retval = -ENOENT;
13762 +               vfree(fw_priv->fw->data);
13763 +               kfree(fw_priv->fw);
13764 +       }
13765 +out:
13766 +       kfree(fw_priv);
13767 +       return retval;
13768 +}
13769 +
13770 +void
13771 +release_firmware(const struct firmware *fw)
13772 +{
13773 +       if (fw) {
13774 +               vfree(fw->data);
13775 +               kfree(fw);
13776 +       }
13777 +}
13778 +
13779 +/**
13780 + * register_firmware: - provide a firmware image for later usage
13781 + * 
13782 + * Description:
13783 + *     Make sure that @data will be available by requesting firmware @name.
13784 + *
13785 + *     Note: This will not be possible until some kind of persistence
13786 + *     is available.
13787 + **/
13788 +void
13789 +register_firmware(const char *name, const u8 *data, size_t size)
13790 +{
13791 +       /* This is meaningless without firmware caching, so until we
13792 +        * decide if firmware caching is reasonable just leave it as a
13793 +        * noop */
13794 +}
13795 +
13796 +/* Async support */
13797 +struct firmware_work {
13798 +       struct tq_struct work;
13799 +       struct module *module;
13800 +       const char *name;
13801 +       const char *device;
13802 +       void *context;
13803 +       void (*cont)(const struct firmware *fw, void *context);
13804 +};
13805 +
13806 +static void
13807 +request_firmware_work_func(void *arg)
13808 +{
13809 +       struct firmware_work *fw_work = arg;
13810 +       const struct firmware *fw;
13811 +       if (!arg)
13812 +               return;
13813 +       request_firmware(&fw, fw_work->name, fw_work->device);
13814 +       fw_work->cont(fw, fw_work->context);
13815 +       release_firmware(fw);
13816 +       __MOD_DEC_USE_COUNT(fw_work->module);
13817 +       kfree(fw_work);
13818 +}
13819 +
13820 +/**
13821 + * request_firmware_nowait:
13822 + *
13823 + * Description:
13824 + *     Asynchronous variant of request_firmware() for contexts where
13825 + *     it is not possible to sleep.
13826 + *
13827 + *     @cont will be called asynchronously when the firmware request is over.
13828 + *
13829 + *     @context will be passed over to @cont.
13830 + *
13831 + *     @fw may be %NULL if firmware request fails.
13832 + *
13833 + **/
13834 +int
13835 +request_firmware_nowait(
13836 +       struct module *module,
13837 +       const char *name, const char *device, void *context,
13838 +       void (*cont)(const struct firmware *fw, void *context))
13839 +{
13840 +       struct firmware_work *fw_work = kmalloc(sizeof (struct firmware_work),
13841 +                                               GFP_ATOMIC);
13842 +       if (!fw_work)
13843 +               return -ENOMEM;
13844 +       if (!try_inc_mod_count(module)) {
13845 +               kfree(fw_work);
13846 +               return -EFAULT;
13847 +       }
13848 +
13849 +       *fw_work = (struct firmware_work) {
13850 +               .module = module,
13851 +               .name = name,
13852 +               .device = device,
13853 +               .context = context,
13854 +               .cont = cont,
13855 +       };
13856 +       INIT_TQUEUE(&fw_work->work, request_firmware_work_func, fw_work);
13857 +
13858 +       schedule_task(&fw_work->work);
13859 +       return 0;
13860 +}
13861 +
13862 +static int __init
13863 +firmware_class_init(void)
13864 +{
13865 +       proc_dir = create_proc_entry("driver/firmware", 0755 | S_IFDIR, NULL);
13866 +       if (!proc_dir)
13867 +               return -EAGAIN;
13868 +       proc_dir_timeout = create_proc_entry("timeout",
13869 +                                            0644 | S_IFREG, proc_dir);
13870 +       if (!proc_dir_timeout) {
13871 +               remove_proc_entry("driver/firmware", NULL);
13872 +               return -EAGAIN;
13873 +       }
13874 +       proc_dir_timeout->read_proc = firmware_timeout_show;
13875 +       proc_dir_timeout->write_proc = firmware_timeout_store;
13876 +       return 0;
13877 +}
13878 +static void __exit
13879 +firmware_class_exit(void)
13880 +{
13881 +       remove_proc_entry("timeout", proc_dir);
13882 +       remove_proc_entry("driver/firmware", NULL);
13883 +}
13884 +
13885 +module_init(firmware_class_init);
13886 +module_exit(firmware_class_exit);
13887 +
13888 +#ifndef CONFIG_FW_LOADER
13889 +EXPORT_SYMBOL(release_firmware);
13890 +EXPORT_SYMBOL(request_firmware);
13891 +EXPORT_SYMBOL(request_firmware_nowait);
13892 +EXPORT_SYMBOL(register_firmware);
13893 +#endif
13894 diff -urN linux-2.4.18/lib/Makefile linux-2.4.18-mh15/lib/Makefile
13895 --- linux-2.4.18/lib/Makefile   2001-09-18 00:31:15.000000000 +0200
13896 +++ linux-2.4.18-mh15/lib/Makefile      2004-08-01 16:26:23.000000000 +0200
13897 @@ -8,13 +8,17 @@
13898  
13899  L_TARGET := lib.a
13900  
13901 -export-objs := cmdline.o dec_and_lock.o rwsem-spinlock.o rwsem.o
13902 +export-objs := cmdline.o dec_and_lock.o rwsem-spinlock.o rwsem.o \
13903 +              firmware_class.o
13904  
13905  obj-y := errno.o ctype.o string.o vsprintf.o brlock.o cmdline.o bust_spinlocks.o rbtree.o
13906  
13907 +obj-$(CONFIG_FW_LOADER) += firmware_class.o
13908  obj-$(CONFIG_RWSEM_GENERIC_SPINLOCK) += rwsem-spinlock.o
13909  obj-$(CONFIG_RWSEM_XCHGADD_ALGORITHM) += rwsem.o
13910  
13911 +include $(TOPDIR)/drivers/bluetooth/Makefile.lib
13912 +
13913  ifneq ($(CONFIG_HAVE_DEC_LOCK),y) 
13914    obj-y += dec_and_lock.o
13915  endif
13916 diff -urN linux-2.4.18/MAINTAINERS linux-2.4.18-mh15/MAINTAINERS
13917 --- linux-2.4.18/MAINTAINERS    2002-02-25 20:37:52.000000000 +0100
13918 +++ linux-2.4.18-mh15/MAINTAINERS       2004-08-01 16:26:23.000000000 +0200
13919 @@ -252,10 +252,88 @@
13920  L:     linux-kernel@vger.kernel.org
13921  S:     Maintained
13922  
13923 -BLUETOOTH SUBSYSTEM (BlueZ)
13924 +BLUETOOTH SUBSYSTEM
13925 +P:     Marcel Holtmann
13926 +M:     marcel@holtmann.org
13927  P:     Maxim Krasnyansky
13928  M:     maxk@qualcomm.com
13929 +L:     bluez-devel@lists.sf.net
13930  W:     http://bluez.sf.net
13931 +W:     http://www.bluez.org
13932 +W:     http://www.holtmann.org/linux/bluetooth/
13933 +S:     Maintained
13934 +
13935 +BLUETOOTH RFCOMM LAYER
13936 +P:     Marcel Holtmann
13937 +M:     marcel@holtmann.org
13938 +P:     Maxim Krasnyansky
13939 +M:     maxk@qualcomm.com
13940 +S:     Maintained
13941 +
13942 +BLUETOOTH BNEP LAYER
13943 +P:     Marcel Holtmann
13944 +M:     marcel@holtmann.org
13945 +P:     Maxim Krasnyansky
13946 +M:     maxk@qualcomm.com
13947 +S:     Maintained
13948 +
13949 +BLUETOOTH CMTP LAYER
13950 +P:     Marcel Holtmann
13951 +M:     marcel@holtmann.org
13952 +S:     Maintained
13953 +
13954 +BLUETOOTH HIDP LAYER
13955 +P:     Marcel Holtmann
13956 +M:     marcel@holtmann.org
13957 +S:     Maintained
13958 +
13959 +BLUETOOTH HCI UART DRIVER
13960 +P:     Marcel Holtmann
13961 +M:     marcel@holtmann.org
13962 +P:     Maxim Krasnyansky
13963 +M:     maxk@qualcomm.com
13964 +S:     Maintained
13965 +
13966 +BLUETOOTH HCI USB DRIVER
13967 +P:     Marcel Holtmann
13968 +M:     marcel@holtmann.org
13969 +P:     Maxim Krasnyansky
13970 +M:     maxk@qualcomm.com
13971 +S:     Maintained
13972 +
13973 +BLUETOOTH HCI BCM203X DRIVER
13974 +P:     Marcel Holtmann
13975 +M:     marcel@holtmann.org
13976 +S:     Maintained
13977 +
13978 +BLUETOOTH HCI BFUSB DRIVER
13979 +P:     Marcel Holtmann
13980 +M:     marcel@holtmann.org
13981 +S:     Maintained
13982 +
13983 +BLUETOOTH HCI DTL1 DRIVER
13984 +P:     Marcel Holtmann
13985 +M:     marcel@holtmann.org
13986 +S:     Maintained
13987 +
13988 +BLUETOOTH HCI BLUECARD DRIVER
13989 +P:     Marcel Holtmann
13990 +M:     marcel@holtmann.org
13991 +S:     Maintained
13992 +
13993 +BLUETOOTH HCI BT3C DRIVER
13994 +P:     Marcel Holtmann
13995 +M:     marcel@holtmann.org
13996 +S:     Maintained
13997 +
13998 +BLUETOOTH HCI BTUART DRIVER
13999 +P:     Marcel Holtmann
14000 +M:     marcel@holtmann.org
14001 +S:     Maintained
14002 +
14003 +BLUETOOTH HCI VHCI DRIVER
14004 +P:     Maxim Krasnyansky
14005 +M:     maxk@qualcomm.com
14006  S:     Maintained
14007  
14008  BTTV VIDEO4LINUX DRIVER
14009 diff -urN linux-2.4.18/net/bluetooth/af_bluetooth.c linux-2.4.18-mh15/net/bluetooth/af_bluetooth.c
14010 --- linux-2.4.18/net/bluetooth/af_bluetooth.c   2001-09-07 18:28:38.000000000 +0200
14011 +++ linux-2.4.18-mh15/net/bluetooth/af_bluetooth.c      2004-08-01 16:26:23.000000000 +0200
14012 @@ -25,14 +25,15 @@
14013  /*
14014   * BlueZ Bluetooth address family and sockets.
14015   *
14016 - * $Id: af_bluetooth.c,v 1.4 2001/07/05 18:42:44 maxk Exp $
14017 + * $Id: af_bluetooth.c,v 1.8 2002/07/22 20:32:54 maxk Exp $
14018   */
14019 -#define VERSION "1.1"
14020 +#define VERSION "2.4"
14021  
14022  #include <linux/config.h>
14023  #include <linux/module.h>
14024  
14025  #include <linux/types.h>
14026 +#include <linux/list.h>
14027  #include <linux/errno.h>
14028  #include <linux/kernel.h>
14029  #include <linux/major.h>
14030 @@ -40,6 +41,7 @@
14031  #include <linux/slab.h>
14032  #include <linux/skbuff.h>
14033  #include <linux/init.h>
14034 +#include <linux/poll.h>
14035  #include <linux/proc_fs.h>
14036  #include <net/sock.h>
14037  
14038 @@ -48,70 +50,79 @@
14039  #endif
14040  
14041  #include <net/bluetooth/bluetooth.h>
14042 -#include <net/bluetooth/bluez.h>
14043 +
14044 +#ifndef AF_BLUETOOTH_DEBUG
14045 +#undef  BT_DBG
14046 +#define BT_DBG( A... )
14047 +#endif
14048  
14049  /* Bluetooth sockets */
14050 -static struct net_proto_family *bluez_sock[BLUEZ_MAX_PROTO];
14051 +#define BLUEZ_MAX_PROTO        7
14052 +static struct net_proto_family *bluez_proto[BLUEZ_MAX_PROTO];
14053  
14054  int bluez_sock_register(int proto, struct net_proto_family *ops)
14055  {
14056 -       if (proto > BLUEZ_MAX_PROTO)
14057 +       if (proto >= BLUEZ_MAX_PROTO)
14058                 return -EINVAL;
14059  
14060 -       if (bluez_sock[proto])
14061 +       if (bluez_proto[proto])
14062                 return -EEXIST;
14063  
14064 -       bluez_sock[proto] = ops;
14065 +       bluez_proto[proto] = ops;
14066         return 0;
14067  }
14068  
14069  int bluez_sock_unregister(int proto)
14070  {
14071 -       if (proto > BLUEZ_MAX_PROTO)
14072 +       if (proto >= BLUEZ_MAX_PROTO)
14073                 return -EINVAL;
14074  
14075 -       if (!bluez_sock[proto])
14076 +       if (!bluez_proto[proto])
14077                 return -ENOENT;
14078  
14079 -       bluez_sock[proto] = NULL;
14080 +       bluez_proto[proto] = NULL;
14081         return 0;
14082  }
14083  
14084  static int bluez_sock_create(struct socket *sock, int proto)
14085  {
14086 -       if (proto > BLUEZ_MAX_PROTO)
14087 +       if (proto >= BLUEZ_MAX_PROTO)
14088                 return -EINVAL;
14089  
14090  #if defined(CONFIG_KMOD)
14091 -       if (!bluez_sock[proto]) {
14092 +       if (!bluez_proto[proto]) {
14093                 char module_name[30];
14094                 sprintf(module_name, "bt-proto-%d", proto);
14095                 request_module(module_name);
14096         }
14097  #endif
14098  
14099 -       if (!bluez_sock[proto])
14100 +       if (!bluez_proto[proto])
14101                 return -ENOENT;
14102  
14103 -       return bluez_sock[proto]->create(sock, proto);
14104 +       return bluez_proto[proto]->create(sock, proto);
14105 +}
14106 +
14107 +void bluez_sock_init(struct socket *sock, struct sock *sk)
14108 +{ 
14109 +       sock_init_data(sock, sk);
14110 +       INIT_LIST_HEAD(&bluez_pi(sk)->accept_q);
14111  }
14112  
14113  void bluez_sock_link(struct bluez_sock_list *l, struct sock *sk)
14114  {
14115 -       write_lock(&l->lock);
14116 -
14117 +       write_lock_bh(&l->lock);
14118         sk->next = l->head;
14119         l->head = sk;
14120         sock_hold(sk);
14121 -
14122 -       write_unlock(&l->lock);
14123 +       write_unlock_bh(&l->lock);
14124  }
14125  
14126  void bluez_sock_unlink(struct bluez_sock_list *l, struct sock *sk)
14127  {
14128         struct sock **skp;
14129  
14130 -       write_lock(&l->lock);
14131 +       write_lock_bh(&l->lock);
14132         for (skp = &l->head; *skp; skp = &((*skp)->next)) {
14133                 if (*skp == sk) {
14134                         *skp = sk->next;
14135 @@ -119,7 +130,163 @@
14136                         break;
14137                 }
14138         }
14139 -       write_unlock(&l->lock);
14140 +       write_unlock_bh(&l->lock);
14141 +}
14142 +
14143 +void bluez_accept_enqueue(struct sock *parent, struct sock *sk)
14144 +{
14145 +       BT_DBG("parent %p, sk %p", parent, sk);
14146 +
14147 +       sock_hold(sk);
14148 +       list_add_tail(&bluez_pi(sk)->accept_q, &bluez_pi(parent)->accept_q);
14149 +       bluez_pi(sk)->parent = parent;
14150 +       parent->ack_backlog++;
14151 +}
14152 +
14153 +static void bluez_accept_unlink(struct sock *sk)
14154 +{
14155 +       BT_DBG("sk %p state %d", sk, sk->state);
14156 +
14157 +       list_del_init(&bluez_pi(sk)->accept_q);
14158 +       bluez_pi(sk)->parent->ack_backlog--;
14159 +       bluez_pi(sk)->parent = NULL;
14160 +       sock_put(sk);
14161 +}
14162 +
14163 +struct sock *bluez_accept_dequeue(struct sock *parent, struct socket *newsock)
14164 +{
14165 +       struct list_head *p, *n;
14166 +       struct bluez_pinfo *pi;
14167 +       struct sock *sk;
14168 +       
14169 +       BT_DBG("parent %p", parent);
14170 +
14171 +       list_for_each_safe(p, n, &bluez_pi(parent)->accept_q) {
14172 +               pi = list_entry(p, struct bluez_pinfo, accept_q);
14173 +               sk = bluez_sk(pi);
14174 +               
14175 +               lock_sock(sk);
14176 +               if (sk->state == BT_CLOSED) {
14177 +                       release_sock(sk);
14178 +                       bluez_accept_unlink(sk);
14179 +                       continue;
14180 +               }
14181 +               
14182 +               if (sk->state == BT_CONNECTED || !newsock) {
14183 +                       bluez_accept_unlink(sk);
14184 +                       if (newsock)
14185 +                               sock_graft(sk, newsock);
14186 +                       release_sock(sk);
14187 +                       return sk;
14188 +               }
14189 +               release_sock(sk);
14190 +       }
14191 +       return NULL;
14192 +}
14193 +
14194 +int bluez_sock_recvmsg(struct socket *sock, struct msghdr *msg, int len, int flags, struct scm_cookie *scm)
14195 +{
14196 +       int noblock = flags & MSG_DONTWAIT;
14197 +       struct sock *sk = sock->sk;
14198 +       struct sk_buff *skb;
14199 +       int copied, err;
14200 +
14201 +       BT_DBG("sock %p sk %p len %d", sock, sk, len);
14202 +
14203 +       if (flags & (MSG_OOB))
14204 +               return -EOPNOTSUPP;
14205 +
14206 +       if (!(skb = skb_recv_datagram(sk, flags, noblock, &err))) {
14207 +               if (sk->shutdown & RCV_SHUTDOWN)
14208 +                       return 0;
14209 +               return err;
14210 +       }
14211 +
14212 +       msg->msg_namelen = 0;
14213 +
14214 +       copied = skb->len;
14215 +       if (len < copied) {
14216 +               msg->msg_flags |= MSG_TRUNC;
14217 +               copied = len;
14218 +       }
14219 +
14220 +       skb->h.raw = skb->data;
14221 +       err = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied);
14222 +
14223 +       skb_free_datagram(sk, skb);
14224 +
14225 +       return err ? : copied;
14226 +}
14227 +
14228 +unsigned int bluez_sock_poll(struct file * file, struct socket *sock, poll_table *wait)
14229 +{
14230 +       struct sock *sk = sock->sk;
14231 +       unsigned int mask = 0;
14232 +
14233 +       BT_DBG("sock %p, sk %p", sock, sk);
14234 +
14235 +       poll_wait(file, sk->sleep, wait);
14236 +
14237 +       if (sk->err || !skb_queue_empty(&sk->error_queue))
14238 +               mask |= POLLERR;
14239 +
14240 +       if (sk->shutdown == SHUTDOWN_MASK)
14241 +               mask |= POLLHUP;
14242 +
14243 +       if (!skb_queue_empty(&sk->receive_queue) || 
14244 +                       !list_empty(&bluez_pi(sk)->accept_q) ||
14245 +                       (sk->shutdown & RCV_SHUTDOWN))
14246 +               mask |= POLLIN | POLLRDNORM;
14247 +
14248 +       if (sk->state == BT_CLOSED)
14249 +               mask |= POLLHUP;
14250 +
14251 +       if (sk->state == BT_CONNECT ||
14252 +                       sk->state == BT_CONNECT2 ||
14253 +                       sk->state == BT_CONFIG)
14254 +               return mask;
14255 +
14256 +       if (sock_writeable(sk))
14257 +               mask |= POLLOUT | POLLWRNORM | POLLWRBAND;
14258 +       else
14259 +               set_bit(SOCK_ASYNC_NOSPACE, &sk->socket->flags);
14260 +
14261 +       return mask;
14262 +}
14263 +
14264 +int bluez_sock_wait_state(struct sock *sk, int state, unsigned long timeo)
14265 +{
14266 +       DECLARE_WAITQUEUE(wait, current);
14267 +       int err = 0;
14268 +
14269 +       BT_DBG("sk %p", sk);
14270 +
14271 +       add_wait_queue(sk->sleep, &wait);
14272 +       while (sk->state != state) {
14273 +               set_current_state(TASK_INTERRUPTIBLE);
14274 +
14275 +               if (!timeo) {
14276 +                       err = -EAGAIN;
14277 +                       break;
14278 +               }
14279 +
14280 +               if (signal_pending(current)) {
14281 +                       err = sock_intr_errno(timeo);
14282 +                       break;
14283 +               }
14284 +
14285 +               release_sock(sk);
14286 +               timeo = schedule_timeout(timeo);
14287 +               lock_sock(sk);
14288 +
14289 +               if (sk->err) {
14290 +                       err = sock_error(sk);
14291 +                       break;
14292 +               }
14293 +       }
14294 +       set_current_state(TASK_RUNNING);
14295 +       remove_wait_queue(sk->sleep, &wait);
14296 +       return err;
14297  }
14298  
14299  struct net_proto_family bluez_sock_family_ops =
14300 @@ -129,9 +296,9 @@
14301  
14302  int bluez_init(void)
14303  {
14304 -       INF("BlueZ HCI Core ver %s Copyright (C) 2000,2001 Qualcomm Inc",
14305 +       BT_INFO("BlueZ Core ver %s Copyright (C) 2000,2001 Qualcomm Inc",
14306                  VERSION);
14307 -       INF("Written 2000,2001 by Maxim Krasnyansky <maxk@qualcomm.com>");
14308 +       BT_INFO("Written 2000,2001 by Maxim Krasnyansky <maxk@qualcomm.com>");
14309  
14310         proc_mkdir("bluetooth", NULL);
14311  
14312 @@ -164,5 +331,6 @@
14313  module_exit(bluez_cleanup);
14314  
14315  MODULE_AUTHOR("Maxim Krasnyansky <maxk@qualcomm.com>");
14316 -MODULE_DESCRIPTION("BlueZ HCI Core ver " VERSION);
14317 +MODULE_DESCRIPTION("BlueZ Core ver " VERSION);
14318 +MODULE_LICENSE("GPL");
14319  #endif
14320 diff -urN linux-2.4.18/net/bluetooth/bnep/bnep.h linux-2.4.18-mh15/net/bluetooth/bnep/bnep.h
14321 --- linux-2.4.18/net/bluetooth/bnep/bnep.h      1970-01-01 01:00:00.000000000 +0100
14322 +++ linux-2.4.18-mh15/net/bluetooth/bnep/bnep.h 2004-08-01 16:26:23.000000000 +0200
14323 @@ -0,0 +1,185 @@
14324 +/*
14325 +  BNEP protocol definition for Linux Bluetooth stack (BlueZ).
14326 +  Copyright (C) 2002 Maxim Krasnyansky <maxk@qualcomm.com>
14327 +       
14328 +  This program is free software; you can redistribute it and/or modify
14329 +  it under the terms of the GNU General Public License, version 2, as
14330 +  published by the Free Software Foundation.
14331 +
14332 +  This program is distributed in the hope that it will be useful,
14333 +  but WITHOUT ANY WARRANTY; without even the implied warranty of
14334 +  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14335 +  GNU General Public License for more details.
14336 +
14337 +  You should have received a copy of the GNU General Public License
14338 +  along with this program; if not, write to the Free Software
14339 +  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
14340 +*/
14341 +
14342 +/*
14343 + * $Id: bnep2.h,v 1.9 2002/07/14 07:09:19 maxk Exp $
14344 + */
14345 +
14346 +#ifndef _BNEP_H
14347 +#define _BNEP_H
14348 +
14349 +#include <linux/types.h>
14350 +#include <net/bluetooth/bluetooth.h>
14351 +
14352 +#include "crc32.h"
14353 +
14354 +// Limits
14355 +#define BNEP_MAX_PROTO_FILTERS     5
14356 +#define BNEP_MAX_MULTICAST_FILTERS 20
14357 +
14358 +// UUIDs
14359 +#define BNEP_BASE_UUID 0x0000000000001000800000805F9B34FB
14360 +#define BNEP_UUID16    0x02
14361 +#define BNEP_UUID32    0x04
14362 +#define BNEP_UUID128   0x16
14363 +
14364 +#define BNEP_SVC_PANU  0x1115
14365 +#define BNEP_SVC_NAP   0x1116
14366 +#define BNEP_SVC_GN    0x1117
14367 +
14368 +// Packet types
14369 +#define BNEP_GENERAL               0x00
14370 +#define BNEP_CONTROL               0x01
14371 +#define BNEP_COMPRESSED            0x02
14372 +#define BNEP_COMPRESSED_SRC_ONLY   0x03
14373 +#define BNEP_COMPRESSED_DST_ONLY   0x04
14374 +
14375 +// Control types
14376 +#define BNEP_CMD_NOT_UNDERSTOOD    0x00
14377 +#define BNEP_SETUP_CONN_REQ        0x01
14378 +#define BNEP_SETUP_CONN_RSP        0x02
14379 +#define BNEP_FILTER_NET_TYPE_SET   0x03
14380 +#define BNEP_FILTER_NET_TYPE_RSP   0x04
14381 +#define BNEP_FILTER_MULTI_ADDR_SET 0x05
14382 +#define BNEP_FILTER_MULTI_ADDR_RSP 0x06
14383 +
14384 +// Extension types
14385 +#define BNEP_EXT_CONTROL           0x00
14386 +
14387 +// Response messages 
14388 +#define BNEP_SUCCESS               0x00
14389 +
14390 +#define BNEP_CONN_INVALID_DST      0x01
14391 +#define BNEP_CONN_INVALID_SRC      0x02
14392 +#define BNEP_CONN_INVALID_SVC      0x03
14393 +#define BNEP_CONN_NOT_ALLOWED      0x04
14394 +
14395 +#define BNEP_FILTER_UNSUPPORTED_REQ    0x01
14396 +#define BNEP_FILTER_INVALID_RANGE      0x02
14397 +#define BNEP_FILTER_INVALID_MCADDR     0x02
14398 +#define BNEP_FILTER_LIMIT_REACHED      0x03
14399 +#define BNEP_FILTER_DENIED_SECURITY    0x04
14400 +
14401 +// L2CAP settings
14402 +#define BNEP_MTU         1691
14403 +#define BNEP_PSM        0x0f
14404 +#define BNEP_FLUSH_TO    0xffff
14405 +#define BNEP_CONNECT_TO  15
14406 +#define BNEP_FILTER_TO   15
14407 +
14408 +// Headers 
14409 +#define BNEP_TYPE_MASK  0x7f
14410 +#define BNEP_EXT_HEADER         0x80
14411 +
14412 +struct bnep_setup_conn_req {
14413 +       __u8  type;
14414 +       __u8  ctrl;
14415 +       __u8  uuid_size;
14416 +       __u8  service[0];
14417 +} __attribute__((packed));
14418 +
14419 +struct bnep_set_filter_req {
14420 +       __u8  type;
14421 +       __u8  ctrl;
14422 +       __u16 len;
14423 +       __u8  list[0];
14424 +} __attribute__((packed));
14425 +
14426 +struct bnep_control_rsp {
14427 +       __u8  type;
14428 +       __u8  ctrl;
14429 +       __u16 resp;
14430 +} __attribute__((packed));
14431 +
14432 +struct bnep_ext_hdr {
14433 +       __u8  type;
14434 +       __u8  len;
14435 +       __u8  data[0];
14436 +} __attribute__((packed));
14437 +
14438 +/* BNEP ioctl defines */
14439 +#define BNEPCONNADD    _IOW('B', 200, int)
14440 +#define BNEPCONNDEL    _IOW('B', 201, int)
14441 +#define BNEPGETCONNLIST        _IOR('B', 210, int)
14442 +#define BNEPGETCONNINFO        _IOR('B', 211, int)
14443 +
14444 +struct bnep_connadd_req {
14445 +       int   sock;       // Connected socket
14446 +       __u32 flags;
14447 +       __u16 role;
14448 +       char  device[16]; // Name of the Ethernet device
14449 +};
14450 +
14451 +struct bnep_conndel_req {
14452 +       __u32 flags;
14453 +       __u8  dst[ETH_ALEN];
14454 +};
14455 +
14456 +struct bnep_conninfo {
14457 +       __u32 flags;
14458 +       __u16 role;
14459 +       __u16 state;    
14460 +       __u8  dst[ETH_ALEN];
14461 +       char  device[16];
14462 +};
14463 +
14464 +struct bnep_connlist_req {
14465 +       __u32  cnum;
14466 +       struct bnep_conninfo *ci;
14467 +};
14468 +
14469 +struct bnep_proto_filter {
14470 +       __u16 start;
14471 +       __u16 end;
14472 +};
14473 +
14474 +int bnep_add_connection(struct bnep_connadd_req *req, struct socket *sock);
14475 +int bnep_del_connection(struct bnep_conndel_req *req);
14476 +int bnep_get_connlist(struct bnep_connlist_req *req);
14477 +int bnep_get_conninfo(struct bnep_conninfo *ci);
14478 +
14479 +// BNEP sessions
14480 +struct bnep_session {
14481 +       struct list_head list;
14482 +       
14483 +       unsigned int  role;
14484 +        unsigned long state;
14485 +        unsigned long flags;
14486 +       atomic_t      killed;
14487 +
14488 +       struct ethhdr eh;
14489 +       struct msghdr msg;
14490 +
14491 +       struct bnep_proto_filter proto_filter[BNEP_MAX_PROTO_FILTERS];
14492 +       u64    mc_filter;
14493 +       
14494 +       struct socket    *sock;
14495 +       struct net_device dev;
14496 +       struct net_device_stats stats;
14497 +};
14498 +
14499 +int bnep_net_init(struct net_device *dev);
14500 +int bnep_sock_init(void);
14501 +int bnep_sock_cleanup(void);
14502 +
14503 +static inline int bnep_mc_hash(__u8 *addr)
14504 +{
14505 +        return (bnep_crc32(~0, addr, ETH_ALEN) >> 26);
14506 +}
14507 +
14508 +#endif
14509 diff -urN linux-2.4.18/net/bluetooth/bnep/Config.in linux-2.4.18-mh15/net/bluetooth/bnep/Config.in
14510 --- linux-2.4.18/net/bluetooth/bnep/Config.in   1970-01-01 01:00:00.000000000 +0100
14511 +++ linux-2.4.18-mh15/net/bluetooth/bnep/Config.in      2004-08-01 16:26:23.000000000 +0200
14512 @@ -0,0 +1,11 @@
14513 +#
14514 +# Bluetooth BNEP layer configuration
14515 +#
14516 +
14517 +dep_tristate 'BNEP protocol support' CONFIG_BLUEZ_BNEP $CONFIG_BLUEZ_L2CAP
14518 +
14519 +if [ "$CONFIG_BLUEZ_BNEP" != "n" ]; then
14520 +   bool '  Multicast filter support' CONFIG_BLUEZ_BNEP_MC_FILTER
14521 +   bool '  Protocol filter support'  CONFIG_BLUEZ_BNEP_PROTO_FILTER
14522 +fi
14523 +
14524 diff -urN linux-2.4.18/net/bluetooth/bnep/core.c linux-2.4.18-mh15/net/bluetooth/bnep/core.c
14525 --- linux-2.4.18/net/bluetooth/bnep/core.c      1970-01-01 01:00:00.000000000 +0100
14526 +++ linux-2.4.18-mh15/net/bluetooth/bnep/core.c 2004-08-01 16:26:23.000000000 +0200
14527 @@ -0,0 +1,718 @@
14528 +/* 
14529 +   BNEP implementation for Linux Bluetooth stack (BlueZ).
14530 +   Copyright (C) 2001-2002 Inventel Systemes
14531 +   Written 2001-2002 by
14532 +       Clément Moreau <clement.moreau@inventel.fr>
14533 +       David Libault  <david.libault@inventel.fr>
14534 +
14535 +   Copyright (C) 2002 Maxim Krasnyanskiy <maxk@qualcomm.com>
14536 +
14537 +   This program is free software; you can redistribute it and/or modify
14538 +   it under the terms of the GNU General Public License version 2 as
14539 +   published by the Free Software Foundation;
14540 +
14541 +   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
14542 +   OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
14543 +   FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS.
14544 +   IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) AND AUTHOR(S) BE LIABLE FOR ANY
14545 +   CLAIM, OR ANY SPECIAL INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES 
14546 +   WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 
14547 +   ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 
14548 +   OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
14549 +
14550 +   ALL LIABILITY, INCLUDING LIABILITY FOR INFRINGEMENT OF ANY PATENTS, 
14551 +   COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS, RELATING TO USE OF THIS 
14552 +   SOFTWARE IS DISCLAIMED.
14553 +*/
14554 +
14555 +/*
14556 + * $Id: core.c,v 1.18 2002/07/14 07:09:19 maxk Exp $
14557 + */ 
14558 +
14559 +#define __KERNEL_SYSCALLS__
14560 +
14561 +#include <linux/config.h>
14562 +#include <linux/module.h>
14563 +
14564 +#include <linux/kernel.h>
14565 +#include <linux/sched.h>
14566 +#include <linux/signal.h>
14567 +#include <linux/init.h>
14568 +#include <linux/wait.h>
14569 +#include <linux/errno.h>
14570 +#include <linux/smp_lock.h>
14571 +#include <linux/net.h>
14572 +#include <net/sock.h>
14573 +
14574 +#include <linux/socket.h>
14575 +#include <linux/file.h>
14576 +
14577 +#include <linux/netdevice.h>
14578 +#include <linux/etherdevice.h>
14579 +#include <linux/skbuff.h>
14580 +
14581 +#include <asm/unaligned.h>
14582 +
14583 +#include <net/bluetooth/bluetooth.h>
14584 +#include <net/bluetooth/l2cap.h>
14585 +
14586 +#include "bnep.h"
14587 +
14588 +#ifndef CONFIG_BLUEZ_BNEP_DEBUG
14589 +#undef  BT_DBG
14590 +#define BT_DBG(D...)
14591 +#endif
14592 +
14593 +#define VERSION "1.2"
14594 +
14595 +static LIST_HEAD(bnep_session_list);
14596 +static DECLARE_RWSEM(bnep_session_sem);
14597 +
14598 +static struct bnep_session *__bnep_get_session(u8 *dst)
14599 +{
14600 +       struct bnep_session *s;
14601 +       struct list_head *p;
14602 +
14603 +       BT_DBG("");
14604 +
14605 +       list_for_each(p, &bnep_session_list) {
14606 +               s = list_entry(p, struct bnep_session, list);   
14607 +               if (!memcmp(dst, s->eh.h_source, ETH_ALEN))
14608 +                       return s;
14609 +       }
14610 +       return NULL;
14611 +}
14612 +
14613 +static void __bnep_link_session(struct bnep_session *s)
14614 +{
14615 +       MOD_INC_USE_COUNT;
14616 +       list_add(&s->list, &bnep_session_list); 
14617 +}
14618 +
14619 +static void __bnep_unlink_session(struct bnep_session *s)
14620 +{
14621 +       list_del(&s->list);
14622 +       MOD_DEC_USE_COUNT;
14623 +}
14624 +
14625 +static int bnep_send(struct bnep_session *s, void *data, size_t len)
14626 +{
14627 +       struct socket *sock = s->sock;
14628 +       struct iovec iv = { data, len };
14629 +       s->msg.msg_iov    = &iv;
14630 +       s->msg.msg_iovlen = 1;
14631 +       return sock->ops->sendmsg(sock, &s->msg, len, NULL);
14632 +}
14633 +
14634 +static int bnep_send_rsp(struct bnep_session *s, u8 ctrl, u16 resp)
14635 +{
14636 +       struct bnep_control_rsp rsp;
14637 +       rsp.type = BNEP_CONTROL;
14638 +       rsp.ctrl = ctrl;
14639 +       rsp.resp = htons(resp);
14640 +       return bnep_send(s, &rsp, sizeof(rsp));
14641 +}
14642 +
14643 +#ifdef CONFIG_BLUEZ_BNEP_PROTO_FILTER
14644 +static inline void bnep_set_default_proto_filter(struct bnep_session *s)
14645 +{
14646 +       /* (IPv4, ARP)  */
14647 +       s->proto_filter[0].start = htons(0x0800);
14648 +       s->proto_filter[0].end   = htons(0x0806);
14649 +       /* (RARP, AppleTalk) */
14650 +       s->proto_filter[1].start = htons(0x8035);
14651 +       s->proto_filter[1].end   = htons(0x80F3);
14652 +       /* (IPX, IPv6) */
14653 +       s->proto_filter[2].start = htons(0x8137);
14654 +       s->proto_filter[2].end   = htons(0x86DD);
14655 +}
14656 +#endif
14657 +
14658 +static int bnep_ctrl_set_netfilter(struct bnep_session *s, u16 *data, int len)
14659 +{
14660 +       int n;
14661 +
14662 +       if (len < 2)
14663 +               return -EILSEQ;
14664 +
14665 +       n = ntohs(get_unaligned(data));
14666 +       data++; len -= 2;
14667 +
14668 +       if (len < n)
14669 +               return -EILSEQ;
14670 +
14671 +       BT_DBG("filter len %d", n);
14672 +
14673 +#ifdef CONFIG_BLUEZ_BNEP_PROTO_FILTER
14674 +       n /= 4;
14675 +       if (n <= BNEP_MAX_PROTO_FILTERS) {
14676 +               struct bnep_proto_filter *f = s->proto_filter;
14677 +               int i;
14678 +
14679 +               for (i = 0; i < n; i++) {
14680 +                       f[i].start = get_unaligned(data++);
14681 +                       f[i].end   = get_unaligned(data++);
14682 +
14683 +                       BT_DBG("proto filter start %d end %d",
14684 +                               f[i].start, f[i].end);
14685 +               }
14686 +
14687 +               if (i < BNEP_MAX_PROTO_FILTERS)
14688 +                       memset(f + i, 0, sizeof(*f));
14689 +
14690 +               if (n == 0)
14691 +                       bnep_set_default_proto_filter(s);
14692 +
14693 +               bnep_send_rsp(s, BNEP_FILTER_NET_TYPE_RSP, BNEP_SUCCESS);
14694 +       } else {
14695 +               bnep_send_rsp(s, BNEP_FILTER_NET_TYPE_RSP, BNEP_FILTER_LIMIT_REACHED);
14696 +       }
14697 +#else
14698 +       bnep_send_rsp(s, BNEP_FILTER_NET_TYPE_RSP, BNEP_FILTER_UNSUPPORTED_REQ);
14699 +#endif
14700 +       return 0;
14701 +}
14702 +
14703 +static int bnep_ctrl_set_mcfilter(struct bnep_session *s, u8 *data, int len)
14704 +{
14705 +       int n;
14706 +
14707 +       if (len < 2)
14708 +               return -EILSEQ;
14709 +
14710 +       n = ntohs(get_unaligned((u16 *) data)); 
14711 +       data += 2; len -= 2;
14712 +
14713 +       if (len < n)
14714 +               return -EILSEQ;
14715 +
14716 +       BT_DBG("filter len %d", n);
14717 +
14718 +#ifdef CONFIG_BLUEZ_BNEP_MC_FILTER
14719 +       n /= (ETH_ALEN * 2);
14720 +
14721 +       if (n > 0) {
14722 +               s->mc_filter = 0;
14723 +
14724 +               /* Always send broadcast */
14725 +               set_bit(bnep_mc_hash(s->dev.broadcast), &s->mc_filter);
14726 +
14727 +               /* Add address ranges to the multicast hash */
14728 +               for (; n > 0; n--) {
14729 +                       u8 a1[6], *a2;
14730 +
14731 +                       memcpy(a1, data, ETH_ALEN); data += ETH_ALEN;
14732 +                       a2 = data; data += ETH_ALEN;
14733 +       
14734 +                       BT_DBG("mc filter %s -> %s",
14735 +                               batostr((void *) a1), batostr((void *) a2));
14736 +
14737 +                       #define INCA(a) { int i = 5; while (i >=0 && ++a[i--] == 0); }
14738 +
14739 +                       /* Iterate from a1 to a2 */
14740 +                       set_bit(bnep_mc_hash(a1), &s->mc_filter);
14741 +                       while (memcmp(a1, a2, 6) < 0 && s->mc_filter != ~0LL) {
14742 +                               INCA(a1);
14743 +                               set_bit(bnep_mc_hash(a1), &s->mc_filter);
14744 +                       }
14745 +               }
14746 +       }
14747 +
14748 +       BT_DBG("mc filter hash 0x%llx", s->mc_filter);
14749 +
14750 +       bnep_send_rsp(s, BNEP_FILTER_MULTI_ADDR_RSP, BNEP_SUCCESS);
14751 +#else
14752 +       bnep_send_rsp(s, BNEP_FILTER_MULTI_ADDR_RSP, BNEP_FILTER_UNSUPPORTED_REQ);
14753 +#endif
14754 +       return 0;
14755 +}
14756 +
14757 +static int bnep_rx_control(struct bnep_session *s, void *data, int len)
14758 +{
14759 +       u8  cmd = *(u8 *)data;
14760 +       int err = 0;
14761 +
14762 +       data++; len--;
14763 +
14764 +       switch (cmd) {
14765 +       case BNEP_CMD_NOT_UNDERSTOOD:
14766 +       case BNEP_SETUP_CONN_REQ:
14767 +       case BNEP_SETUP_CONN_RSP:
14768 +       case BNEP_FILTER_NET_TYPE_RSP:
14769 +       case BNEP_FILTER_MULTI_ADDR_RSP:
14770 +               /* Ignore these for now */
14771 +               break;
14772 +               
14773 +       case BNEP_FILTER_NET_TYPE_SET:
14774 +               err = bnep_ctrl_set_netfilter(s, data, len);
14775 +               break;
14776 +
14777 +       case BNEP_FILTER_MULTI_ADDR_SET:
14778 +               err = bnep_ctrl_set_mcfilter(s, data, len);
14779 +               break;
14780 +
14781 +       default: {
14782 +                       u8 pkt[3];
14783 +                       pkt[0] = BNEP_CONTROL;
14784 +                       pkt[1] = BNEP_CMD_NOT_UNDERSTOOD;
14785 +                       pkt[2] = cmd;
14786 +                       bnep_send(s, pkt, sizeof(pkt));
14787 +               }
14788 +               break;
14789 +       }
14790 +
14791 +       return err;
14792 +}
14793 +
14794 +static int bnep_rx_extension(struct bnep_session *s, struct sk_buff *skb)
14795 +{
14796 +       struct bnep_ext_hdr *h;
14797 +       int err = 0;
14798 +
14799 +       do {
14800 +               h = (void *) skb->data;
14801 +               if (!skb_pull(skb, sizeof(*h))) {
14802 +                       err = -EILSEQ;
14803 +                       break;
14804 +               }
14805 +
14806 +               BT_DBG("type 0x%x len %d", h->type, h->len);
14807 +               
14808 +               switch (h->type & BNEP_TYPE_MASK) {
14809 +               case BNEP_EXT_CONTROL:
14810 +                       bnep_rx_control(s, skb->data, skb->len);
14811 +                       break;
14812 +
14813 +               default:
14814 +                       /* Unknown extension, skip it. */
14815 +                       break;
14816 +               }
14817 +
14818 +               if (!skb_pull(skb, h->len)) {
14819 +                       err = -EILSEQ;
14820 +                       break;
14821 +               }
14822 +       } while (!err && (h->type & BNEP_EXT_HEADER));
14823 +
14824 +       return err;
14825 +}
14826 +
14827 +static u8 __bnep_rx_hlen[] = {
14828 +       ETH_HLEN,     /* BNEP_GENERAL */
14829 +       0,            /* BNEP_CONTROL */
14830 +       2,            /* BNEP_COMPRESSED */
14831 +       ETH_ALEN + 2, /* BNEP_COMPRESSED_SRC_ONLY */
14832 +       ETH_ALEN + 2  /* BNEP_COMPRESSED_DST_ONLY */
14833 +};
14834 +#define BNEP_RX_TYPES  (sizeof(__bnep_rx_hlen) - 1)
14835 +
14836 +static inline int bnep_rx_frame(struct bnep_session *s, struct sk_buff *skb)
14837 +{
14838 +       struct net_device *dev = &s->dev;
14839 +       struct sk_buff *nskb;
14840 +       u8 type;
14841 +
14842 +       dev->last_rx = jiffies;
14843 +       s->stats.rx_bytes += skb->len;
14844 +
14845 +       type = *(u8 *) skb->data; skb_pull(skb, 1);
14846 +
14847 +       if ((type & BNEP_TYPE_MASK) > BNEP_RX_TYPES)
14848 +               goto badframe;
14849 +
14850 +       if ((type & BNEP_TYPE_MASK) == BNEP_CONTROL) {
14851 +               bnep_rx_control(s, skb->data, skb->len);
14852 +               kfree_skb(skb);
14853 +               return 0;
14854 +       }
14855 +
14856 +       skb->mac.raw = skb->data;
14857 +
14858 +       /* Verify and pull out header */
14859 +       if (!skb_pull(skb, __bnep_rx_hlen[type & BNEP_TYPE_MASK]))
14860 +               goto badframe;
14861 +
14862 +       s->eh.h_proto = get_unaligned((u16 *) (skb->data - 2));
14863 +
14864 +       if (type & BNEP_EXT_HEADER) {
14865 +               if (bnep_rx_extension(s, skb) < 0)
14866 +                       goto badframe;
14867 +       }
14868 +
14869 +       /* Strip 802.1p header */
14870 +       if (ntohs(s->eh.h_proto) == 0x8100) {
14871 +               if (!skb_pull(skb, 4))
14872 +                       goto badframe;
14873 +               s->eh.h_proto = get_unaligned((u16 *) (skb->data - 2));
14874 +       }
14875 +
14876 +       /* We have to alloc new skb and copy data here :(. Because original skb
14877 +        * may not be modified and because of the alignment requirements. */
14878 +       nskb = alloc_skb(2 + ETH_HLEN + skb->len, GFP_KERNEL);
14879 +       if (!nskb) {
14880 +               s->stats.rx_dropped++;
14881 +               kfree_skb(skb);
14882 +               return -ENOMEM;
14883 +       }
14884 +       skb_reserve(nskb, 2);
14885 +
14886 +       /* Decompress header and construct ether frame */
14887 +       switch (type & BNEP_TYPE_MASK) {
14888 +       case BNEP_COMPRESSED:
14889 +               memcpy(__skb_put(nskb, ETH_HLEN), &s->eh, ETH_HLEN);
14890 +               break;
14891 +
14892 +       case BNEP_COMPRESSED_SRC_ONLY:
14893 +               memcpy(__skb_put(nskb, ETH_ALEN), s->eh.h_dest, ETH_ALEN);
14894 +               memcpy(__skb_put(nskb, ETH_ALEN), skb->mac.raw, ETH_ALEN);
14895 +               put_unaligned(s->eh.h_proto, (u16 *) __skb_put(nskb, 2));
14896 +               break;
14897 +
14898 +       case BNEP_COMPRESSED_DST_ONLY:
14899 +               memcpy(__skb_put(nskb, ETH_ALEN), skb->mac.raw, ETH_ALEN);
14900 +               memcpy(__skb_put(nskb, ETH_ALEN + 2), s->eh.h_source, ETH_ALEN + 2);
14901 +               break;
14902 +
14903 +       case BNEP_GENERAL:
14904 +               memcpy(__skb_put(nskb, ETH_ALEN * 2), skb->mac.raw, ETH_ALEN * 2);
14905 +               put_unaligned(s->eh.h_proto, (u16 *) __skb_put(nskb, 2));
14906 +               break;
14907 +       }
14908 +
14909 +       memcpy(__skb_put(nskb, skb->len), skb->data, skb->len);
14910 +       kfree_skb(skb);
14911 +
14912 +       s->stats.rx_packets++;
14913 +       nskb->dev       = dev;
14914 +       nskb->ip_summed = CHECKSUM_UNNECESSARY;
14915 +       nskb->protocol  = eth_type_trans(nskb, dev);
14916 +       netif_rx_ni(nskb);
14917 +       return 0;
14918 +
14919 +badframe:
14920 +       s->stats.rx_errors++;
14921 +       kfree_skb(skb);
14922 +       return 0;
14923 +}
14924 +
14925 +static u8 __bnep_tx_types[] = {
14926 +       BNEP_GENERAL,
14927 +       BNEP_COMPRESSED_SRC_ONLY,
14928 +       BNEP_COMPRESSED_DST_ONLY,
14929 +       BNEP_COMPRESSED
14930 +};
14931 +
14932 +static inline int bnep_tx_frame(struct bnep_session *s, struct sk_buff *skb)
14933 +{
14934 +       struct ethhdr *eh = (void *) skb->data;
14935 +       struct socket *sock = s->sock;
14936 +       struct iovec iv[3];
14937 +       int len = 0, il = 0;
14938 +       u8 type = 0;
14939 +
14940 +       BT_DBG("skb %p dev %p type %d", skb, skb->dev, skb->pkt_type);
14941 +
14942 +       if (!skb->dev) {
14943 +               /* Control frame sent by us */
14944 +               goto send;
14945 +       }
14946 +
14947 +       iv[il++] = (struct iovec) { &type, 1 };
14948 +       len++;
14949 +
14950 +       if (!memcmp(eh->h_dest, s->eh.h_source, ETH_ALEN))
14951 +               type |= 0x01;
14952 +
14953 +       if (!memcmp(eh->h_source, s->eh.h_dest, ETH_ALEN))
14954 +               type |= 0x02;
14955 +
14956 +       if (type)
14957 +               skb_pull(skb, ETH_ALEN * 2);
14958 +
14959 +       type = __bnep_tx_types[type];
14960 +       switch (type) {
14961 +       case BNEP_COMPRESSED_SRC_ONLY:
14962 +               iv[il++] = (struct iovec) { eh->h_source, ETH_ALEN };
14963 +               len += ETH_ALEN;
14964 +               break;
14965 +               
14966 +       case BNEP_COMPRESSED_DST_ONLY:
14967 +               iv[il++] = (struct iovec) { eh->h_dest, ETH_ALEN };
14968 +               len += ETH_ALEN;
14969 +               break;
14970 +       }
14971 +
14972 +send:
14973 +       iv[il++] = (struct iovec) { skb->data, skb->len };
14974 +       len += skb->len;
14975 +
14976 +       /* FIXME: linearize skb */
14977 +
14978 +       s->msg.msg_iov    = iv;
14979 +       s->msg.msg_iovlen = il;
14980 +       len = sock->ops->sendmsg(sock, &s->msg, len, NULL);
14981 +       kfree_skb(skb);
14982 +
14983 +       if (len > 0) {
14984 +               s->stats.tx_bytes += len;
14985 +               s->stats.tx_packets++;
14986 +               return 0;
14987 +       }
14988 +
14989 +       return len;
14990 +}
14991 +
14992 +static int bnep_session(void *arg)
14993 +{
14994 +       struct bnep_session *s = arg;
14995 +       struct net_device *dev = &s->dev;
14996 +       struct sock *sk = s->sock->sk;
14997 +       struct sk_buff *skb;
14998 +       wait_queue_t wait;
14999 +
15000 +       BT_DBG("");
15001 +
15002 +       daemonize(); reparent_to_init();
15003 +
15004 +       sprintf(current->comm, "kbnepd %s", dev->name);
15005 +
15006 +       sigfillset(&current->blocked);
15007 +       flush_signals(current);
15008 +
15009 +       current->nice = -15;
15010 +
15011 +       set_fs(KERNEL_DS);
15012 +
15013 +       init_waitqueue_entry(&wait, current);
15014 +       add_wait_queue(sk->sleep, &wait);
15015 +       while (!atomic_read(&s->killed)) {
15016 +               set_current_state(TASK_INTERRUPTIBLE);
15017 +
15018 +               // RX
15019 +               while ((skb = skb_dequeue(&sk->receive_queue))) {
15020 +                       skb_orphan(skb);
15021 +                       bnep_rx_frame(s, skb);
15022 +               }
15023 +
15024 +               if (sk->state != BT_CONNECTED)
15025 +                       break;
15026 +
15027 +               // TX
15028 +               while ((skb = skb_dequeue(&sk->write_queue)))
15029 +                       if (bnep_tx_frame(s, skb))
15030 +                               break;
15031 +               netif_wake_queue(dev);
15032 +
15033 +               schedule();
15034 +       }
15035 +       set_current_state(TASK_RUNNING);
15036 +       remove_wait_queue(sk->sleep, &wait);
15037 +
15038 +       /* Cleanup session */
15039 +       down_write(&bnep_session_sem);
15040 +
15041 +       /* Delete network device */
15042 +       unregister_netdev(dev);
15043 +
15044 +       /* Release the socket */
15045 +       fput(s->sock->file);
15046 +
15047 +       __bnep_unlink_session(s);
15048 +
15049 +       up_write(&bnep_session_sem);
15050 +       kfree(s);
15051 +       return 0;
15052 +}
15053 +
15054 +int bnep_add_connection(struct bnep_connadd_req *req, struct socket *sock)
15055 +{
15056 +       struct net_device *dev;
15057 +       struct bnep_session *s, *ss;
15058 +       u8 dst[ETH_ALEN], src[ETH_ALEN];
15059 +       int err;
15060 +
15061 +       BT_DBG("");
15062 +
15063 +       baswap((void *) dst, &bluez_pi(sock->sk)->dst);
15064 +       baswap((void *) src, &bluez_pi(sock->sk)->src);
15065 +
15066 +       s = kmalloc(sizeof(struct bnep_session), GFP_KERNEL);
15067 +       if (!s) 
15068 +               return -ENOMEM;
15069 +       memset(s, 0, sizeof(struct bnep_session));
15070 +
15071 +       down_write(&bnep_session_sem);
15072 +
15073 +       ss = __bnep_get_session(dst);
15074 +       if (ss && ss->state == BT_CONNECTED) {
15075 +               err = -EEXIST;
15076 +               goto failed;
15077 +       }
15078 +
15079 +       dev = &s->dev;
15080 +       
15081 +       if (*req->device)
15082 +               strcpy(dev->name, req->device);
15083 +       else
15084 +               strcpy(dev->name, "bnep%d");
15085 +
15086 +       memset(dev->broadcast, 0xff, ETH_ALEN);
15087 +
15088 +       /* This is rx header therefor addresses are swaped.
15089 +        * ie eh.h_dest is our local address. */
15090 +       memcpy(s->eh.h_dest,   &src, ETH_ALEN);
15091 +       memcpy(s->eh.h_source, &dst, ETH_ALEN);
15092 +
15093 +       s->sock  = sock;
15094 +       s->role  = req->role;
15095 +       s->state = BT_CONNECTED;
15096 +
15097 +       s->msg.msg_flags = MSG_NOSIGNAL;
15098 +
15099 +#ifdef CONFIG_BLUEZ_BNEP_MC_FILTER
15100 +       /* Set default mc filter */
15101 +       set_bit(bnep_mc_hash(dev->broadcast), &s->mc_filter);
15102 +#endif
15103 +
15104 +#ifdef CONFIG_BLUEZ_BNEP_PROTO_FILTER
15105 +       /* Set default protocol filter */
15106 +       bnep_set_default_proto_filter(s);
15107 +#endif
15108 +
15109 +       dev->init = bnep_net_init;
15110 +       dev->priv = s;
15111 +       err = register_netdev(dev);
15112 +       if (err) {
15113 +               goto failed;
15114 +       }
15115 +
15116 +       __bnep_link_session(s);
15117 +
15118 +       err = kernel_thread(bnep_session, s, CLONE_FS | CLONE_FILES | CLONE_SIGHAND);
15119 +       if (err < 0) {
15120 +               /* Session thread start failed, gotta cleanup. */
15121 +               unregister_netdev(dev);
15122 +               __bnep_unlink_session(s);
15123 +               goto failed;
15124 +       }
15125 +
15126 +       up_write(&bnep_session_sem);
15127 +       strcpy(req->device, dev->name);
15128 +       return 0;
15129 +
15130 +failed:
15131 +       up_write(&bnep_session_sem);
15132 +       kfree(s);
15133 +       return err;
15134 +}
15135 +
15136 +int bnep_del_connection(struct bnep_conndel_req *req)
15137 +{
15138 +       struct bnep_session *s;
15139 +       int  err = 0;
15140 +
15141 +       BT_DBG("");
15142 +
15143 +       down_read(&bnep_session_sem);
15144 +
15145 +       s = __bnep_get_session(req->dst);
15146 +       if (s) {
15147 +               /* Wakeup user-space which is polling for socket errors.
15148 +                * This is temporary hack untill we have shutdown in L2CAP */
15149 +               s->sock->sk->err = EUNATCH;
15150 +               
15151 +               /* Kill session thread */
15152 +               atomic_inc(&s->killed);
15153 +               wake_up_interruptible(s->sock->sk->sleep);
15154 +       } else
15155 +               err = -ENOENT;
15156 +
15157 +       up_read(&bnep_session_sem);
15158 +       return err;
15159 +}
15160 +
15161 +static void __bnep_copy_ci(struct bnep_conninfo *ci, struct bnep_session *s)
15162 +{
15163 +       memcpy(ci->dst, s->eh.h_source, ETH_ALEN);
15164 +       strcpy(ci->device, s->dev.name);
15165 +       ci->flags = s->flags;
15166 +       ci->state = s->state;
15167 +       ci->role  = s->role;
15168 +}
15169 +
15170 +int bnep_get_connlist(struct bnep_connlist_req *req)
15171 +{
15172 +       struct list_head *p;
15173 +       int err = 0, n = 0;
15174 +
15175 +       down_read(&bnep_session_sem);
15176 +
15177 +       list_for_each(p, &bnep_session_list) {
15178 +               struct bnep_session *s;
15179 +               struct bnep_conninfo ci;
15180 +
15181 +               s = list_entry(p, struct bnep_session, list);
15182 +
15183 +               __bnep_copy_ci(&ci, s);
15184 +               
15185 +               if (copy_to_user(req->ci, &ci, sizeof(ci))) {
15186 +                       err = -EFAULT;
15187 +                       break;
15188 +               }
15189 +
15190 +               if (++n >= req->cnum)
15191 +                       break;
15192 +
15193 +               req->ci++;
15194 +       }
15195 +       req->cnum = n;
15196 +
15197 +       up_read(&bnep_session_sem);
15198 +       return err;
15199 +}
15200 +
15201 +int bnep_get_conninfo(struct bnep_conninfo *ci)
15202 +{
15203 +       struct bnep_session *s;
15204 +       int err = 0;
15205 +
15206 +       down_read(&bnep_session_sem);
15207 +
15208 +       s = __bnep_get_session(ci->dst);
15209 +       if (s)
15210 +               __bnep_copy_ci(ci, s);
15211 +       else
15212 +               err = -ENOENT;
15213 +
15214 +       up_read(&bnep_session_sem);
15215 +       return err;
15216 +}
15217 +
15218 +static int __init bnep_init_module(void)
15219 +{
15220 +       l2cap_load();
15221 +
15222 +       bnep_crc32_init();
15223 +       bnep_sock_init();
15224 +
15225 +       BT_INFO("BlueZ BNEP ver %s", VERSION);
15226 +       BT_INFO("Copyright (C) 2001,2002 Inventel Systemes");
15227 +       BT_INFO("Written 2001,2002 by Clement Moreau <clement.moreau@inventel.fr>");
15228 +       BT_INFO("Written 2001,2002 by David Libault <david.libault@inventel.fr>");
15229 +       BT_INFO("Copyright (C) 2002 Maxim Krasnyanskiy <maxk@qualcomm.com>");
15230 +
15231 +       return 0;
15232 +}
15233 +
15234 +static void __exit bnep_cleanup_module(void)
15235 +{
15236 +       bnep_sock_cleanup();
15237 +       bnep_crc32_cleanup();
15238 +}
15239 +
15240 +module_init(bnep_init_module);
15241 +module_exit(bnep_cleanup_module);
15242 +
15243 +MODULE_DESCRIPTION("BlueZ BNEP ver " VERSION);
15244 +MODULE_AUTHOR("David Libault <david.libault@inventel.fr>, Maxim Krasnyanskiy <maxk@qualcomm.com>");
15245 +MODULE_LICENSE("GPL");
15246 diff -urN linux-2.4.18/net/bluetooth/bnep/crc32.c linux-2.4.18-mh15/net/bluetooth/bnep/crc32.c
15247 --- linux-2.4.18/net/bluetooth/bnep/crc32.c     1970-01-01 01:00:00.000000000 +0100
15248 +++ linux-2.4.18-mh15/net/bluetooth/bnep/crc32.c        2004-08-01 16:26:23.000000000 +0200
15249 @@ -0,0 +1,59 @@
15250 +/* 
15251 + * Based on linux-2.5/lib/crc32 by Matt Domsch <Matt_Domsch@dell.com>
15252 + *
15253 + * FIXME: Remove in 2.5  
15254 + */
15255 +
15256 +#include <linux/kernel.h>
15257 +#include <linux/module.h>
15258 +#include <linux/types.h>
15259 +#include <linux/slab.h>
15260 +#include <linux/init.h>
15261 +#include <asm/atomic.h>
15262 +
15263 +#include "crc32.h"
15264 +
15265 +#define CRCPOLY_BE 0x04c11db7
15266 +#define CRC_BE_BITS 8
15267 +
15268 +static u32 *bnep_crc32_table;
15269 +
15270 +/*
15271 + * This code is in the public domain; copyright abandoned.
15272 + * Liability for non-performance of this code is limited to the amount
15273 + * you paid for it.  Since it is distributed for free, your refund will
15274 + * be very very small.  If it breaks, you get to keep both pieces.
15275 + */
15276 +u32 bnep_crc32(u32 crc, unsigned char const *p, size_t len)
15277 +{
15278 +       while (len--)
15279 +               crc = (crc << 8) ^ bnep_crc32_table[(crc >> 24) ^ *p++];
15280 +       
15281 +       return crc;
15282 +}
15283 +
15284 +int __init bnep_crc32_init(void)
15285 +{
15286 +       unsigned i, j;
15287 +       u32 crc = 0x80000000;
15288 +
15289 +       bnep_crc32_table = kmalloc((1 << CRC_BE_BITS) * sizeof(u32), GFP_KERNEL);
15290 +       if (!bnep_crc32_table)
15291 +               return -ENOMEM;
15292 +
15293 +       bnep_crc32_table[0] = 0;
15294 +
15295 +       for (i = 1; i < 1 << CRC_BE_BITS; i <<= 1) {
15296 +               crc = (crc << 1) ^ ((crc & 0x80000000) ? CRCPOLY_BE : 0);
15297 +               for (j = 0; j < i; j++)
15298 +                       bnep_crc32_table[i + j] = crc ^ bnep_crc32_table[j];
15299 +       }
15300 +       return 0;
15301 +}
15302 +
15303 +void __exit bnep_crc32_cleanup(void)
15304 +{
15305 +       if (bnep_crc32_table)
15306 +               kfree(bnep_crc32_table);
15307 +       bnep_crc32_table = NULL;
15308 +}
15309 diff -urN linux-2.4.18/net/bluetooth/bnep/crc32.h linux-2.4.18-mh15/net/bluetooth/bnep/crc32.h
15310 --- linux-2.4.18/net/bluetooth/bnep/crc32.h     1970-01-01 01:00:00.000000000 +0100
15311 +++ linux-2.4.18-mh15/net/bluetooth/bnep/crc32.h        2004-08-01 16:26:23.000000000 +0200
15312 @@ -0,0 +1,10 @@
15313 +/*
15314 + * crc32.h
15315 + * See crc32.c for license and changes
15316 + *
15317 + * FIXME: Remove in 2.5
15318 + */
15319 +
15320 +int  bnep_crc32_init(void);
15321 +void bnep_crc32_cleanup(void);
15322 +u32  bnep_crc32(u32 crc, unsigned char const *p, size_t len);
15323 diff -urN linux-2.4.18/net/bluetooth/bnep/Makefile linux-2.4.18-mh15/net/bluetooth/bnep/Makefile
15324 --- linux-2.4.18/net/bluetooth/bnep/Makefile    1970-01-01 01:00:00.000000000 +0100
15325 +++ linux-2.4.18-mh15/net/bluetooth/bnep/Makefile       2004-08-01 16:26:23.000000000 +0200
15326 @@ -0,0 +1,10 @@
15327 +#
15328 +# Makefile for the Linux Bluetooth BNEP layer
15329 +#
15330 +
15331 +O_TARGET := bnep.o
15332 +
15333 +obj-y   := core.o sock.o netdev.o crc32.o
15334 +obj-m    += $(O_TARGET)
15335 +
15336 +include $(TOPDIR)/Rules.make
15337 diff -urN linux-2.4.18/net/bluetooth/bnep/netdev.c linux-2.4.18-mh15/net/bluetooth/bnep/netdev.c
15338 --- linux-2.4.18/net/bluetooth/bnep/netdev.c    1970-01-01 01:00:00.000000000 +0100
15339 +++ linux-2.4.18-mh15/net/bluetooth/bnep/netdev.c       2004-08-01 16:26:23.000000000 +0200
15340 @@ -0,0 +1,254 @@
15341 +/* 
15342 +   BNEP implementation for Linux Bluetooth stack (BlueZ).
15343 +   Copyright (C) 2001-2002 Inventel Systemes
15344 +   Written 2001-2002 by
15345 +       Clément Moreau <clement.moreau@inventel.fr>
15346 +       David Libault  <david.libault@inventel.fr>
15347 +
15348 +   Copyright (C) 2002 Maxim Krasnyanskiy <maxk@qualcomm.com>
15349 +
15350 +   This program is free software; you can redistribute it and/or modify
15351 +   it under the terms of the GNU General Public License version 2 as
15352 +   published by the Free Software Foundation;
15353 +
15354 +   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15355 +   OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15356 +   FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS.
15357 +   IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) AND AUTHOR(S) BE LIABLE FOR ANY
15358 +   CLAIM, OR ANY SPECIAL INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES 
15359 +   WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 
15360 +   ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 
15361 +   OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15362 +
15363 +   ALL LIABILITY, INCLUDING LIABILITY FOR INFRINGEMENT OF ANY PATENTS, 
15364 +   COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS, RELATING TO USE OF THIS 
15365 +   SOFTWARE IS DISCLAIMED.
15366 +*/
15367 +
15368 +/*
15369 + * $Id: netdev.c,v 1.7 2002/07/14 05:39:26 maxk Exp $
15370 + */ 
15371 +
15372 +#include <linux/config.h>
15373 +#include <linux/module.h>
15374 +
15375 +#include <linux/socket.h>
15376 +#include <linux/netdevice.h>
15377 +#include <linux/etherdevice.h>
15378 +#include <linux/skbuff.h>
15379 +#include <linux/wait.h>
15380 +
15381 +#include <asm/unaligned.h>
15382 +
15383 +#include <net/bluetooth/bluetooth.h>
15384 +#include <net/bluetooth/hci_core.h>
15385 +#include <net/bluetooth/l2cap.h>
15386 +
15387 +#include "bnep.h"
15388 +
15389 +#ifndef CONFIG_BLUEZ_BNEP_DEBUG
15390 +#undef  BT_DBG
15391 +#define BT_DBG( A... )
15392 +#endif
15393 +
15394 +#define BNEP_TX_QUEUE_LEN 20
15395 +
15396 +static int bnep_net_open(struct net_device *dev)
15397 +{
15398 +       netif_start_queue(dev);
15399 +       return 0;
15400 +}
15401 +
15402 +static int bnep_net_close(struct net_device *dev)
15403 +{
15404 +       netif_stop_queue(dev);
15405 +       return 0;
15406 +}
15407 +
15408 +static struct net_device_stats *bnep_net_get_stats(struct net_device *dev)
15409 +{
15410 +       struct bnep_session *s = dev->priv;
15411 +       return &s->stats;
15412 +}
15413 +
15414 +static void bnep_net_set_mc_list(struct net_device *dev)
15415 +{
15416 +#ifdef CONFIG_BLUEZ_BNEP_MC_FILTER
15417 +       struct bnep_session *s = dev->priv;
15418 +       struct sock *sk = s->sock->sk;
15419 +       struct bnep_set_filter_req *r;
15420 +       struct sk_buff *skb;
15421 +       int size;
15422 +
15423 +       BT_DBG("%s mc_count %d", dev->name, dev->mc_count);
15424 +
15425 +       size = sizeof(*r) + (BNEP_MAX_MULTICAST_FILTERS + 1) * ETH_ALEN * 2;
15426 +       skb  = alloc_skb(size, GFP_ATOMIC);
15427 +       if (!skb) {
15428 +               BT_ERR("%s Multicast list allocation failed", dev->name);
15429 +               return;
15430 +       }
15431 +
15432 +       r = (void *) skb->data;
15433 +       __skb_put(skb, sizeof(*r));
15434 +
15435 +       r->type = BNEP_CONTROL;
15436 +       r->ctrl = BNEP_FILTER_MULTI_ADDR_SET;
15437 +
15438 +        if (dev->flags & (IFF_PROMISC | IFF_ALLMULTI)) {
15439 +               u8 start[ETH_ALEN] = { 0x01 };
15440 +
15441 +               /* Request all addresses */
15442 +               memcpy(__skb_put(skb, ETH_ALEN), start, ETH_ALEN);
15443 +               memcpy(__skb_put(skb, ETH_ALEN), dev->broadcast, ETH_ALEN);
15444 +               r->len = htons(ETH_ALEN * 2);
15445 +       } else {
15446 +                struct dev_mc_list *dmi = dev->mc_list;
15447 +               int i, len = skb->len;
15448 +
15449 +               if (dev->flags & IFF_BROADCAST) {
15450 +                       memcpy(__skb_put(skb, ETH_ALEN), dev->broadcast, ETH_ALEN);
15451 +                       memcpy(__skb_put(skb, ETH_ALEN), dev->broadcast, ETH_ALEN);
15452 +               }       
15453 +               
15454 +               /* FIXME: We should group addresses here. */
15455 +
15456 +               for (i = 0; i < dev->mc_count && i < BNEP_MAX_MULTICAST_FILTERS; i++) {
15457 +                       memcpy(__skb_put(skb, ETH_ALEN), dmi->dmi_addr, ETH_ALEN);
15458 +                       memcpy(__skb_put(skb, ETH_ALEN), dmi->dmi_addr, ETH_ALEN);
15459 +                       dmi = dmi->next;
15460 +               }
15461 +               r->len = htons(skb->len - len);
15462 +       }
15463 +
15464 +       skb_queue_tail(&sk->write_queue, skb);
15465 +       wake_up_interruptible(sk->sleep);
15466 +#endif
15467 +}
15468 +
15469 +static int bnep_net_set_mac_addr(struct net_device *dev, void *arg)
15470 +{
15471 +       BT_DBG("%s", dev->name);
15472 +       return 0;
15473 +}
15474 +
15475 +static void bnep_net_timeout(struct net_device *dev)
15476 +{
15477 +       BT_DBG("net_timeout");
15478 +       netif_wake_queue(dev);
15479 +}
15480 +
15481 +static int bnep_net_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
15482 +{
15483 +       return -EINVAL;
15484 +}
15485 +
15486 +#ifdef CONFIG_BLUEZ_BNEP_MC_FILTER
15487 +static inline int bnep_net_mc_filter(struct sk_buff *skb, struct bnep_session *s)
15488 +{
15489 +       struct ethhdr *eh = (void *) skb->data;
15490 +
15491 +       if ((eh->h_dest[0] & 1) && !test_bit(bnep_mc_hash(eh->h_dest), &s->mc_filter)) {
15492 +               BT_DBG("BNEP: filtered skb %p, dst %.2x:%.2x:%.2x:%.2x:%.2x:%.2x", skb, 
15493 +                               eh->h_dest[0], eh->h_dest[1], eh->h_dest[2],
15494 +                               eh->h_dest[3], eh->h_dest[4], eh->h_dest[5]);
15495 +               return 1;
15496 +       }
15497 +       return 0;
15498 +}
15499 +#endif
15500 +
15501 +#ifdef CONFIG_BLUEZ_BNEP_PROTO_FILTER
15502 +/* Determine ether protocol. Based on eth_type_trans. */
15503 +static inline u16 bnep_net_eth_proto(struct sk_buff *skb)
15504 +{
15505 +       struct ethhdr *eh = (void *) skb->data;
15506 +       
15507 +       if (ntohs(eh->h_proto) >= 1536)
15508 +               return eh->h_proto;
15509 +               
15510 +       if (get_unaligned((u16 *) skb->data) == 0xFFFF)
15511 +               return htons(ETH_P_802_3);
15512 +               
15513 +       return htons(ETH_P_802_2);
15514 +}
15515 +
15516 +static inline int bnep_net_proto_filter(struct sk_buff *skb, struct bnep_session *s)
15517 +{
15518 +       u16 proto = bnep_net_eth_proto(skb);
15519 +       struct bnep_proto_filter *f = s->proto_filter;
15520 +       int i;
15521 +       
15522 +       for (i = 0; i < BNEP_MAX_PROTO_FILTERS && f[i].end; i++) {
15523 +               if (proto >= f[i].start && proto <= f[i].end)
15524 +                       return 0;
15525 +       }
15526 +
15527 +       BT_DBG("BNEP: filtered skb %p, proto 0x%.4x", skb, proto);
15528 +       return 1;
15529 +}
15530 +#endif
15531 +
15532 +static int bnep_net_xmit(struct sk_buff *skb, struct net_device *dev)
15533 +{
15534 +       struct bnep_session *s = dev->priv;
15535 +       struct sock *sk = s->sock->sk;
15536 +
15537 +       BT_DBG("skb %p, dev %p", skb, dev);
15538 +
15539 +#ifdef CONFIG_BLUEZ_BNEP_MC_FILTER
15540 +       if (bnep_net_mc_filter(skb, s)) {
15541 +               kfree_skb(skb);
15542 +               return 0;
15543 +       }
15544 +#endif
15545 +       
15546 +#ifdef CONFIG_BLUEZ_BNEP_PROTO_FILTER
15547 +       if (bnep_net_proto_filter(skb, s)) {
15548 +               kfree_skb(skb);
15549 +               return 0;
15550 +       }
15551 +#endif
15552 +       
15553 +       /*
15554 +        * We cannot send L2CAP packets from here as we are potentially in a bh.
15555 +        * So we have to queue them and wake up session thread which is sleeping
15556 +        * on the sk->sleep.
15557 +        */
15558 +       dev->trans_start = jiffies;
15559 +       skb_queue_tail(&sk->write_queue, skb);
15560 +       wake_up_interruptible(sk->sleep);
15561 +
15562 +       if (skb_queue_len(&sk->write_queue) >= BNEP_TX_QUEUE_LEN) {
15563 +               BT_DBG("tx queue is full");
15564 +
15565 +               /* Stop queuing.
15566 +                * Session thread will do netif_wake_queue() */
15567 +               netif_stop_queue(dev);
15568 +       }
15569 +
15570 +       return 0;
15571 +}
15572 +
15573 +int bnep_net_init(struct net_device *dev)
15574 +{
15575 +       struct bnep_session *s = dev->priv;
15576 +
15577 +       memcpy(dev->dev_addr, s->eh.h_dest, ETH_ALEN);
15578 +       dev->addr_len = ETH_ALEN;
15579 +
15580 +       ether_setup(dev);
15581 +
15582 +       dev->open            = bnep_net_open;
15583 +       dev->stop            = bnep_net_close;
15584 +       dev->hard_start_xmit = bnep_net_xmit;
15585 +       dev->get_stats       = bnep_net_get_stats;
15586 +       dev->do_ioctl        = bnep_net_ioctl;
15587 +       dev->set_mac_address = bnep_net_set_mac_addr;
15588 +       dev->set_multicast_list = bnep_net_set_mc_list;
15589 +
15590 +       dev->watchdog_timeo  = HZ * 2;
15591 +       dev->tx_timeout      = bnep_net_timeout;
15592 +
15593 +       return 0;
15594 +}
15595 diff -urN linux-2.4.18/net/bluetooth/bnep/sock.c linux-2.4.18-mh15/net/bluetooth/bnep/sock.c
15596 --- linux-2.4.18/net/bluetooth/bnep/sock.c      1970-01-01 01:00:00.000000000 +0100
15597 +++ linux-2.4.18-mh15/net/bluetooth/bnep/sock.c 2004-08-01 16:26:23.000000000 +0200
15598 @@ -0,0 +1,210 @@
15599 +/* 
15600 +   BNEP implementation for Linux Bluetooth stack (BlueZ).
15601 +   Copyright (C) 2001-2002 Inventel Systemes
15602 +   Written 2001-2002 by
15603 +       David Libault  <david.libault@inventel.fr>
15604 +
15605 +   Copyright (C) 2002 Maxim Krasnyanskiy <maxk@qualcomm.com>
15606 +
15607 +   This program is free software; you can redistribute it and/or modify
15608 +   it under the terms of the GNU General Public License version 2 as
15609 +   published by the Free Software Foundation;
15610 +
15611 +   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15612 +   OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15613 +   FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS.
15614 +   IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) AND AUTHOR(S) BE LIABLE FOR ANY
15615 +   CLAIM, OR ANY SPECIAL INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES 
15616 +   WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 
15617 +   ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 
15618 +   OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15619 +
15620 +   ALL LIABILITY, INCLUDING LIABILITY FOR INFRINGEMENT OF ANY PATENTS, 
15621 +   COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS, RELATING TO USE OF THIS 
15622 +   SOFTWARE IS DISCLAIMED.
15623 +*/
15624 +
15625 +/*
15626 + * $Id: sock.c,v 1.3 2002/07/10 22:59:52 maxk Exp $
15627 + */ 
15628 +
15629 +#include <linux/config.h>
15630 +#include <linux/module.h>
15631 +
15632 +#include <linux/types.h>
15633 +#include <linux/errno.h>
15634 +#include <linux/kernel.h>
15635 +#include <linux/major.h>
15636 +#include <linux/sched.h>
15637 +#include <linux/slab.h>
15638 +#include <linux/poll.h>
15639 +#include <linux/fcntl.h>
15640 +#include <linux/skbuff.h>
15641 +#include <linux/socket.h>
15642 +#include <linux/ioctl.h>
15643 +#include <linux/file.h>
15644 +#include <net/sock.h>
15645 +
15646 +#include <asm/system.h>
15647 +#include <asm/uaccess.h>
15648 +
15649 +#include "bnep.h"
15650 +
15651 +#ifndef CONFIG_BLUEZ_BNEP_DEBUG
15652 +#undef  BT_DBG
15653 +#define BT_DBG( A... )
15654 +#endif
15655 +
15656 +static int bnep_sock_release(struct socket *sock)
15657 +{
15658 +       struct sock *sk = sock->sk;
15659 +
15660 +       BT_DBG("sock %p sk %p", sock, sk);
15661 +
15662 +       if (!sk)
15663 +               return 0;
15664 +
15665 +       sock_orphan(sk);
15666 +       sock_put(sk);
15667 +
15668 +       MOD_DEC_USE_COUNT;
15669 +       return 0;
15670 +}
15671 +
15672 +static int bnep_sock_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
15673 +{
15674 +       struct bnep_connlist_req cl;
15675 +       struct bnep_connadd_req  ca;
15676 +       struct bnep_conndel_req  cd;
15677 +       struct bnep_conninfo ci;
15678 +       struct socket *nsock;
15679 +       int err;
15680 +
15681 +       BT_DBG("cmd %x arg %lx", cmd, arg);
15682 +
15683 +       switch (cmd) {
15684 +       case BNEPCONNADD:
15685 +               if (!capable(CAP_NET_ADMIN))
15686 +                       return -EACCES;
15687 +
15688 +               if (copy_from_user(&ca, (void *) arg, sizeof(ca)))
15689 +                       return -EFAULT;
15690 +       
15691 +               nsock = sockfd_lookup(ca.sock, &err);
15692 +               if (!nsock)
15693 +                       return err;
15694 +
15695 +               if (nsock->sk->state != BT_CONNECTED) {
15696 +                       fput(nsock->file);
15697 +                       return -EBADFD;
15698 +               }
15699 +
15700 +               err = bnep_add_connection(&ca, nsock);
15701 +               if (!err) {
15702 +                       if (copy_to_user((void *) arg, &ca, sizeof(ca)))
15703 +                               err = -EFAULT;
15704 +               } else
15705 +                       fput(nsock->file);
15706 +
15707 +               return err;
15708 +       
15709 +       case BNEPCONNDEL:
15710 +               if (!capable(CAP_NET_ADMIN))
15711 +                       return -EACCES;
15712 +
15713 +               if (copy_from_user(&cd, (void *) arg, sizeof(cd)))
15714 +                       return -EFAULT;
15715 +       
15716 +               return bnep_del_connection(&cd);
15717 +
15718 +       case BNEPGETCONNLIST:
15719 +               if (copy_from_user(&cl, (void *) arg, sizeof(cl)))
15720 +                       return -EFAULT;
15721 +
15722 +               if (cl.cnum <= 0)
15723 +                       return -EINVAL;
15724 +       
15725 +               err = bnep_get_connlist(&cl);
15726 +               if (!err && copy_to_user((void *) arg, &cl, sizeof(cl)))
15727 +                       return -EFAULT;
15728 +
15729 +               return err;
15730 +
15731 +       case BNEPGETCONNINFO:
15732 +               if (copy_from_user(&ci, (void *) arg, sizeof(ci)))
15733 +                       return -EFAULT;
15734 +
15735 +               err = bnep_get_conninfo(&ci);
15736 +               if (!err && copy_to_user((void *) arg, &ci, sizeof(ci)))
15737 +                       return -EFAULT;
15738 +
15739 +               return err;
15740 +
15741 +       default:
15742 +               return -EINVAL;
15743 +       }
15744 +
15745 +       return 0;
15746 +}
15747 +
15748 +static struct proto_ops bnep_sock_ops = {
15749 +       family:     PF_BLUETOOTH,
15750 +       release:    bnep_sock_release,
15751 +       ioctl:      bnep_sock_ioctl,
15752 +       bind:       sock_no_bind,
15753 +       getname:    sock_no_getname,
15754 +       sendmsg:    sock_no_sendmsg,
15755 +       recvmsg:    sock_no_recvmsg,
15756 +       poll:       sock_no_poll,
15757 +       listen:     sock_no_listen,
15758 +       shutdown:   sock_no_shutdown,
15759 +       setsockopt: sock_no_setsockopt,
15760 +       getsockopt: sock_no_getsockopt,
15761 +       connect:    sock_no_connect,
15762 +       socketpair: sock_no_socketpair,
15763 +       accept:     sock_no_accept,
15764 +       mmap:       sock_no_mmap
15765 +};
15766 +
15767 +static int bnep_sock_create(struct socket *sock, int protocol)
15768 +{
15769 +       struct sock *sk;
15770 +
15771 +       BT_DBG("sock %p", sock);
15772 +
15773 +       if (sock->type != SOCK_RAW)
15774 +               return -ESOCKTNOSUPPORT;
15775 +
15776 +       sock->ops = &bnep_sock_ops;
15777 +
15778 +       if (!(sk = sk_alloc(PF_BLUETOOTH, GFP_KERNEL, 1)))
15779 +               return -ENOMEM;
15780 +
15781 +       MOD_INC_USE_COUNT;
15782 +
15783 +       sock->state = SS_UNCONNECTED;
15784 +       sock_init_data(sock, sk);
15785 +
15786 +       sk->destruct = NULL;
15787 +       sk->protocol = protocol;
15788 +
15789 +       return 0;
15790 +}
15791 +
15792 +static struct net_proto_family bnep_sock_family_ops = {
15793 +       family: PF_BLUETOOTH,
15794 +       create: bnep_sock_create
15795 +};
15796 +
15797 +int bnep_sock_init(void)
15798 +{
15799 +       bluez_sock_register(BTPROTO_BNEP, &bnep_sock_family_ops);
15800 +       return 0;
15801 +}
15802 +
15803 +int bnep_sock_cleanup(void)
15804 +{
15805 +       if (bluez_sock_unregister(BTPROTO_BNEP))
15806 +               BT_ERR("Can't unregister BNEP socket");
15807 +       return 0;
15808 +}
15809 diff -urN linux-2.4.18/net/bluetooth/cmtp/capi.c linux-2.4.18-mh15/net/bluetooth/cmtp/capi.c
15810 --- linux-2.4.18/net/bluetooth/cmtp/capi.c      1970-01-01 01:00:00.000000000 +0100
15811 +++ linux-2.4.18-mh15/net/bluetooth/cmtp/capi.c 2004-08-01 16:26:23.000000000 +0200
15812 @@ -0,0 +1,707 @@
15813 +/* 
15814 +   CMTP implementation for Linux Bluetooth stack (BlueZ).
15815 +   Copyright (C) 2002-2003 Marcel Holtmann <marcel@holtmann.org>
15816 +
15817 +   This program is free software; you can redistribute it and/or modify
15818 +   it under the terms of the GNU General Public License version 2 as
15819 +   published by the Free Software Foundation;
15820 +
15821 +   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15822 +   OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15823 +   FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS.
15824 +   IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) AND AUTHOR(S) BE LIABLE FOR ANY
15825 +   CLAIM, OR ANY SPECIAL INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES 
15826 +   WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 
15827 +   ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 
15828 +   OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15829 +
15830 +   ALL LIABILITY, INCLUDING LIABILITY FOR INFRINGEMENT OF ANY PATENTS, 
15831 +   COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS, RELATING TO USE OF THIS 
15832 +   SOFTWARE IS DISCLAIMED.
15833 +*/
15834 +
15835 +#include <linux/config.h>
15836 +#include <linux/module.h>
15837 +
15838 +#include <linux/types.h>
15839 +#include <linux/errno.h>
15840 +#include <linux/kernel.h>
15841 +#include <linux/major.h>
15842 +#include <linux/sched.h>
15843 +#include <linux/slab.h>
15844 +#include <linux/poll.h>
15845 +#include <linux/fcntl.h>
15846 +#include <linux/skbuff.h>
15847 +#include <linux/socket.h>
15848 +#include <linux/ioctl.h>
15849 +#include <linux/file.h>
15850 +#include <net/sock.h>
15851 +
15852 +#include <linux/capi.h>
15853 +
15854 +#include "../drivers/isdn/avmb1/capilli.h"
15855 +#include "../drivers/isdn/avmb1/capicmd.h"
15856 +#include "../drivers/isdn/avmb1/capiutil.h"
15857 +
15858 +#include "cmtp.h"
15859 +
15860 +#ifndef CONFIG_BLUEZ_CMTP_DEBUG
15861 +#undef  BT_DBG
15862 +#define BT_DBG(D...)
15863 +#endif
15864 +
15865 +#define REVISION "1.0"
15866 +
15867 +#define CAPI_INTEROPERABILITY          0x20
15868 +
15869 +#define CAPI_INTEROPERABILITY_REQ      CAPICMD(CAPI_INTEROPERABILITY, CAPI_REQ)
15870 +#define CAPI_INTEROPERABILITY_CONF     CAPICMD(CAPI_INTEROPERABILITY, CAPI_CONF)
15871 +#define CAPI_INTEROPERABILITY_IND      CAPICMD(CAPI_INTEROPERABILITY, CAPI_IND)
15872 +#define CAPI_INTEROPERABILITY_RESP     CAPICMD(CAPI_INTEROPERABILITY, CAPI_RESP)
15873 +
15874 +#define CAPI_INTEROPERABILITY_REQ_LEN  (CAPI_MSG_BASELEN + 2)
15875 +#define CAPI_INTEROPERABILITY_CONF_LEN (CAPI_MSG_BASELEN + 4)
15876 +#define CAPI_INTEROPERABILITY_IND_LEN  (CAPI_MSG_BASELEN + 2)
15877 +#define CAPI_INTEROPERABILITY_RESP_LEN (CAPI_MSG_BASELEN + 2)
15878 +
15879 +#define CAPI_FUNCTION_REGISTER         0
15880 +#define CAPI_FUNCTION_RELEASE          1
15881 +#define CAPI_FUNCTION_GET_PROFILE      2
15882 +#define CAPI_FUNCTION_GET_MANUFACTURER 3
15883 +#define CAPI_FUNCTION_GET_VERSION      4
15884 +#define CAPI_FUNCTION_GET_SERIAL_NUMBER        5
15885 +#define CAPI_FUNCTION_MANUFACTURER     6
15886 +#define CAPI_FUNCTION_LOOPBACK         7
15887 +
15888 +static struct capi_driver_interface *di;
15889 +
15890 +
15891 +#define CMTP_MSGNUM    1
15892 +#define CMTP_APPLID    2
15893 +#define CMTP_MAPPING   3
15894 +
15895 +static struct cmtp_application *cmtp_application_add(struct cmtp_session *session, __u16 appl)
15896 +{
15897 +       struct cmtp_application *app = kmalloc(sizeof(*app), GFP_KERNEL);
15898 +
15899 +       BT_DBG("session %p application %p appl %d", session, app, appl);
15900 +
15901 +       if (!app)
15902 +               return NULL;
15903 +
15904 +       memset(app, 0, sizeof(*app));
15905 +
15906 +       app->state = BT_OPEN;
15907 +       app->appl = appl;
15908 +
15909 +       list_add_tail(&app->list, &session->applications);
15910 +
15911 +       return app;
15912 +}
15913 +
15914 +static void cmtp_application_del(struct cmtp_session *session, struct cmtp_application *app)
15915 +{
15916 +       BT_DBG("session %p application %p", session, app);
15917 +
15918 +       if (app) {
15919 +               list_del(&app->list);
15920 +               kfree(app);
15921 +       }
15922 +}
15923 +
15924 +static struct cmtp_application *cmtp_application_get(struct cmtp_session *session, int pattern, __u16 value)
15925 +{
15926 +       struct cmtp_application *app;
15927 +       struct list_head *p, *n;
15928 +
15929 +       list_for_each_safe(p, n, &session->applications) {
15930 +               app = list_entry(p, struct cmtp_application, list);
15931 +               switch (pattern) {
15932 +               case CMTP_MSGNUM:
15933 +                       if (app->msgnum == value)
15934 +                               return app;
15935 +                       break;
15936 +               case CMTP_APPLID:
15937 +                       if (app->appl == value)
15938 +                               return app;
15939 +                       break;
15940 +               case CMTP_MAPPING:
15941 +                       if (app->mapping == value)
15942 +                               return app;
15943 +                       break;
15944 +               }
15945 +       }
15946 +
15947 +       return NULL;
15948 +}
15949 +
15950 +static int cmtp_msgnum_get(struct cmtp_session *session)
15951 +{
15952 +       session->msgnum++;
15953 +
15954 +       if ((session->msgnum & 0xff) > 200)
15955 +               session->msgnum = CMTP_INITIAL_MSGNUM + 1;
15956 +
15957 +       return session->msgnum;
15958 +}
15959 +
15960 +
15961 +static void cmtp_send_interopmsg(struct cmtp_session *session,
15962 +                                       __u8 subcmd, __u16 appl, __u16 msgnum,
15963 +                                       __u16 function, unsigned char *buf, int len)
15964 +{
15965 +       struct sk_buff *skb;
15966 +       unsigned char *s;
15967 +
15968 +       BT_DBG("session %p subcmd 0x%02x appl %d msgnum %d", session, subcmd, appl, msgnum);
15969 +
15970 +       if (!(skb = alloc_skb(CAPI_MSG_BASELEN + 6 + len, GFP_ATOMIC))) {
15971 +               BT_ERR("Can't allocate memory for interoperability packet");
15972 +               return;
15973 +       }
15974 +
15975 +       s = skb_put(skb, CAPI_MSG_BASELEN + 6 + len);
15976 +
15977 +       capimsg_setu16(s, 0, CAPI_MSG_BASELEN + 6 + len);
15978 +       capimsg_setu16(s, 2, appl);
15979 +       capimsg_setu8 (s, 4, CAPI_INTEROPERABILITY);
15980 +       capimsg_setu8 (s, 5, subcmd);
15981 +       capimsg_setu16(s, 6, msgnum);
15982 +
15983 +       /* Interoperability selector (Bluetooth Device Management) */
15984 +       capimsg_setu16(s, 8, 0x0001);
15985 +
15986 +       capimsg_setu8 (s, 10, 3 + len);
15987 +       capimsg_setu16(s, 11, function);
15988 +       capimsg_setu8 (s, 13, len);
15989 +
15990 +       if (len > 0)
15991 +               memcpy(s + 14, buf, len);
15992 +
15993 +       cmtp_send_capimsg(session, skb);
15994 +}
15995 +
15996 +static void cmtp_recv_interopmsg(struct cmtp_session *session, struct sk_buff *skb)
15997 +{
15998 +       struct capi_ctr *ctrl = session->ctrl;
15999 +       struct cmtp_application *application;
16000 +       __u16 appl, msgnum, func, info;
16001 +       __u32 controller;
16002 +
16003 +       BT_DBG("session %p skb %p len %d", session, skb, skb->len);
16004 +
16005 +       switch (CAPIMSG_SUBCOMMAND(skb->data)) {
16006 +       case CAPI_CONF:
16007 +               func = CAPIMSG_U16(skb->data, CAPI_MSG_BASELEN + 5);
16008 +               info = CAPIMSG_U16(skb->data, CAPI_MSG_BASELEN + 8);
16009 +
16010 +               switch (func) {
16011 +               case CAPI_FUNCTION_REGISTER:
16012 +                       msgnum = CAPIMSG_MSGID(skb->data);
16013 +
16014 +                       application = cmtp_application_get(session, CMTP_MSGNUM, msgnum);
16015 +                       if (application) {
16016 +                               application->state = BT_CONNECTED;
16017 +                               application->msgnum = 0;
16018 +                               application->mapping = CAPIMSG_APPID(skb->data);
16019 +                               wake_up_interruptible(&session->wait);
16020 +                       }
16021 +
16022 +                       break;
16023 +
16024 +               case CAPI_FUNCTION_RELEASE:
16025 +                       appl = CAPIMSG_APPID(skb->data);
16026 +
16027 +                       application = cmtp_application_get(session, CMTP_MAPPING, appl);
16028 +                       if (application) {
16029 +                               application->state = BT_CLOSED;
16030 +                               application->msgnum = 0;
16031 +                               wake_up_interruptible(&session->wait);
16032 +                       }
16033 +
16034 +                       break;
16035 +
16036 +               case CAPI_FUNCTION_GET_PROFILE:
16037 +                       controller = CAPIMSG_U16(skb->data, CAPI_MSG_BASELEN + 11);
16038 +                       msgnum = CAPIMSG_MSGID(skb->data);
16039 +
16040 +                       if (!info && (msgnum == CMTP_INITIAL_MSGNUM)) {
16041 +                               session->ncontroller = controller;
16042 +                               wake_up_interruptible(&session->wait);
16043 +                               break;
16044 +                       }
16045 +
16046 +                       if (!info && ctrl) {
16047 +                               memcpy(&ctrl->profile,
16048 +                                       skb->data + CAPI_MSG_BASELEN + 11,
16049 +                                       sizeof(capi_profile));
16050 +                               session->state = BT_CONNECTED;
16051 +                               ctrl->ready(ctrl);
16052 +                       }
16053 +
16054 +                       break;
16055 +
16056 +               case CAPI_FUNCTION_GET_MANUFACTURER:
16057 +                       controller = CAPIMSG_U32(skb->data, CAPI_MSG_BASELEN + 10);
16058 +
16059 +                       if (!info && ctrl) {
16060 +                               strncpy(ctrl->manu,
16061 +                                       skb->data + CAPI_MSG_BASELEN + 15,
16062 +                                       skb->data[CAPI_MSG_BASELEN + 14]);
16063 +                       }
16064 +
16065 +                       break;
16066 +
16067 +               case CAPI_FUNCTION_GET_VERSION:
16068 +                       controller = CAPIMSG_U32(skb->data, CAPI_MSG_BASELEN + 12);
16069 +
16070 +                       if (!info && ctrl) {
16071 +                               ctrl->version.majorversion = CAPIMSG_U32(skb->data, CAPI_MSG_BASELEN + 16);
16072 +                               ctrl->version.minorversion = CAPIMSG_U32(skb->data, CAPI_MSG_BASELEN + 20);
16073 +                               ctrl->version.majormanuversion = CAPIMSG_U32(skb->data, CAPI_MSG_BASELEN + 24);
16074 +                               ctrl->version.minormanuversion = CAPIMSG_U32(skb->data, CAPI_MSG_BASELEN + 28);
16075 +                       }
16076 +
16077 +                       break;
16078 +
16079 +               case CAPI_FUNCTION_GET_SERIAL_NUMBER:
16080 +                       controller = CAPIMSG_U32(skb->data, CAPI_MSG_BASELEN + 12);
16081 +
16082 +                       if (!info && ctrl) {
16083 +                               memset(ctrl->serial, 0, CAPI_SERIAL_LEN);
16084 +                               strncpy(ctrl->serial,
16085 +                                       skb->data + CAPI_MSG_BASELEN + 17,
16086 +                                       skb->data[CAPI_MSG_BASELEN + 16]);
16087 +                       }
16088 +
16089 +                       break;
16090 +               }
16091 +
16092 +               break;
16093 +
16094 +       case CAPI_IND:
16095 +               func = CAPIMSG_U16(skb->data, CAPI_MSG_BASELEN + 3);
16096 +
16097 +               if (func == CAPI_FUNCTION_LOOPBACK) {
16098 +                       appl = CAPIMSG_APPID(skb->data);
16099 +                       msgnum = CAPIMSG_MSGID(skb->data);
16100 +                       cmtp_send_interopmsg(session, CAPI_RESP, appl, msgnum, func,
16101 +                                               skb->data + CAPI_MSG_BASELEN + 6,
16102 +                                               skb->data[CAPI_MSG_BASELEN + 5]);
16103 +               }
16104 +
16105 +               break;
16106 +       }
16107 +
16108 +       kfree_skb(skb);
16109 +}
16110 +
16111 +void cmtp_recv_capimsg(struct cmtp_session *session, struct sk_buff *skb)
16112 +{
16113 +       struct capi_ctr *ctrl = session->ctrl;
16114 +       struct cmtp_application *application;
16115 +       __u16 cmd, appl, info;
16116 +       __u32 ncci, contr;
16117 +
16118 +       BT_DBG("session %p skb %p len %d", session, skb, skb->len);
16119 +
16120 +       if (CAPIMSG_COMMAND(skb->data) == CAPI_INTEROPERABILITY) {
16121 +               cmtp_recv_interopmsg(session, skb);
16122 +               return;
16123 +       }
16124 +
16125 +       if (session->flags & (1 << CMTP_LOOPBACK)) {
16126 +               kfree_skb(skb);
16127 +               return;
16128 +       }
16129 +
16130 +       cmd = CAPICMD(CAPIMSG_COMMAND(skb->data), CAPIMSG_SUBCOMMAND(skb->data));
16131 +       appl = CAPIMSG_APPID(skb->data);
16132 +       contr = CAPIMSG_CONTROL(skb->data);
16133 +
16134 +       application = cmtp_application_get(session, CMTP_MAPPING, appl);
16135 +       if (application) {
16136 +               appl = application->appl;
16137 +               CAPIMSG_SETAPPID(skb->data, appl);
16138 +       } else {
16139 +               BT_ERR("Can't find application with id %d", appl);
16140 +               kfree_skb(skb);
16141 +               return;
16142 +       }
16143 +
16144 +       if ((contr & 0x7f) == 0x01) {
16145 +               contr = (contr & 0xffffff80) | session->num;
16146 +               CAPIMSG_SETCONTROL(skb->data, contr);
16147 +       }
16148 +
16149 +       if (!ctrl) {
16150 +               BT_ERR("Can't find controller %d for message", session->num);
16151 +               kfree_skb(skb);
16152 +               return;
16153 +       }
16154 +
16155 +       switch (cmd) {
16156 +       case CAPI_CONNECT_B3_CONF:
16157 +               ncci = CAPIMSG_NCCI(skb->data);
16158 +               info = CAPIMSG_U16(skb->data, 12);
16159 +
16160 +               BT_DBG("CONNECT_B3_CONF ncci 0x%02x info 0x%02x", ncci, info);
16161 +
16162 +               if (info == 0)
16163 +                       ctrl->new_ncci(ctrl, appl, ncci, 8);
16164 +
16165 +               ctrl->handle_capimsg(ctrl, appl, skb);
16166 +               break;
16167 +
16168 +       case CAPI_CONNECT_B3_IND:
16169 +               ncci = CAPIMSG_NCCI(skb->data);
16170 +
16171 +               BT_DBG("CONNECT_B3_IND ncci 0x%02x", ncci);
16172 +
16173 +               ctrl->new_ncci(ctrl, appl, ncci, 8);
16174 +               ctrl->handle_capimsg(ctrl, appl, skb);
16175 +               break;
16176 +
16177 +       case CAPI_DISCONNECT_B3_IND:
16178 +               ncci = CAPIMSG_NCCI(skb->data);
16179 +
16180 +               BT_DBG("DISCONNECT_B3_IND ncci 0x%02x", ncci);
16181 +
16182 +               if (ncci == 0xffffffff)
16183 +                       BT_ERR("DISCONNECT_B3_IND with ncci 0xffffffff");
16184 +
16185 +               ctrl->handle_capimsg(ctrl, appl, skb);
16186 +               ctrl->free_ncci(ctrl, appl, ncci);
16187 +               break;
16188 +
16189 +       default:
16190 +               ctrl->handle_capimsg(ctrl, appl, skb);
16191 +               break;
16192 +       }
16193 +}
16194 +
16195 +void cmtp_send_capimsg(struct cmtp_session *session, struct sk_buff *skb)
16196 +{
16197 +       struct cmtp_scb *scb = (void *) skb->cb;
16198 +
16199 +       BT_DBG("session %p skb %p len %d", session, skb, skb->len);
16200 +
16201 +       scb->id = -1;
16202 +       scb->data = (CAPIMSG_COMMAND(skb->data) == CAPI_DATA_B3);
16203 +
16204 +       skb_queue_tail(&session->transmit, skb);
16205 +
16206 +       cmtp_schedule(session);
16207 +}
16208 +
16209 +
16210 +static int cmtp_load_firmware(struct capi_ctr *ctrl, capiloaddata *data)
16211 +{
16212 +       BT_DBG("ctrl %p data %p", ctrl, data);
16213 +
16214 +       return -EIO;
16215 +}
16216 +
16217 +static void cmtp_reset_ctr(struct capi_ctr *ctrl)
16218 +{
16219 +       BT_DBG("ctrl %p", ctrl);
16220 +
16221 +       ctrl->reseted(ctrl);
16222 +}
16223 +
16224 +static void cmtp_remove_ctr(struct capi_ctr *ctrl)
16225 +{
16226 +       struct cmtp_session *session = ctrl->driverdata;
16227 +
16228 +       BT_DBG("ctrl %p", ctrl);
16229 +
16230 +       ctrl->suspend_output(ctrl);
16231 +
16232 +       atomic_inc(&session->terminate);
16233 +       cmtp_schedule(session);
16234 +}
16235 +
16236 +static void cmtp_register_appl(struct capi_ctr *ctrl, __u16 appl, capi_register_params *rp)
16237 +{
16238 +       DECLARE_WAITQUEUE(wait, current);
16239 +       struct cmtp_session *session = ctrl->driverdata;
16240 +       struct cmtp_application *application;
16241 +       unsigned long timeo = CMTP_INTEROP_TIMEOUT;
16242 +       unsigned char buf[8];
16243 +       int err = 0, nconn, want = rp->level3cnt;
16244 +
16245 +       BT_DBG("ctrl %p appl %d level3cnt %d datablkcnt %d datablklen %d",
16246 +               ctrl, appl, rp->level3cnt, rp->datablkcnt, rp->datablklen);
16247 +
16248 +       application = cmtp_application_add(session, appl);
16249 +       if (!application) {
16250 +               BT_ERR("Can't allocate memory for new application");
16251 +               ctrl->appl_released(ctrl, appl);
16252 +               return;
16253 +       }
16254 +
16255 +       if (want < 0)
16256 +               nconn = ctrl->profile.nbchannel * -want;
16257 +       else
16258 +               nconn = want;
16259 +
16260 +       if (nconn == 0)
16261 +               nconn = ctrl->profile.nbchannel;
16262 +
16263 +       capimsg_setu16(buf, 0, nconn);
16264 +       capimsg_setu16(buf, 2, rp->datablkcnt);
16265 +       capimsg_setu16(buf, 4, rp->datablklen);
16266 +
16267 +       application->state = BT_CONFIG;
16268 +       application->msgnum = cmtp_msgnum_get(session);
16269 +
16270 +       cmtp_send_interopmsg(session, CAPI_REQ, 0x0000, application->msgnum,
16271 +                               CAPI_FUNCTION_REGISTER, buf, 6);
16272 +
16273 +       add_wait_queue(&session->wait, &wait);
16274 +       while (1) {
16275 +               set_current_state(TASK_INTERRUPTIBLE);
16276 +
16277 +               if (!timeo) {
16278 +                       err = -EAGAIN;
16279 +                       break;
16280 +               }
16281 +
16282 +               if (application->state == BT_CLOSED) {
16283 +                       err = -application->err;
16284 +                       break;
16285 +               }
16286 +
16287 +               if (application->state == BT_CONNECTED)
16288 +                       break;
16289 +
16290 +               if (signal_pending(current)) {
16291 +                       err = -EINTR;
16292 +                       break;
16293 +               }
16294 +
16295 +               timeo = schedule_timeout(timeo);
16296 +       }
16297 +       set_current_state(TASK_RUNNING);
16298 +       remove_wait_queue(&session->wait, &wait);
16299 +
16300 +       if (err) {
16301 +               ctrl->appl_released(ctrl, appl);
16302 +               cmtp_application_del(session, application);
16303 +               return;
16304 +       }
16305 +
16306 +       ctrl->appl_registered(ctrl, appl);
16307 +}
16308 +
16309 +static void cmtp_release_appl(struct capi_ctr *ctrl, __u16 appl)
16310 +{
16311 +       DECLARE_WAITQUEUE(wait, current);
16312 +       struct cmtp_session *session = ctrl->driverdata;
16313 +       struct cmtp_application *application;
16314 +       unsigned long timeo = CMTP_INTEROP_TIMEOUT;
16315 +
16316 +       BT_DBG("ctrl %p appl %d", ctrl, appl);
16317 +
16318 +       application = cmtp_application_get(session, CMTP_APPLID, appl);
16319 +       if (!application) {
16320 +               BT_ERR("Can't find application");
16321 +               return;
16322 +       }
16323 +
16324 +       application->msgnum = cmtp_msgnum_get(session);
16325 +
16326 +       cmtp_send_interopmsg(session, CAPI_REQ, application->mapping, application->msgnum,
16327 +                               CAPI_FUNCTION_RELEASE, NULL, 0);
16328 +
16329 +       add_wait_queue(&session->wait, &wait);
16330 +       while (timeo) {
16331 +               set_current_state(TASK_INTERRUPTIBLE);
16332 +
16333 +               if (application->state == BT_CLOSED)
16334 +                       break;
16335 +
16336 +               if (signal_pending(current))
16337 +                       break;
16338 +
16339 +               timeo = schedule_timeout(timeo);
16340 +       }
16341 +       set_current_state(TASK_RUNNING);
16342 +       remove_wait_queue(&session->wait, &wait);
16343 +
16344 +       cmtp_application_del(session, application);
16345 +       ctrl->appl_released(ctrl, appl);
16346 +}
16347 +
16348 +static void cmtp_send_message(struct capi_ctr *ctrl, struct sk_buff *skb)
16349 +{
16350 +       struct cmtp_session *session = ctrl->driverdata;
16351 +       struct cmtp_application *application;
16352 +       __u16 appl;
16353 +       __u32 contr;
16354 +
16355 +       BT_DBG("ctrl %p skb %p", ctrl, skb);
16356 +
16357 +       appl = CAPIMSG_APPID(skb->data);
16358 +       contr = CAPIMSG_CONTROL(skb->data);
16359 +
16360 +       application = cmtp_application_get(session, CMTP_APPLID, appl);
16361 +       if ((!application) || (application->state != BT_CONNECTED)) {
16362 +               BT_ERR("Can't find application with id %d", appl);
16363 +               kfree_skb(skb);
16364 +               return;
16365 +       }
16366 +
16367 +       CAPIMSG_SETAPPID(skb->data, application->mapping);
16368 +
16369 +       if ((contr & 0x7f) == session->num) {
16370 +               contr = (contr & 0xffffff80) | 0x01;
16371 +               CAPIMSG_SETCONTROL(skb->data, contr);
16372 +       }
16373 +
16374 +       cmtp_send_capimsg(session, skb);
16375 +}
16376 +
16377 +static char *cmtp_procinfo(struct capi_ctr *ctrl)
16378 +{
16379 +       return "CAPI Message Transport Protocol";
16380 +}
16381 +
16382 +static int cmtp_ctr_read_proc(char *page, char **start, off_t off, int count, int *eof, struct capi_ctr *ctrl)
16383 +{
16384 +       struct cmtp_session *session = ctrl->driverdata;
16385 +       struct cmtp_application *app;
16386 +       struct list_head *p, *n;
16387 +       int len = 0;
16388 +
16389 +       len += sprintf(page + len, "%s (Revision %s)\n\n", cmtp_procinfo(ctrl), REVISION);
16390 +       len += sprintf(page + len, "addr %s\n", session->name);
16391 +       len += sprintf(page + len, "ctrl %d\n", session->num);
16392 +
16393 +       list_for_each_safe(p, n, &session->applications) {
16394 +               app = list_entry(p, struct cmtp_application, list);
16395 +               len += sprintf(page + len, "appl %d -> %d\n", app->appl, app->mapping);
16396 +       }
16397 +
16398 +       if (off + count >= len)
16399 +               *eof = 1;
16400 +
16401 +       if (len < off)
16402 +               return 0;
16403 +
16404 +       *start = page + off;
16405 +
16406 +       return ((count < len - off) ? count : len - off);
16407 +}
16408 +
16409 +static struct capi_driver cmtp_driver = {
16410 +       name:           "cmtp",
16411 +       revision:       REVISION,
16412 +       load_firmware:  cmtp_load_firmware,
16413 +       reset_ctr:      cmtp_reset_ctr,
16414 +       remove_ctr:     cmtp_remove_ctr,
16415 +       register_appl:  cmtp_register_appl,
16416 +       release_appl:   cmtp_release_appl,
16417 +       send_message:   cmtp_send_message,
16418 +       procinfo:       cmtp_procinfo,
16419 +       ctr_read_proc:  cmtp_ctr_read_proc,
16420 +
16421 +       driver_read_proc:       0,
16422 +       add_card:               0,
16423 +};
16424 +
16425 +
16426 +int cmtp_attach_device(struct cmtp_session *session)
16427 +{
16428 +       DECLARE_WAITQUEUE(wait, current);
16429 +       unsigned long timeo = CMTP_INTEROP_TIMEOUT;
16430 +       unsigned char buf[4];
16431 +
16432 +       BT_DBG("session %p", session);
16433 +
16434 +       capimsg_setu32(buf, 0, 0);
16435 +
16436 +       cmtp_send_interopmsg(session, CAPI_REQ, 0xffff, CMTP_INITIAL_MSGNUM,
16437 +                               CAPI_FUNCTION_GET_PROFILE, buf, 4);
16438 +
16439 +       add_wait_queue(&session->wait, &wait);
16440 +       while (timeo) {
16441 +               set_current_state(TASK_INTERRUPTIBLE);
16442 +
16443 +               if (session->ncontroller)
16444 +                       break;
16445 +
16446 +               if (signal_pending(current))
16447 +                       break;
16448 +
16449 +               timeo = schedule_timeout(timeo);
16450 +       }
16451 +       set_current_state(TASK_RUNNING);
16452 +       remove_wait_queue(&session->wait, &wait);
16453 +
16454 +       BT_INFO("Found %d CAPI controller(s) on device %s", session->ncontroller, session->name);
16455 +
16456 +       if (!timeo)
16457 +               return -ETIMEDOUT;
16458 +
16459 +       if (!session->ncontroller)
16460 +               return -ENODEV;
16461 +
16462 +
16463 +       if (session->ncontroller > 1)
16464 +               BT_INFO("Setting up only CAPI controller 1");
16465 +
16466 +       if (!(session->ctrl = di->attach_ctr(&cmtp_driver, session->name, session))) {
16467 +               BT_ERR("Can't attach new controller");
16468 +               return -EBUSY;
16469 +       }
16470 +
16471 +       session->num = session->ctrl->cnr;
16472 +
16473 +       BT_DBG("session %p ctrl %p num %d", session, session->ctrl, session->num);
16474 +
16475 +       capimsg_setu32(buf, 0, 1);
16476 +
16477 +       cmtp_send_interopmsg(session, CAPI_REQ, 0xffff, cmtp_msgnum_get(session),
16478 +                               CAPI_FUNCTION_GET_MANUFACTURER, buf, 4);
16479 +
16480 +       cmtp_send_interopmsg(session, CAPI_REQ, 0xffff, cmtp_msgnum_get(session),
16481 +                               CAPI_FUNCTION_GET_VERSION, buf, 4);
16482 +
16483 +       cmtp_send_interopmsg(session, CAPI_REQ, 0xffff, cmtp_msgnum_get(session),
16484 +                               CAPI_FUNCTION_GET_SERIAL_NUMBER, buf, 4);
16485 +
16486 +       cmtp_send_interopmsg(session, CAPI_REQ, 0xffff, cmtp_msgnum_get(session),
16487 +                               CAPI_FUNCTION_GET_PROFILE, buf, 4);
16488 +
16489 +       return 0;
16490 +}
16491 +
16492 +void cmtp_detach_device(struct cmtp_session *session)
16493 +{
16494 +       struct capi_ctr *ctrl = session->ctrl;
16495 +
16496 +       BT_DBG("session %p ctrl %p", session, ctrl);
16497 +
16498 +       if (!ctrl)
16499 +               return;
16500 +
16501 +       ctrl->reseted(ctrl);
16502 +
16503 +       di->detach_ctr(ctrl);
16504 +}
16505 +
16506 +int cmtp_init_capi(void)
16507 +{
16508 +       if (!(di = attach_capi_driver(&cmtp_driver))) {
16509 +               BT_ERR("Can't attach CAPI driver");
16510 +               return -EIO;
16511 +       }
16512 +
16513 +       return 0;
16514 +}
16515 +
16516 +void cmtp_cleanup_capi(void)
16517 +{
16518 +       detach_capi_driver(&cmtp_driver);
16519 +}
16520 diff -urN linux-2.4.18/net/bluetooth/cmtp/cmtp.h linux-2.4.18-mh15/net/bluetooth/cmtp/cmtp.h
16521 --- linux-2.4.18/net/bluetooth/cmtp/cmtp.h      1970-01-01 01:00:00.000000000 +0100
16522 +++ linux-2.4.18-mh15/net/bluetooth/cmtp/cmtp.h 2004-08-01 16:26:23.000000000 +0200
16523 @@ -0,0 +1,138 @@
16524 +/* 
16525 +   CMTP implementation for Linux Bluetooth stack (BlueZ).
16526 +   Copyright (C) 2002-2003 Marcel Holtmann <marcel@holtmann.org>
16527 +
16528 +   This program is free software; you can redistribute it and/or modify
16529 +   it under the terms of the GNU General Public License version 2 as
16530 +   published by the Free Software Foundation;
16531 +
16532 +   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
16533 +   OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16534 +   FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS.
16535 +   IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) AND AUTHOR(S) BE LIABLE FOR ANY
16536 +   CLAIM, OR ANY SPECIAL INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES 
16537 +   WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 
16538 +   ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 
16539 +   OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16540 +
16541 +   ALL LIABILITY, INCLUDING LIABILITY FOR INFRINGEMENT OF ANY PATENTS, 
16542 +   COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS, RELATING TO USE OF THIS 
16543 +   SOFTWARE IS DISCLAIMED.
16544 +*/
16545 +
16546 +#ifndef __CMTP_H
16547 +#define __CMTP_H
16548 +
16549 +#include <linux/types.h>
16550 +#include <net/bluetooth/bluetooth.h>
16551 +
16552 +#define BTNAMSIZ 18
16553 +
16554 +/* CMTP ioctl defines */
16555 +#define CMTPCONNADD    _IOW('C', 200, int)
16556 +#define CMTPCONNDEL    _IOW('C', 201, int)
16557 +#define CMTPGETCONNLIST        _IOR('C', 210, int)
16558 +#define CMTPGETCONNINFO        _IOR('C', 211, int)
16559 +
16560 +#define CMTP_LOOPBACK  0
16561 +
16562 +struct cmtp_connadd_req {
16563 +       int   sock;     // Connected socket
16564 +       __u32 flags;
16565 +};
16566 +
16567 +struct cmtp_conndel_req {
16568 +       bdaddr_t bdaddr;
16569 +       __u32    flags;
16570 +};
16571 +
16572 +struct cmtp_conninfo {
16573 +       bdaddr_t bdaddr;
16574 +       __u32    flags;
16575 +       __u16    state;
16576 +       int      num;
16577 +};
16578 +
16579 +struct cmtp_connlist_req {
16580 +       __u32  cnum;
16581 +       struct cmtp_conninfo *ci;
16582 +};
16583 +
16584 +int cmtp_add_connection(struct cmtp_connadd_req *req, struct socket *sock);
16585 +int cmtp_del_connection(struct cmtp_conndel_req *req);
16586 +int cmtp_get_connlist(struct cmtp_connlist_req *req);
16587 +int cmtp_get_conninfo(struct cmtp_conninfo *ci);
16588 +
16589 +/* CMTP session defines */
16590 +#define CMTP_INTEROP_TIMEOUT   (HZ * 5)
16591 +#define CMTP_INITIAL_MSGNUM    0xff00
16592 +
16593 +struct cmtp_session {
16594 +       struct list_head list;
16595 +
16596 +       struct socket *sock;
16597 +
16598 +       bdaddr_t bdaddr;
16599 +
16600 +       unsigned long state;
16601 +       unsigned long flags;
16602 +
16603 +       uint mtu;
16604 +
16605 +       char name[BTNAMSIZ];
16606 +
16607 +       atomic_t terminate;
16608 +
16609 +       wait_queue_head_t wait;
16610 +
16611 +       int ncontroller;
16612 +       int num;
16613 +       struct capi_ctr *ctrl;
16614 +
16615 +       struct list_head applications;
16616 +
16617 +       unsigned long blockids;
16618 +       int msgnum;
16619 +
16620 +       struct sk_buff_head transmit;
16621 +
16622 +       struct sk_buff *reassembly[16];
16623 +};
16624 +
16625 +struct cmtp_application {
16626 +       struct list_head list;
16627 +
16628 +       unsigned long state;
16629 +       int err;
16630 +
16631 +       __u16 appl;
16632 +       __u16 mapping;
16633 +
16634 +       __u16 msgnum;
16635 +};
16636 +
16637 +struct cmtp_scb {
16638 +       int id;
16639 +       int data;
16640 +};
16641 +
16642 +int  cmtp_attach_device(struct cmtp_session *session);
16643 +void cmtp_detach_device(struct cmtp_session *session);
16644 +
16645 +void cmtp_recv_capimsg(struct cmtp_session *session, struct sk_buff *skb);
16646 +void cmtp_send_capimsg(struct cmtp_session *session, struct sk_buff *skb);
16647 +
16648 +static inline void cmtp_schedule(struct cmtp_session *session)
16649 +{
16650 +       struct sock *sk = session->sock->sk;
16651 +
16652 +       wake_up_interruptible(sk->sleep);
16653 +}
16654 +
16655 +/* CMTP init defines */
16656 +int cmtp_init_capi(void);
16657 +int cmtp_init_sockets(void);
16658 +void cmtp_cleanup_capi(void);
16659 +void cmtp_cleanup_sockets(void);
16660 +
16661 +#endif /* __CMTP_H */
16662 diff -urN linux-2.4.18/net/bluetooth/cmtp/Config.in linux-2.4.18-mh15/net/bluetooth/cmtp/Config.in
16663 --- linux-2.4.18/net/bluetooth/cmtp/Config.in   1970-01-01 01:00:00.000000000 +0100
16664 +++ linux-2.4.18-mh15/net/bluetooth/cmtp/Config.in      2004-08-01 16:26:23.000000000 +0200
16665 @@ -0,0 +1,7 @@
16666 +#
16667 +# Bluetooth CMTP layer configuration
16668 +#
16669 +
16670 +if [ "$CONFIG_ISDN" = "y" -o "$CONFIG_ISDN" = "m" ]; then 
16671 +   dep_tristate 'CMTP protocol support' CONFIG_BLUEZ_CMTP $CONFIG_ISDN_CAPI $CONFIG_BLUEZ_L2CAP
16672 +fi
16673 diff -urN linux-2.4.18/net/bluetooth/cmtp/core.c linux-2.4.18-mh15/net/bluetooth/cmtp/core.c
16674 --- linux-2.4.18/net/bluetooth/cmtp/core.c      1970-01-01 01:00:00.000000000 +0100
16675 +++ linux-2.4.18-mh15/net/bluetooth/cmtp/core.c 2004-08-01 16:26:23.000000000 +0200
16676 @@ -0,0 +1,515 @@
16677 +/* 
16678 +   CMTP implementation for Linux Bluetooth stack (BlueZ).
16679 +   Copyright (C) 2002-2003 Marcel Holtmann <marcel@holtmann.org>
16680 +
16681 +   This program is free software; you can redistribute it and/or modify
16682 +   it under the terms of the GNU General Public License version 2 as
16683 +   published by the Free Software Foundation;
16684 +
16685 +   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
16686 +   OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16687 +   FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS.
16688 +   IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) AND AUTHOR(S) BE LIABLE FOR ANY
16689 +   CLAIM, OR ANY SPECIAL INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES 
16690 +   WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 
16691 +   ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 
16692 +   OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16693 +
16694 +   ALL LIABILITY, INCLUDING LIABILITY FOR INFRINGEMENT OF ANY PATENTS, 
16695 +   COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS, RELATING TO USE OF THIS 
16696 +   SOFTWARE IS DISCLAIMED.
16697 +*/
16698 +
16699 +#include <linux/config.h>
16700 +#include <linux/module.h>
16701 +
16702 +#include <linux/types.h>
16703 +#include <linux/errno.h>
16704 +#include <linux/kernel.h>
16705 +#include <linux/major.h>
16706 +#include <linux/sched.h>
16707 +#include <linux/slab.h>
16708 +#include <linux/poll.h>
16709 +#include <linux/fcntl.h>
16710 +#include <linux/skbuff.h>
16711 +#include <linux/socket.h>
16712 +#include <linux/ioctl.h>
16713 +#include <linux/file.h>
16714 +#include <linux/init.h>
16715 +#include <net/sock.h>
16716 +
16717 +#include <net/bluetooth/bluetooth.h>
16718 +#include <net/bluetooth/l2cap.h>
16719 +
16720 +#include "cmtp.h"
16721 +
16722 +#ifndef CONFIG_BLUEZ_CMTP_DEBUG
16723 +#undef  BT_DBG
16724 +#define BT_DBG(D...)
16725 +#endif
16726 +
16727 +#define VERSION "1.0"
16728 +
16729 +static DECLARE_RWSEM(cmtp_session_sem);
16730 +static LIST_HEAD(cmtp_session_list);
16731 +
16732 +static struct cmtp_session *__cmtp_get_session(bdaddr_t *bdaddr)
16733 +{
16734 +       struct cmtp_session *session;
16735 +       struct list_head *p;
16736 +
16737 +       BT_DBG("");
16738 +
16739 +       list_for_each(p, &cmtp_session_list) {
16740 +               session = list_entry(p, struct cmtp_session, list);
16741 +               if (!bacmp(bdaddr, &session->bdaddr))
16742 +                       return session;
16743 +       }
16744 +       return NULL;
16745 +}
16746 +
16747 +static void __cmtp_link_session(struct cmtp_session *session)
16748 +{
16749 +       MOD_INC_USE_COUNT;
16750 +       list_add(&session->list, &cmtp_session_list);
16751 +}
16752 +
16753 +static void __cmtp_unlink_session(struct cmtp_session *session)
16754 +{
16755 +       list_del(&session->list);
16756 +       MOD_DEC_USE_COUNT;
16757 +}
16758 +
16759 +static void __cmtp_copy_session(struct cmtp_session *session, struct cmtp_conninfo *ci)
16760 +{
16761 +       bacpy(&ci->bdaddr, &session->bdaddr);
16762 +
16763 +       ci->flags = session->flags;
16764 +       ci->state = session->state;
16765 +
16766 +       ci->num = session->num;
16767 +}
16768 +
16769 +
16770 +static inline int cmtp_alloc_block_id(struct cmtp_session *session)
16771 +{
16772 +       int i, id = -1;
16773 +
16774 +       for (i = 0; i < 16; i++)
16775 +               if (!test_and_set_bit(i, &session->blockids)) {
16776 +                       id = i;
16777 +                       break;
16778 +               }
16779 +
16780 +       return id;
16781 +}
16782 +
16783 +static inline void cmtp_free_block_id(struct cmtp_session *session, int id)
16784 +{
16785 +       clear_bit(id, &session->blockids);
16786 +}
16787 +
16788 +static inline void cmtp_add_msgpart(struct cmtp_session *session, int id, const unsigned char *buf, int count)
16789 +{
16790 +       struct sk_buff *skb = session->reassembly[id], *nskb;
16791 +       int size;
16792 +
16793 +       BT_DBG("session %p buf %p count %d", session, buf, count);
16794 +
16795 +       size = (skb) ? skb->len + count : count;
16796 +
16797 +       if (!(nskb = alloc_skb(size, GFP_ATOMIC))) {
16798 +               BT_ERR("Can't allocate memory for CAPI message");
16799 +               return;
16800 +       }
16801 +
16802 +       if (skb && (skb->len > 0))
16803 +               memcpy(skb_put(nskb, skb->len), skb->data, skb->len);
16804 +
16805 +       memcpy(skb_put(nskb, count), buf, count);
16806 +
16807 +       session->reassembly[id] = nskb;
16808 +
16809 +       if (skb)
16810 +               kfree_skb(skb);
16811 +}
16812 +
16813 +static inline int cmtp_recv_frame(struct cmtp_session *session, struct sk_buff *skb)
16814 +{
16815 +       __u8 hdr, hdrlen, id;
16816 +       __u16 len;
16817 +
16818 +       BT_DBG("session %p skb %p len %d", session, skb, skb->len);
16819 +
16820 +       while (skb->len > 0) {
16821 +               hdr = skb->data[0];
16822 +
16823 +               switch (hdr & 0xc0) {
16824 +               case 0x40:
16825 +                       hdrlen = 2;
16826 +                       len = skb->data[1];
16827 +                       break;
16828 +               case 0x80:
16829 +                       hdrlen = 3;
16830 +                       len = skb->data[1] | (skb->data[2] << 8);
16831 +                       break;
16832 +               default:
16833 +                       hdrlen = 1;
16834 +                       len = 0;
16835 +                       break;
16836 +               }
16837 +
16838 +               id = (hdr & 0x3c) >> 2;
16839 +
16840 +               BT_DBG("hdr 0x%02x hdrlen %d len %d id %d", hdr, hdrlen, len, id);
16841 +
16842 +               if (hdrlen + len > skb->len) {
16843 +                       BT_ERR("Wrong size or header information in CMTP frame");
16844 +                       break;
16845 +               }
16846 +
16847 +               if (len == 0) {
16848 +                       skb_pull(skb, hdrlen);
16849 +                       continue;
16850 +               }
16851 +
16852 +               switch (hdr & 0x03) {
16853 +               case 0x00:
16854 +                       cmtp_add_msgpart(session, id, skb->data + hdrlen, len);
16855 +                       cmtp_recv_capimsg(session, session->reassembly[id]);
16856 +                       session->reassembly[id] = NULL;
16857 +                       break;
16858 +               case 0x01:
16859 +                       cmtp_add_msgpart(session, id, skb->data + hdrlen, len);
16860 +                       break;
16861 +               default:
16862 +                       if (session->reassembly[id] != NULL)
16863 +                               kfree_skb(session->reassembly[id]);
16864 +                       session->reassembly[id] = NULL;
16865 +                       break;
16866 +               }
16867 +
16868 +               skb_pull(skb, hdrlen + len);
16869 +       }
16870 +
16871 +       kfree_skb(skb);
16872 +       return 0;
16873 +}
16874 +
16875 +static int cmtp_send_frame(struct cmtp_session *session, unsigned char *data, int len)
16876 +{
16877 +       struct socket *sock = session->sock;
16878 +       struct iovec iv = { data, len };
16879 +       struct msghdr msg;
16880 +       int err;
16881 +
16882 +       BT_DBG("session %p data %p len %d", session, data, len);
16883 +
16884 +       if (!len)
16885 +               return 0;
16886 +
16887 +       memset(&msg, 0, sizeof(msg));
16888 +       msg.msg_iovlen = 1;
16889 +       msg.msg_iov = &iv;
16890 +
16891 +       err = sock->ops->sendmsg(sock, &msg, len, 0);
16892 +       return err;
16893 +}
16894 +
16895 +static int cmtp_process_transmit(struct cmtp_session *session)
16896 +{
16897 +       struct sk_buff *skb, *nskb;
16898 +       unsigned char *hdr;
16899 +       unsigned int size, tail;
16900 +
16901 +       BT_DBG("session %p", session);
16902 +
16903 +       if (!(nskb = alloc_skb(session->mtu, GFP_ATOMIC))) {
16904 +               BT_ERR("Can't allocate memory for new frame");
16905 +               return -ENOMEM;
16906 +       }
16907 +
16908 +       while ((skb = skb_dequeue(&session->transmit))) {
16909 +               struct cmtp_scb *scb = (void *) skb->cb;
16910 +
16911 +               if ((tail = (session->mtu - nskb->len)) < 5) {
16912 +                       cmtp_send_frame(session, nskb->data, nskb->len);
16913 +                       skb_trim(nskb, 0);
16914 +                       tail = session->mtu;
16915 +               }
16916 +
16917 +               size = min_t(uint, ((tail < 258) ? (tail - 2) : (tail - 3)), skb->len);
16918 +
16919 +               if ((scb->id < 0) && ((scb->id = cmtp_alloc_block_id(session)) < 0)) {
16920 +                       skb_queue_head(&session->transmit, skb);
16921 +                       break;
16922 +               }
16923 +
16924 +               if (size < 256) {
16925 +                       hdr = skb_put(nskb, 2);
16926 +                       hdr[0] = 0x40
16927 +                               | ((scb->id << 2) & 0x3c)
16928 +                               | ((skb->len == size) ? 0x00 : 0x01);
16929 +                       hdr[1] = size;
16930 +               } else {
16931 +                       hdr = skb_put(nskb, 3);
16932 +                       hdr[0] = 0x80
16933 +                               | ((scb->id << 2) & 0x3c)
16934 +                               | ((skb->len == size) ? 0x00 : 0x01);
16935 +                       hdr[1] = size & 0xff;
16936 +                       hdr[2] = size >> 8;
16937 +               }
16938 +
16939 +               memcpy(skb_put(nskb, size), skb->data, size);
16940 +               skb_pull(skb, size);
16941 +
16942 +               if (skb->len > 0) {
16943 +                       skb_queue_head(&session->transmit, skb);
16944 +               } else {
16945 +                       cmtp_free_block_id(session, scb->id);
16946 +                       if (scb->data) {
16947 +                               cmtp_send_frame(session, nskb->data, nskb->len);
16948 +                               skb_trim(nskb, 0);
16949 +                       }
16950 +                       kfree_skb(skb);
16951 +               }
16952 +       }
16953 +
16954 +       cmtp_send_frame(session, nskb->data, nskb->len);
16955 +
16956 +       kfree_skb(nskb);
16957 +
16958 +       return skb_queue_len(&session->transmit);
16959 +}
16960 +
16961 +static int cmtp_session(void *arg)
16962 +{
16963 +       struct cmtp_session *session = arg;
16964 +       struct sock *sk = session->sock->sk;
16965 +       struct sk_buff *skb;
16966 +       wait_queue_t wait;
16967 +
16968 +       BT_DBG("session %p", session);
16969 +
16970 +       daemonize(); reparent_to_init();
16971 +
16972 +       sprintf(current->comm, "kcmtpd_ctr_%d", session->num);
16973 +
16974 +       sigfillset(&current->blocked);
16975 +       flush_signals(current);
16976 +
16977 +       current->nice = -15;
16978 +
16979 +       set_fs(KERNEL_DS);
16980 +
16981 +       init_waitqueue_entry(&wait, current);
16982 +       add_wait_queue(sk->sleep, &wait);
16983 +       while (!atomic_read(&session->terminate)) {
16984 +               set_current_state(TASK_INTERRUPTIBLE);
16985 +
16986 +               if (sk->state != BT_CONNECTED)
16987 +                       break;
16988 +
16989 +               while ((skb = skb_dequeue(&sk->receive_queue))) {
16990 +                       skb_orphan(skb);
16991 +                       cmtp_recv_frame(session, skb);
16992 +               }
16993 +
16994 +               cmtp_process_transmit(session);
16995 +
16996 +               schedule();
16997 +       }
16998 +       set_current_state(TASK_RUNNING);
16999 +       remove_wait_queue(sk->sleep, &wait);
17000 +
17001 +       down_write(&cmtp_session_sem);
17002 +
17003 +       if (!(session->flags & (1 << CMTP_LOOPBACK)))
17004 +               cmtp_detach_device(session);
17005 +
17006 +       fput(session->sock->file);
17007 +
17008 +       __cmtp_unlink_session(session);
17009 +
17010 +       up_write(&cmtp_session_sem);
17011 +
17012 +       kfree(session);
17013 +       return 0;
17014 +}
17015 +
17016 +int cmtp_add_connection(struct cmtp_connadd_req *req, struct socket *sock)
17017 +{
17018 +       struct cmtp_session *session, *s;
17019 +       bdaddr_t src, dst;
17020 +       int i, err;
17021 +
17022 +       BT_DBG("");
17023 +
17024 +       baswap(&src, &bluez_pi(sock->sk)->src);
17025 +       baswap(&dst, &bluez_pi(sock->sk)->dst);
17026 +
17027 +       session = kmalloc(sizeof(struct cmtp_session), GFP_KERNEL);
17028 +       if (!session) 
17029 +               return -ENOMEM;
17030 +       memset(session, 0, sizeof(struct cmtp_session));
17031 +
17032 +       down_write(&cmtp_session_sem);
17033 +
17034 +       s = __cmtp_get_session(&bluez_pi(sock->sk)->dst);
17035 +       if (s && s->state == BT_CONNECTED) {
17036 +               err = -EEXIST;
17037 +               goto failed;
17038 +       }
17039 +
17040 +       bacpy(&session->bdaddr, &bluez_pi(sock->sk)->dst);
17041 +
17042 +       session->mtu = min_t(uint, l2cap_pi(sock->sk)->omtu, l2cap_pi(sock->sk)->imtu);
17043 +
17044 +       BT_DBG("mtu %d", session->mtu);
17045 +
17046 +       sprintf(session->name, "%s", batostr(&dst));
17047 +
17048 +       session->sock  = sock;
17049 +       session->state = BT_CONFIG;
17050 +
17051 +       init_waitqueue_head(&session->wait);
17052 +
17053 +       session->ctrl   = NULL;
17054 +       session->msgnum = CMTP_INITIAL_MSGNUM;
17055 +
17056 +       INIT_LIST_HEAD(&session->applications);
17057 +
17058 +       skb_queue_head_init(&session->transmit);
17059 +
17060 +       for (i = 0; i < 16; i++)
17061 +               session->reassembly[i] = NULL;
17062 +
17063 +       session->flags = req->flags;
17064 +
17065 +       __cmtp_link_session(session);
17066 +
17067 +       err = kernel_thread(cmtp_session, session, CLONE_FS | CLONE_FILES | CLONE_SIGHAND);
17068 +       if (err < 0)
17069 +               goto unlink;
17070 +
17071 +       if (!(session->flags & (1 << CMTP_LOOPBACK))) {
17072 +               err = cmtp_attach_device(session);
17073 +               if (err < 0)
17074 +                       goto detach;
17075 +       }
17076 +
17077 +       up_write(&cmtp_session_sem);
17078 +       return 0;
17079 +
17080 +detach:
17081 +       cmtp_detach_device(session);
17082 +
17083 +unlink:
17084 +       __cmtp_unlink_session(session);
17085 +
17086 +failed:
17087 +       up_write(&cmtp_session_sem);
17088 +       kfree(session);
17089 +       return err;
17090 +}
17091 +
17092 +int cmtp_del_connection(struct cmtp_conndel_req *req)
17093 +{
17094 +       struct cmtp_session *session;
17095 +       int err = 0;
17096 +
17097 +       BT_DBG("");
17098 +
17099 +       down_read(&cmtp_session_sem);
17100 +
17101 +       session = __cmtp_get_session(&req->bdaddr);
17102 +       if (session) {
17103 +               /* Flush the transmit queue */
17104 +               skb_queue_purge(&session->transmit);
17105 +
17106 +               /* Kill session thread */
17107 +               atomic_inc(&session->terminate);
17108 +               cmtp_schedule(session);
17109 +       } else
17110 +               err = -ENOENT;
17111 +
17112 +       up_read(&cmtp_session_sem);
17113 +       return err;
17114 +}
17115 +
17116 +int cmtp_get_connlist(struct cmtp_connlist_req *req)
17117 +{
17118 +       struct list_head *p;
17119 +       int err = 0, n = 0;
17120 +
17121 +       BT_DBG("");
17122 +
17123 +       down_read(&cmtp_session_sem);
17124 +
17125 +       list_for_each(p, &cmtp_session_list) {
17126 +               struct cmtp_session *session;
17127 +               struct cmtp_conninfo ci;
17128 +
17129 +               session = list_entry(p, struct cmtp_session, list);
17130 +
17131 +               __cmtp_copy_session(session, &ci);
17132 +
17133 +               if (copy_to_user(req->ci, &ci, sizeof(ci))) {
17134 +                       err = -EFAULT;
17135 +                       break;
17136 +               }
17137 +
17138 +               if (++n >= req->cnum)
17139 +                       break;
17140 +
17141 +               req->ci++;
17142 +       }
17143 +       req->cnum = n;
17144 +
17145 +       up_read(&cmtp_session_sem);
17146 +       return err;
17147 +}
17148 +
17149 +int cmtp_get_conninfo(struct cmtp_conninfo *ci)
17150 +{
17151 +       struct cmtp_session *session;
17152 +       int err = 0;
17153 +
17154 +       down_read(&cmtp_session_sem);
17155 +
17156 +       session = __cmtp_get_session(&ci->bdaddr);
17157 +       if (session)
17158 +               __cmtp_copy_session(session, ci);
17159 +       else
17160 +               err = -ENOENT;
17161 +
17162 +       up_read(&cmtp_session_sem);
17163 +       return err;
17164 +}
17165 +
17166 +
17167 +int __init init_cmtp(void)
17168 +{
17169 +       l2cap_load();
17170 +
17171 +       cmtp_init_capi();
17172 +       cmtp_init_sockets();
17173 +
17174 +       BT_INFO("BlueZ CMTP ver %s", VERSION);
17175 +       BT_INFO("Copyright (C) 2002-2003 Marcel Holtmann <marcel@holtmann.org>");
17176 +
17177 +       return 0;
17178 +}
17179 +
17180 +void __exit exit_cmtp(void)
17181 +{
17182 +       cmtp_cleanup_sockets();
17183 +       cmtp_cleanup_capi();
17184 +}
17185 +
17186 +module_init(init_cmtp);
17187 +module_exit(exit_cmtp);
17188 +
17189 +MODULE_AUTHOR("Marcel Holtmann <marcel@holtmann.org>");
17190 +MODULE_DESCRIPTION("BlueZ CMTP ver " VERSION);
17191 +MODULE_LICENSE("GPL");
17192 diff -urN linux-2.4.18/net/bluetooth/cmtp/Makefile linux-2.4.18-mh15/net/bluetooth/cmtp/Makefile
17193 --- linux-2.4.18/net/bluetooth/cmtp/Makefile    1970-01-01 01:00:00.000000000 +0100
17194 +++ linux-2.4.18-mh15/net/bluetooth/cmtp/Makefile       2004-08-01 16:26:23.000000000 +0200
17195 @@ -0,0 +1,10 @@
17196 +#
17197 +# Makefile for the Linux Bluetooth CMTP layer
17198 +#
17199 +
17200 +O_TARGET := cmtp.o
17201 +
17202 +obj-y  := core.o sock.o capi.o
17203 +obj-m  += $(O_TARGET)
17204 +
17205 +include $(TOPDIR)/Rules.make
17206 diff -urN linux-2.4.18/net/bluetooth/cmtp/sock.c linux-2.4.18-mh15/net/bluetooth/cmtp/sock.c
17207 --- linux-2.4.18/net/bluetooth/cmtp/sock.c      1970-01-01 01:00:00.000000000 +0100
17208 +++ linux-2.4.18-mh15/net/bluetooth/cmtp/sock.c 2004-08-01 16:26:23.000000000 +0200
17209 @@ -0,0 +1,208 @@
17210 +/* 
17211 +   CMTP implementation for Linux Bluetooth stack (BlueZ).
17212 +   Copyright (C) 2002-2003 Marcel Holtmann <marcel@holtmann.org>
17213 +
17214 +   This program is free software; you can redistribute it and/or modify
17215 +   it under the terms of the GNU General Public License version 2 as
17216 +   published by the Free Software Foundation;
17217 +
17218 +   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17219 +   OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17220 +   FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS.
17221 +   IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) AND AUTHOR(S) BE LIABLE FOR ANY
17222 +   CLAIM, OR ANY SPECIAL INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES 
17223 +   WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 
17224 +   ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 
17225 +   OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17226 +
17227 +   ALL LIABILITY, INCLUDING LIABILITY FOR INFRINGEMENT OF ANY PATENTS, 
17228 +   COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS, RELATING TO USE OF THIS 
17229 +   SOFTWARE IS DISCLAIMED.
17230 +*/
17231 +
17232 +#include <linux/config.h>
17233 +#include <linux/module.h>
17234 +
17235 +#include <linux/types.h>
17236 +#include <linux/errno.h>
17237 +#include <linux/kernel.h>
17238 +#include <linux/major.h>
17239 +#include <linux/sched.h>
17240 +#include <linux/slab.h>
17241 +#include <linux/poll.h>
17242 +#include <linux/fcntl.h>
17243 +#include <linux/skbuff.h>
17244 +#include <linux/socket.h>
17245 +#include <linux/ioctl.h>
17246 +#include <linux/file.h>
17247 +#include <net/sock.h>
17248 +
17249 +#include <asm/system.h>
17250 +#include <asm/uaccess.h>
17251 +
17252 +#include "cmtp.h"
17253 +
17254 +#ifndef CONFIG_BLUEZ_CMTP_DEBUG
17255 +#undef  BT_DBG
17256 +#define BT_DBG(D...)
17257 +#endif
17258 +
17259 +static int cmtp_sock_release(struct socket *sock)
17260 +{
17261 +       struct sock *sk = sock->sk;
17262 +
17263 +       BT_DBG("sock %p sk %p", sock, sk);
17264 +
17265 +       if (!sk)
17266 +               return 0;
17267 +
17268 +       sock_orphan(sk);
17269 +       sock_put(sk);
17270 +
17271 +       MOD_DEC_USE_COUNT;
17272 +       return 0;
17273 +}
17274 +
17275 +static int cmtp_sock_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
17276 +{
17277 +       struct cmtp_connadd_req ca;
17278 +       struct cmtp_conndel_req cd;
17279 +       struct cmtp_connlist_req cl;
17280 +       struct cmtp_conninfo ci;
17281 +       struct socket *nsock;
17282 +       int err;
17283 +
17284 +       BT_DBG("cmd %x arg %lx", cmd, arg);
17285 +
17286 +       switch (cmd) {
17287 +       case CMTPCONNADD:
17288 +               if (!capable(CAP_NET_ADMIN))
17289 +                       return -EACCES;
17290 +
17291 +               if (copy_from_user(&ca, (void *) arg, sizeof(ca)))
17292 +                       return -EFAULT;
17293 +
17294 +               nsock = sockfd_lookup(ca.sock, &err);
17295 +               if (!nsock)
17296 +                       return err;
17297 +
17298 +               if (nsock->sk->state != BT_CONNECTED) {
17299 +                       fput(nsock->file);
17300 +                       return -EBADFD;
17301 +               }
17302 +
17303 +               err = cmtp_add_connection(&ca, nsock);
17304 +               if (!err) {
17305 +                       if (copy_to_user((void *) arg, &ca, sizeof(ca)))
17306 +                               err = -EFAULT;
17307 +               } else
17308 +                       fput(nsock->file);
17309 +
17310 +               return err;
17311 +
17312 +       case CMTPCONNDEL:
17313 +               if (!capable(CAP_NET_ADMIN))
17314 +                       return -EACCES;
17315 +
17316 +               if (copy_from_user(&cd, (void *) arg, sizeof(cd)))
17317 +                       return -EFAULT;
17318 +
17319 +               return cmtp_del_connection(&cd);
17320 +
17321 +       case CMTPGETCONNLIST:
17322 +               if (copy_from_user(&cl, (void *) arg, sizeof(cl)))
17323 +                       return -EFAULT;
17324 +
17325 +               if (cl.cnum <= 0)
17326 +                       return -EINVAL;
17327 +
17328 +               err = cmtp_get_connlist(&cl);
17329 +               if (!err && copy_to_user((void *) arg, &cl, sizeof(cl)))
17330 +                       return -EFAULT;
17331 +
17332 +               return err;
17333 +
17334 +       case CMTPGETCONNINFO:
17335 +               if (copy_from_user(&ci, (void *) arg, sizeof(ci)))
17336 +                       return -EFAULT;
17337 +
17338 +               err = cmtp_get_conninfo(&ci);
17339 +               if (!err && copy_to_user((void *) arg, &ci, sizeof(ci)))
17340 +                       return -EFAULT;
17341 +
17342 +               return err;
17343 +       }
17344 +
17345 +       return -EINVAL;
17346 +}
17347 +
17348 +static struct proto_ops cmtp_sock_ops = {
17349 +       family:         PF_BLUETOOTH,
17350 +       release:        cmtp_sock_release,
17351 +       ioctl:          cmtp_sock_ioctl,
17352 +       bind:           sock_no_bind,
17353 +       getname:        sock_no_getname,
17354 +       sendmsg:        sock_no_sendmsg,
17355 +       recvmsg:        sock_no_recvmsg,
17356 +       poll:           sock_no_poll,
17357 +       listen:         sock_no_listen,
17358 +       shutdown:       sock_no_shutdown,
17359 +       setsockopt:     sock_no_setsockopt,
17360 +       getsockopt:     sock_no_getsockopt,
17361 +       connect:        sock_no_connect,
17362 +       socketpair:     sock_no_socketpair,
17363 +       accept:         sock_no_accept,
17364 +       mmap:           sock_no_mmap
17365 +};
17366 +
17367 +static int cmtp_sock_create(struct socket *sock, int protocol)
17368 +{
17369 +       struct sock *sk;
17370 +
17371 +       BT_DBG("sock %p", sock);
17372 +
17373 +       if (sock->type != SOCK_RAW)
17374 +               return -ESOCKTNOSUPPORT;
17375 +
17376 +       sock->ops = &cmtp_sock_ops;
17377 +
17378 +       if (!(sk = sk_alloc(PF_BLUETOOTH, GFP_KERNEL, 1)))
17379 +               return -ENOMEM;
17380 +
17381 +       MOD_INC_USE_COUNT;
17382 +
17383 +       sock->state = SS_UNCONNECTED;
17384 +       sock_init_data(sock, sk);
17385 +
17386 +       sk->destruct = NULL;
17387 +       sk->protocol = protocol;
17388 +
17389 +       return 0;
17390 +}
17391 +
17392 +static struct net_proto_family cmtp_sock_family_ops = {
17393 +       family:         PF_BLUETOOTH,
17394 +       create:         cmtp_sock_create
17395 +};
17396 +
17397 +int cmtp_init_sockets(void)
17398 +{
17399 +       int err;
17400 +
17401 +       if ((err = bluez_sock_register(BTPROTO_CMTP, &cmtp_sock_family_ops))) {
17402 +               BT_ERR("Can't register CMTP socket layer (%d)", err);
17403 +               return err;
17404 +       }
17405 +
17406 +       return 0;
17407 +}
17408 +
17409 +void cmtp_cleanup_sockets(void)
17410 +{
17411 +       int err;
17412 +
17413 +       if ((err = bluez_sock_unregister(BTPROTO_CMTP)))
17414 +               BT_ERR("Can't unregister CMTP socket layer (%d)", err);
17415 +
17416 +       return;
17417 +}
17418 diff -urN linux-2.4.18/net/bluetooth/Config.in linux-2.4.18-mh15/net/bluetooth/Config.in
17419 --- linux-2.4.18/net/bluetooth/Config.in        2001-06-12 04:15:27.000000000 +0200
17420 +++ linux-2.4.18-mh15/net/bluetooth/Config.in   2004-08-01 16:26:23.000000000 +0200
17421 @@ -1,16 +1,23 @@
17422  #
17423 -# Bluetooth configuration
17424 +# Bluetooth subsystem configuration
17425  #
17426  
17427  if [ "$CONFIG_NET" != "n" ]; then
17428 +
17429     mainmenu_option next_comment
17430     comment 'Bluetooth support'
17431     dep_tristate 'Bluetooth subsystem support' CONFIG_BLUEZ $CONFIG_NET
17432  
17433     if [ "$CONFIG_BLUEZ" != "n" ]; then
17434        dep_tristate 'L2CAP protocol support' CONFIG_BLUEZ_L2CAP $CONFIG_BLUEZ
17435 +      dep_tristate 'SCO links support' CONFIG_BLUEZ_SCO $CONFIG_BLUEZ
17436 +      source net/bluetooth/rfcomm/Config.in
17437 +      source net/bluetooth/bnep/Config.in
17438 +      source net/bluetooth/cmtp/Config.in
17439 +      source net/bluetooth/hidp/Config.in
17440        source drivers/bluetooth/Config.in
17441     fi
17442 +
17443     endmenu
17444  fi
17445  
17446 diff -urN linux-2.4.18/net/bluetooth/hci_conn.c linux-2.4.18-mh15/net/bluetooth/hci_conn.c
17447 --- linux-2.4.18/net/bluetooth/hci_conn.c       1970-01-01 01:00:00.000000000 +0100
17448 +++ linux-2.4.18-mh15/net/bluetooth/hci_conn.c  2004-08-01 16:26:23.000000000 +0200
17449 @@ -0,0 +1,435 @@
17450 +/* 
17451 +   BlueZ - Bluetooth protocol stack for Linux
17452 +   Copyright (C) 2000-2001 Qualcomm Incorporated
17453 +
17454 +   Written 2000,2001 by Maxim Krasnyansky <maxk@qualcomm.com>
17455 +
17456 +   This program is free software; you can redistribute it and/or modify
17457 +   it under the terms of the GNU General Public License version 2 as
17458 +   published by the Free Software Foundation;
17459 +
17460 +   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17461 +   OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17462 +   FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS.
17463 +   IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) AND AUTHOR(S) BE LIABLE FOR ANY
17464 +   CLAIM, OR ANY SPECIAL INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES 
17465 +   WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 
17466 +   ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 
17467 +   OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17468 +
17469 +   ALL LIABILITY, INCLUDING LIABILITY FOR INFRINGEMENT OF ANY PATENTS, 
17470 +   COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS, RELATING TO USE OF THIS 
17471 +   SOFTWARE IS DISCLAIMED.
17472 +*/
17473 +
17474 +/*
17475 + * HCI Connection handling.
17476 + *
17477 + * $Id: hci_conn.c,v 1.5 2002/07/17 18:46:25 maxk Exp $
17478 + */
17479 +
17480 +#include <linux/config.h>
17481 +#include <linux/module.h>
17482 +
17483 +#include <linux/types.h>
17484 +#include <linux/errno.h>
17485 +#include <linux/kernel.h>
17486 +#include <linux/major.h>
17487 +#include <linux/sched.h>
17488 +#include <linux/slab.h>
17489 +#include <linux/poll.h>
17490 +#include <linux/fcntl.h>
17491 +#include <linux/init.h>
17492 +#include <linux/skbuff.h>
17493 +#include <linux/interrupt.h>
17494 +#include <linux/notifier.h>
17495 +#include <net/sock.h>
17496 +
17497 +#include <asm/system.h>
17498 +#include <asm/uaccess.h>
17499 +#include <asm/unaligned.h>
17500 +
17501 +#include <net/bluetooth/bluetooth.h>
17502 +#include <net/bluetooth/hci_core.h>
17503 +
17504 +#ifndef HCI_CORE_DEBUG
17505 +#undef  BT_DBG
17506 +#define BT_DBG( A... )
17507 +#endif
17508 +
17509 +void hci_acl_connect(struct hci_conn *conn)
17510 +{
17511 +       struct hci_dev *hdev = conn->hdev;
17512 +       struct inquiry_entry *ie;
17513 +       create_conn_cp cp;
17514 +
17515 +       BT_DBG("%p", conn);
17516 +
17517 +       conn->state = BT_CONNECT;
17518 +       conn->out   = 1;
17519 +       conn->link_mode = HCI_LM_MASTER;
17520 +
17521 +       memset(&cp, 0, sizeof(cp));
17522 +       bacpy(&cp.bdaddr, &conn->dst);
17523 +       cp.pscan_rep_mode = 0x02;
17524 +
17525 +       if ((ie = inquiry_cache_lookup(hdev, &conn->dst)) &&
17526 +                       inquiry_entry_age(ie) <= INQUIRY_ENTRY_AGE_MAX) {
17527 +               cp.pscan_rep_mode = ie->info.pscan_rep_mode;
17528 +               cp.pscan_mode     = ie->info.pscan_mode;
17529 +               cp.clock_offset   = ie->info.clock_offset | __cpu_to_le16(0x8000);
17530 +       }
17531 +
17532 +       cp.pkt_type = __cpu_to_le16(hdev->pkt_type & ACL_PTYPE_MASK);
17533 +       if (lmp_rswitch_capable(hdev) && !(hdev->link_mode & HCI_LM_MASTER))
17534 +               cp.role_switch  = 0x01;
17535 +       else
17536 +               cp.role_switch  = 0x00;
17537 +               
17538 +       hci_send_cmd(hdev, OGF_LINK_CTL, OCF_CREATE_CONN,
17539 +                               CREATE_CONN_CP_SIZE, &cp);
17540 +}
17541 +
17542 +void hci_acl_disconn(struct hci_conn *conn, __u8 reason)
17543 +{
17544 +       disconnect_cp cp;
17545 +
17546 +       BT_DBG("%p", conn);
17547 +
17548 +       conn->state = BT_DISCONN;
17549 +
17550 +       cp.handle = __cpu_to_le16(conn->handle);
17551 +       cp.reason = reason;
17552 +       hci_send_cmd(conn->hdev, OGF_LINK_CTL, OCF_DISCONNECT,
17553 +                               DISCONNECT_CP_SIZE, &cp);
17554 +}
17555 +
17556 +void hci_add_sco(struct hci_conn *conn, __u16 handle)
17557 +{
17558 +       struct hci_dev *hdev = conn->hdev;
17559 +       add_sco_cp cp;
17560 +
17561 +       BT_DBG("%p", conn);
17562 +
17563 +       conn->state = BT_CONNECT;
17564 +       conn->out = 1;
17565 +
17566 +       cp.pkt_type = __cpu_to_le16(hdev->pkt_type & SCO_PTYPE_MASK);
17567 +       cp.handle   = __cpu_to_le16(handle);
17568 +
17569 +       hci_send_cmd(hdev, OGF_LINK_CTL, OCF_ADD_SCO, ADD_SCO_CP_SIZE, &cp);
17570 +}
17571 +
17572 +static void hci_conn_timeout(unsigned long arg)
17573 +{
17574 +       struct hci_conn *conn = (void *)arg;
17575 +       struct hci_dev  *hdev = conn->hdev;
17576 +
17577 +       BT_DBG("conn %p state %d", conn, conn->state);
17578 +
17579 +       if (atomic_read(&conn->refcnt))
17580 +               return;
17581 +
17582 +       hci_dev_lock(hdev);
17583 +       if (conn->state == BT_CONNECTED)
17584 +               hci_acl_disconn(conn, 0x13);
17585 +       else
17586 +               conn->state = BT_CLOSED;
17587 +       hci_dev_unlock(hdev);
17588 +       return;
17589 +}
17590 +
17591 +static void hci_conn_init_timer(struct hci_conn *conn)
17592 +{
17593 +       init_timer(&conn->timer);
17594 +       conn->timer.function = hci_conn_timeout;
17595 +       conn->timer.data = (unsigned long)conn;
17596 +}
17597 +
17598 +struct hci_conn *hci_conn_add(struct hci_dev *hdev, int type, bdaddr_t *dst)
17599 +{
17600 +       struct hci_conn *conn;
17601 +
17602 +       BT_DBG("%s dst %s", hdev->name, batostr(dst));
17603 +
17604 +       if (!(conn = kmalloc(sizeof(struct hci_conn), GFP_ATOMIC)))
17605 +               return NULL;
17606 +       memset(conn, 0, sizeof(struct hci_conn));
17607 +
17608 +       bacpy(&conn->dst, dst);
17609 +       conn->type   = type;
17610 +       conn->hdev   = hdev;
17611 +       conn->state  = BT_OPEN;
17612 +
17613 +       skb_queue_head_init(&conn->data_q);
17614 +       hci_conn_init_timer(conn);
17615 +
17616 +       atomic_set(&conn->refcnt, 0);
17617 +
17618 +       hci_dev_hold(hdev);
17619 +
17620 +       tasklet_disable(&hdev->tx_task);
17621 +       conn_hash_add(hdev, conn);
17622 +       tasklet_enable(&hdev->tx_task);
17623 +
17624 +       return conn;
17625 +}
17626 +
17627 +int hci_conn_del(struct hci_conn *conn)
17628 +{
17629 +       struct hci_dev  *hdev = conn->hdev;
17630 +
17631 +       BT_DBG("%s conn %p handle %d", hdev->name, conn, conn->handle);
17632 +       
17633 +       hci_conn_del_timer(conn);
17634 +
17635 +       if (conn->type == SCO_LINK) {
17636 +               struct hci_conn *acl = conn->link;
17637 +               if (acl) {
17638 +                       acl->link = NULL;
17639 +                       hci_conn_put(acl);
17640 +               }
17641 +       } else {
17642 +               struct hci_conn *sco = conn->link;
17643 +               if (sco)
17644 +                       sco->link = NULL;
17645 +
17646 +               /* Unacked frames */
17647 +               hdev->acl_cnt += conn->sent;
17648 +       }
17649 +
17650 +       tasklet_disable(&hdev->tx_task);
17651 +       conn_hash_del(hdev, conn);
17652 +       tasklet_enable(&hdev->tx_task);
17653 +
17654 +       skb_queue_purge(&conn->data_q);
17655 +
17656 +       hci_dev_put(hdev);
17657 +
17658 +       kfree(conn);
17659 +       return 0;
17660 +}
17661 +
17662 +struct hci_dev *hci_get_route(bdaddr_t *dst, bdaddr_t *src)
17663 +{
17664 +       int use_src = bacmp(src, BDADDR_ANY);
17665 +       struct hci_dev *hdev = NULL;
17666 +       struct list_head *p;
17667 +
17668 +       BT_DBG("%s -> %s", batostr(src), batostr(dst));
17669 +
17670 +       read_lock_bh(&hdev_list_lock);
17671 +
17672 +       list_for_each(p, &hdev_list) {
17673 +               struct hci_dev *d;
17674 +               d = list_entry(p, struct hci_dev, list);
17675 +               
17676 +               if (!test_bit(HCI_UP, &d->flags))
17677 +                       continue;
17678 +
17679 +               /* Simple routing: 
17680 +                *      No source address - find interface with bdaddr != dst 
17681 +                *      Source address    - find interface with bdaddr == src 
17682 +                */
17683 +
17684 +               if (use_src) {
17685 +                       if (!bacmp(&d->bdaddr, src)) {
17686 +                               hdev = d; break;
17687 +                       }
17688 +               } else {
17689 +                       if (bacmp(&d->bdaddr, dst)) {
17690 +                               hdev = d; break;
17691 +                       }
17692 +               }
17693 +       }
17694 +
17695 +       if (hdev)
17696 +               hci_dev_hold(hdev);
17697 +
17698 +       read_unlock_bh(&hdev_list_lock);
17699 +       return hdev;
17700 +}
17701 +
17702 +/* Create SCO or ACL connection.
17703 + * Device _must_ be locked */
17704 +struct hci_conn * hci_connect(struct hci_dev *hdev, int type, bdaddr_t *dst)
17705 +{
17706 +       struct hci_conn *acl;
17707 +
17708 +       BT_DBG("%s dst %s", hdev->name, batostr(dst));
17709 +
17710 +       if (!(acl = conn_hash_lookup_ba(hdev, ACL_LINK, dst))) {
17711 +               if (!(acl = hci_conn_add(hdev, ACL_LINK, dst)))
17712 +                       return NULL;
17713 +       }
17714 +
17715 +       hci_conn_hold(acl);
17716 +
17717 +       if (acl->state == BT_OPEN || acl->state == BT_CLOSED)
17718 +               hci_acl_connect(acl);
17719 +
17720 +       if (type == SCO_LINK) {
17721 +               struct hci_conn *sco;
17722 +
17723 +               if (!(sco = conn_hash_lookup_ba(hdev, SCO_LINK, dst))) {
17724 +                       if (!(sco = hci_conn_add(hdev, SCO_LINK, dst))) {
17725 +                               hci_conn_put(acl);
17726 +                               return NULL;
17727 +                       }
17728 +               }
17729 +               acl->link = sco;
17730 +               sco->link = acl;
17731 +
17732 +               hci_conn_hold(sco);
17733 +
17734 +               if (acl->state == BT_CONNECTED && 
17735 +                               (sco->state == BT_OPEN || sco->state == BT_CLOSED))
17736 +                       hci_add_sco(sco, acl->handle);
17737 +
17738 +               return sco;
17739 +       } else {
17740 +               return acl;
17741 +       }
17742 +}
17743 +
17744 +/* Authenticate remote device */
17745 +int hci_conn_auth(struct hci_conn *conn)
17746 +{
17747 +       BT_DBG("conn %p", conn);
17748 +       
17749 +       if (conn->link_mode & HCI_LM_AUTH)
17750 +               return 1;
17751 +       
17752 +       if (!test_and_set_bit(HCI_CONN_AUTH_PEND, &conn->pend)) {
17753 +               auth_requested_cp ar;
17754 +               ar.handle = __cpu_to_le16(conn->handle);
17755 +               hci_send_cmd(conn->hdev, OGF_LINK_CTL, OCF_AUTH_REQUESTED,
17756 +                               AUTH_REQUESTED_CP_SIZE, &ar);
17757 +       }
17758 +       return 0;
17759 +}
17760 +
17761 +/* Enable encryption */
17762 +int hci_conn_encrypt(struct hci_conn *conn)
17763 +{
17764 +       BT_DBG("conn %p", conn);
17765 +       
17766 +       if (conn->link_mode & HCI_LM_ENCRYPT)
17767 +               return 1;
17768 +       
17769 +       if (test_and_set_bit(HCI_CONN_ENCRYPT_PEND, &conn->pend))
17770 +               return 0;
17771 +
17772 +       if (hci_conn_auth(conn)) {
17773 +               set_conn_encrypt_cp ce;
17774 +               ce.handle  = __cpu_to_le16(conn->handle);
17775 +               ce.encrypt = 1; 
17776 +               hci_send_cmd(conn->hdev, OGF_LINK_CTL, OCF_SET_CONN_ENCRYPT,
17777 +                               SET_CONN_ENCRYPT_CP_SIZE, &ce);
17778 +       }
17779 +       return 0;
17780 +}
17781 +
17782 +/* Drop all connection on the device */
17783 +void hci_conn_hash_flush(struct hci_dev *hdev)
17784 +{
17785 +       struct conn_hash *h = &hdev->conn_hash;
17786 +        struct list_head *p;
17787 +
17788 +       BT_DBG("hdev %s", hdev->name);
17789 +
17790 +       p = h->list.next;
17791 +       while (p != &h->list) {
17792 +               struct hci_conn *c;
17793 +
17794 +               c = list_entry(p, struct hci_conn, list);
17795 +               p = p->next;
17796 +
17797 +               c->state = BT_CLOSED;
17798 +
17799 +               hci_proto_disconn_ind(c, 0x16);
17800 +               hci_conn_del(c);
17801 +       }
17802 +}
17803 +
17804 +int hci_get_conn_list(unsigned long arg)
17805 +{
17806 +       struct hci_conn_list_req req, *cl;
17807 +       struct hci_conn_info *ci;
17808 +       struct hci_dev *hdev;
17809 +       struct list_head *p;
17810 +       int n = 0, size, err;
17811 +
17812 +       if (copy_from_user(&req, (void *) arg, sizeof(req)))
17813 +               return -EFAULT;
17814 +
17815 +       if (!req.conn_num || req.conn_num > (PAGE_SIZE * 2) / sizeof(*ci))
17816 +               return -EINVAL;
17817 +
17818 +       size = sizeof(req) + req.conn_num * sizeof(*ci);
17819 +
17820 +       if (!(cl = (void *) kmalloc(size, GFP_KERNEL)))
17821 +               return -ENOMEM;
17822 +
17823 +       if (!(hdev = hci_dev_get(req.dev_id))) {
17824 +               kfree(cl);
17825 +               return -ENODEV;
17826 +       }
17827 +
17828 +       ci = cl->conn_info;
17829 +
17830 +       hci_dev_lock_bh(hdev);
17831 +       list_for_each(p, &hdev->conn_hash.list) {
17832 +               register struct hci_conn *c;
17833 +               c = list_entry(p, struct hci_conn, list);
17834 +
17835 +               bacpy(&(ci + n)->bdaddr, &c->dst);
17836 +               (ci + n)->handle = c->handle;
17837 +               (ci + n)->type  = c->type;
17838 +               (ci + n)->out   = c->out;
17839 +               (ci + n)->state = c->state;
17840 +               (ci + n)->link_mode = c->link_mode;
17841 +               if (++n >= req.conn_num)
17842 +                       break;
17843 +       }
17844 +       hci_dev_unlock_bh(hdev);
17845 +
17846 +       cl->dev_id = hdev->id;
17847 +       cl->conn_num = n;
17848 +       size = sizeof(req) + n * sizeof(*ci);
17849 +
17850 +       hci_dev_put(hdev);
17851 +
17852 +       err = copy_to_user((void *) arg, cl, size);
17853 +       kfree(cl);
17854 +
17855 +       return err ? -EFAULT : 0;
17856 +}
17857 +
17858 +int hci_get_conn_info(struct hci_dev *hdev, unsigned long arg)
17859 +{
17860 +       struct hci_conn_info_req req;
17861 +       struct hci_conn_info ci;
17862 +       struct hci_conn *conn;
17863 +       char *ptr = (void *) arg + sizeof(req);
17864 +
17865 +       if (copy_from_user(&req, (void *) arg, sizeof(req)))
17866 +               return -EFAULT;
17867 +
17868 +       hci_dev_lock_bh(hdev);
17869 +       conn = conn_hash_lookup_ba(hdev, req.type, &req.bdaddr);
17870 +       if (conn) {
17871 +               bacpy(&ci.bdaddr, &conn->dst);
17872 +               ci.handle = conn->handle;
17873 +               ci.type  = conn->type;
17874 +               ci.out   = conn->out;
17875 +               ci.state = conn->state;
17876 +               ci.link_mode = conn->link_mode;
17877 +       }
17878 +       hci_dev_unlock_bh(hdev);
17879 +
17880 +       if (!conn)
17881 +               return -ENOENT;
17882 +
17883 +       return copy_to_user(ptr, &ci, sizeof(ci)) ? -EFAULT : 0;
17884 +}
17885 diff -urN linux-2.4.18/net/bluetooth/hci_core.c linux-2.4.18-mh15/net/bluetooth/hci_core.c
17886 --- linux-2.4.18/net/bluetooth/hci_core.c       2001-11-09 23:21:21.000000000 +0100
17887 +++ linux-2.4.18-mh15/net/bluetooth/hci_core.c  2004-08-01 16:26:23.000000000 +0200
17888 @@ -25,11 +25,12 @@
17889  /*
17890   * BlueZ HCI Core.
17891   *
17892 - * $Id: hci_core.c,v 1.22 2001/08/03 04:19:50 maxk Exp $
17893 + * $Id: hci_core.c,v 1.14 2002/08/26 16:57:57 maxk Exp $
17894   */
17895  
17896  #include <linux/config.h>
17897  #include <linux/module.h>
17898 +#include <linux/kmod.h>
17899  
17900  #include <linux/types.h>
17901  #include <linux/errno.h>
17902 @@ -50,12 +51,11 @@
17903  #include <asm/unaligned.h>
17904  
17905  #include <net/bluetooth/bluetooth.h>
17906 -#include <net/bluetooth/bluez.h>
17907  #include <net/bluetooth/hci_core.h>
17908  
17909  #ifndef HCI_CORE_DEBUG
17910 -#undef  DBG
17911 -#define DBG( A... )
17912 +#undef  BT_DBG
17913 +#define BT_DBG( A... )
17914  #endif
17915  
17916  static void hci_cmd_task(unsigned long arg);
17917 @@ -63,279 +63,69 @@
17918  static void hci_tx_task(unsigned long arg);
17919  static void hci_notify(struct hci_dev *hdev, int event);
17920  
17921 -static rwlock_t hci_task_lock = RW_LOCK_UNLOCKED;
17922 +rwlock_t hci_task_lock = RW_LOCK_UNLOCKED;
17923  
17924  /* HCI device list */
17925 -struct hci_dev *hdev_list[HCI_MAX_DEV];
17926 -spinlock_t hdev_list_lock;
17927 -#define GET_HDEV(a) (hdev_list[a])
17928 -
17929 -/* HCI protocol list */
17930 -struct hci_proto *hproto_list[HCI_MAX_PROTO];
17931 -#define GET_HPROTO(a) (hproto_list[a])
17932 +LIST_HEAD(hdev_list);
17933 +rwlock_t hdev_list_lock = RW_LOCK_UNLOCKED;
17934  
17935 -/* HCI notifiers list */
17936 -struct notifier_block *hci_dev_notifier;
17937 -
17938 -/* HCI device notifications */
17939 -int hci_register_notifier(struct notifier_block *nb)
17940 -{
17941 -       int err, i;
17942 -       struct hci_dev *hdev;
17943 -
17944 -       if ((err = notifier_chain_register(&hci_dev_notifier, nb)))
17945 -               return err;
17946 -
17947 -       /* Notify about already registered devices */
17948 -       spin_lock(&hdev_list_lock);
17949 -       for (i = 0; i < HCI_MAX_DEV; i++) {
17950 -               if (!(hdev = GET_HDEV(i)))
17951 -                       continue;
17952 -               if (hdev->flags & HCI_UP)
17953 -                       (*nb->notifier_call)(nb, HCI_DEV_UP, hdev);
17954 -       }
17955 -       spin_unlock(&hdev_list_lock);
17956 -
17957 -       return 0;
17958 -}
17959 -
17960 -int hci_unregister_notifier(struct notifier_block *nb)
17961 -{
17962 -       return notifier_chain_unregister(&hci_dev_notifier, nb);
17963 -}
17964 -
17965 -static inline void hci_notify(struct hci_dev *hdev, int event)
17966 -{
17967 -       notifier_call_chain(&hci_dev_notifier, event, hdev);
17968 -}
17969 -
17970 -/* Get HCI device by index (device is locked on return)*/
17971 -struct hci_dev *hci_dev_get(int index)
17972 -{
17973 -       struct hci_dev *hdev;
17974 -       DBG("%d", index);
17975 -
17976 -       if (index < 0 || index >= HCI_MAX_DEV)
17977 -               return NULL;
17978 -
17979 -       spin_lock(&hdev_list_lock);
17980 -       if ((hdev = GET_HDEV(index)))
17981 -               hci_dev_hold(hdev);
17982 -       spin_unlock(&hdev_list_lock);
17983 -
17984 -       return hdev;
17985 -}
17986 -
17987 -/* Flush inquiry cache */
17988 -void inquiry_cache_flush(struct inquiry_cache *cache)
17989 -{
17990 -       struct inquiry_entry *next = cache->list, *e;
17991 -
17992 -       DBG("cache %p", cache);
17993 -
17994 -       cache->list = NULL;
17995 -       while ((e = next)) {
17996 -               next = e->next;
17997 -               kfree(e);
17998 -       }
17999 -}
18000 -
18001 -/* Lookup by bdaddr.
18002 - * Cache must be locked. */
18003 -static struct inquiry_entry * __inquiry_cache_lookup(struct inquiry_cache *cache, bdaddr_t *bdaddr)
18004 -{
18005 -       struct inquiry_entry *e;
18006 -
18007 -       DBG("cache %p, %s", cache, batostr(bdaddr));
18008 -
18009 -       for (e = cache->list; e; e = e->next)
18010 -               if (!bacmp(&e->info.bdaddr, bdaddr))
18011 -                       break;
18012 -
18013 -       return e;
18014 -}
18015 -
18016 -static void inquiry_cache_update(struct inquiry_cache *cache, inquiry_info *info)
18017 -{
18018 -       struct inquiry_entry *e;
18019 -
18020 -       DBG("cache %p, %s", cache, batostr(&info->bdaddr));
18021 -
18022 -       inquiry_cache_lock(cache);
18023 -
18024 -       if (!(e = __inquiry_cache_lookup(cache, &info->bdaddr))) {
18025 -               /* Entry not in the cache. Add new one. */
18026 -               if (!(e = kmalloc(sizeof(struct inquiry_entry), GFP_ATOMIC)))
18027 -                       goto unlock;
18028 -               memset(e, 0, sizeof(struct inquiry_entry));
18029 -               e->next     = cache->list;
18030 -               cache->list = e;
18031 -       }
18032 -
18033 -       memcpy(&e->info, info, sizeof(inquiry_info));
18034 -       e->timestamp = jiffies;
18035 -       cache->timestamp = jiffies;
18036 -unlock:
18037 -       inquiry_cache_unlock(cache);
18038 -}
18039 -
18040 -static int inquiry_cache_dump(struct inquiry_cache *cache, int num, __u8 *buf)
18041 -{
18042 -       inquiry_info *info = (inquiry_info *) buf;
18043 -       struct inquiry_entry *e;
18044 -       int copied = 0;
18045 +/* HCI protocols */
18046 +#define HCI_MAX_PROTO  2
18047 +struct hci_proto *hci_proto[HCI_MAX_PROTO];
18048  
18049 -       inquiry_cache_lock(cache);
18050 -
18051 -       for (e = cache->list; e && copied < num; e = e->next, copied++)
18052 -               memcpy(info++, &e->info, sizeof(inquiry_info));
18053 +/* HCI notifiers list */
18054 +static struct notifier_block *hci_notifier;
18055  
18056 -       inquiry_cache_unlock(cache);
18057  
18058 -       DBG("cache %p, copied %d", cache, copied);
18059 -       return copied;
18060 -}
18061 +/* ---- HCI notifications ---- */
18062  
18063 -/* --------- BaseBand connections --------- */
18064 -static struct hci_conn *hci_conn_add(struct hci_dev *hdev, __u16 handle, __u8 type, bdaddr_t *dst)
18065 +int hci_register_notifier(struct notifier_block *nb)
18066  {
18067 -       struct hci_conn *conn;
18068 -
18069 -       DBG("%s handle %d dst %s", hdev->name, handle, batostr(dst));
18070 -
18071 -       if ( conn_hash_lookup(&hdev->conn_hash, handle)) {
18072 -               ERR("%s handle 0x%x already exists", hdev->name, handle);
18073 -               return NULL;
18074 -       }
18075 -
18076 -       if (!(conn = kmalloc(sizeof(struct hci_conn), GFP_ATOMIC)))
18077 -               return NULL;
18078 -       memset(conn, 0, sizeof(struct hci_conn));
18079 -
18080 -       bacpy(&conn->dst, dst);
18081 -       conn->handle = handle;
18082 -       conn->type   = type;
18083 -       conn->hdev   = hdev;
18084 -
18085 -       skb_queue_head_init(&conn->data_q);
18086 -
18087 -       hci_dev_hold(hdev);
18088 -       conn_hash_add(&hdev->conn_hash, handle, conn);
18089 -
18090 -       return conn;
18091 +       return notifier_chain_register(&hci_notifier, nb);
18092  }
18093  
18094 -static int hci_conn_del(struct hci_dev *hdev, struct hci_conn *conn)
18095 +int hci_unregister_notifier(struct notifier_block *nb)
18096  {
18097 -       DBG("%s conn %p handle %d", hdev->name, conn, conn->handle);
18098 -
18099 -       conn_hash_del(&hdev->conn_hash, conn);
18100 -       hci_dev_put(hdev);
18101 -
18102 -       /* Unacked frames */
18103 -       hdev->acl_cnt += conn->sent;
18104 -
18105 -       skb_queue_purge(&conn->data_q);
18106 -
18107 -       kfree(conn);
18108 -       return 0;
18109 +       return notifier_chain_unregister(&hci_notifier, nb);
18110  }
18111  
18112 -/* Drop all connection on the device */
18113 -static void hci_conn_hash_flush(struct hci_dev *hdev)
18114 +void hci_notify(struct hci_dev *hdev, int event)
18115  {
18116 -       struct conn_hash *h = &hdev->conn_hash;
18117 -       struct hci_proto *hp;
18118 -        struct list_head *p;
18119 -
18120 -       DBG("hdev %s", hdev->name);
18121 -
18122 -       p = h->list.next;
18123 -       while (p != &h->list) {
18124 -               struct hci_conn *c;
18125 -
18126 -               c = list_entry(p, struct hci_conn, list);
18127 -               p = p->next;
18128 -
18129 -               if (c->type == ACL_LINK) {
18130 -                       /* ACL link notify L2CAP layer */
18131 -                       if ((hp = GET_HPROTO(HCI_PROTO_L2CAP)) && hp->disconn_ind)
18132 -                               hp->disconn_ind(c, 0x16);
18133 -               } else {
18134 -                       /* SCO link (no notification) */
18135 -               }
18136 -
18137 -               hci_conn_del(hdev, c);
18138 -       }
18139 +       notifier_call_chain(&hci_notifier, event, hdev);
18140  }
18141  
18142 -int hci_connect(struct hci_dev *hdev, bdaddr_t *bdaddr)
18143 -{
18144 -       struct inquiry_cache *cache = &hdev->inq_cache;
18145 -       struct inquiry_entry *e;
18146 -       create_conn_cp cc;
18147 -       __u16 clock_offset;
18148 -
18149 -       DBG("%s bdaddr %s", hdev->name, batostr(bdaddr));
18150 -
18151 -       if (!(hdev->flags & HCI_UP))
18152 -               return -ENODEV;
18153 -
18154 -       inquiry_cache_lock_bh(cache);
18155 -
18156 -       if (!(e = __inquiry_cache_lookup(cache, bdaddr)) || inquiry_entry_age(e) > INQUIRY_ENTRY_AGE_MAX) {
18157 -               cc.pscan_rep_mode = 0;
18158 -               cc.pscan_mode     = 0;
18159 -               clock_offset      = 0;
18160 -       } else {
18161 -               cc.pscan_rep_mode = e->info.pscan_rep_mode;
18162 -               cc.pscan_mode     = e->info.pscan_mode;
18163 -               clock_offset      = __le16_to_cpu(e->info.clock_offset) & 0x8000;
18164 -       }
18165 -
18166 -       inquiry_cache_unlock_bh(cache);
18167 -
18168 -       bacpy(&cc.bdaddr, bdaddr);
18169 -       cc.pkt_type     = __cpu_to_le16(hdev->pkt_type);
18170 -       cc.clock_offset = __cpu_to_le16(clock_offset);
18171 -
18172 -       if (lmp_rswitch_capable(hdev))
18173 -               cc.role_switch  = 0x01;
18174 -       else
18175 -               cc.role_switch  = 0x00;
18176 -               
18177 -       hci_send_cmd(hdev, OGF_LINK_CTL, OCF_CREATE_CONN, CREATE_CONN_CP_SIZE, &cc);
18178 +/* ---- HCI hotplug support ---- */
18179  
18180 -       return 0;
18181 -}
18182 +#ifdef CONFIG_HOTPLUG
18183  
18184 -int hci_disconnect(struct hci_conn *conn, __u8 reason)
18185 +static int hci_run_hotplug(char *dev, char *action)
18186  {
18187 -       disconnect_cp dc;
18188 -
18189 -       DBG("conn %p handle %d", conn, conn->handle);
18190 +       char *argv[3], *envp[5], dstr[20], astr[32];
18191  
18192 -       dc.handle = __cpu_to_le16(conn->handle);
18193 -       dc.reason = reason;
18194 -       hci_send_cmd(conn->hdev, OGF_LINK_CTL, OCF_DISCONNECT, DISCONNECT_CP_SIZE, &dc);
18195 +       sprintf(dstr, "DEVICE=%s", dev);
18196 +       sprintf(astr, "ACTION=%s", action);
18197  
18198 -       return 0;
18199 -}
18200 +        argv[0] = hotplug_path;
18201 +        argv[1] = "bluetooth";
18202 +        argv[2] = NULL;
18203  
18204 -/* --------- HCI request handling ------------ */
18205 -static inline void hci_req_lock(struct hci_dev *hdev)
18206 -{
18207 -       down(&hdev->req_lock);
18208 +       envp[0] = "HOME=/";
18209 +       envp[1] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin";
18210 +       envp[2] = dstr;
18211 +       envp[3] = astr;
18212 +       envp[4] = NULL;
18213 +       
18214 +       return call_usermodehelper(argv[0], argv, envp);
18215  }
18216 +#else
18217 +#define hci_run_hotplug(A...)
18218 +#endif
18219  
18220 -static inline void hci_req_unlock(struct hci_dev *hdev)
18221 -{
18222 -       up(&hdev->req_lock);
18223 -}
18224 +/* ---- HCI requests ---- */
18225  
18226 -static inline void hci_req_complete(struct hci_dev *hdev, int result)
18227 +void hci_req_complete(struct hci_dev *hdev, int result)
18228  {
18229 -       DBG("%s result 0x%2.2x", hdev->name, result);
18230 +       BT_DBG("%s result 0x%2.2x", hdev->name, result);
18231  
18232         if (hdev->req_status == HCI_REQ_PEND) {
18233                 hdev->req_result = result;
18234 @@ -344,9 +134,9 @@
18235         }
18236  }
18237  
18238 -static inline void hci_req_cancel(struct hci_dev *hdev, int err)
18239 +void hci_req_cancel(struct hci_dev *hdev, int err)
18240  {
18241 -       DBG("%s err 0x%2.2x", hdev->name, err);
18242 +       BT_DBG("%s err 0x%2.2x", hdev->name, err);
18243  
18244         if (hdev->req_status == HCI_REQ_PEND) {
18245                 hdev->req_result = err;
18246 @@ -356,23 +146,22 @@
18247  }
18248  
18249  /* Execute request and wait for completion. */
18250 -static int __hci_request(struct hci_dev *hdev, void (*req)(struct hci_dev *hdev, unsigned long opt),
18251 -                         unsigned long opt, __u32 timeout)
18252 +static int __hci_request(struct hci_dev *hdev, void (*req)(struct hci_dev *hdev, unsigned long opt), unsigned long opt, __u32 timeout)
18253  {
18254         DECLARE_WAITQUEUE(wait, current);
18255         int err = 0;
18256  
18257 -       DBG("%s start", hdev->name);
18258 +       BT_DBG("%s start", hdev->name);
18259  
18260         hdev->req_status = HCI_REQ_PEND;
18261  
18262         add_wait_queue(&hdev->req_wait_q, &wait);
18263 -       current->state = TASK_INTERRUPTIBLE;
18264 +       set_current_state(TASK_INTERRUPTIBLE);
18265  
18266         req(hdev, opt);
18267         schedule_timeout(timeout);
18268  
18269 -       current->state = TASK_RUNNING;
18270 +       set_current_state(TASK_RUNNING);
18271         remove_wait_queue(&hdev->req_wait_q, &wait);
18272  
18273         if (signal_pending(current))
18274 @@ -394,7 +183,7 @@
18275  
18276         hdev->req_status = hdev->req_result = 0;
18277  
18278 -       DBG("%s end: err %d", hdev->name, err);
18279 +       BT_DBG("%s end: err %d", hdev->name, err);
18280  
18281         return err;
18282  }
18283 @@ -412,10 +201,9 @@
18284         return ret;
18285  }
18286  
18287 -/* --------- HCI requests ---------- */
18288  static void hci_reset_req(struct hci_dev *hdev, unsigned long opt)
18289  {
18290 -       DBG("%s %ld", hdev->name, opt);
18291 +       BT_DBG("%s %ld", hdev->name, opt);
18292  
18293         /* Reset device */
18294         hci_send_cmd(hdev, OGF_HOST_CTL, OCF_RESET, 0, NULL);
18295 @@ -423,27 +211,44 @@
18296  
18297  static void hci_init_req(struct hci_dev *hdev, unsigned long opt)
18298  {
18299 -       set_event_flt_cp ec;
18300 +       set_event_flt_cp ef;
18301         __u16 param;
18302  
18303 -       DBG("%s %ld", hdev->name, opt);
18304 +       BT_DBG("%s %ld", hdev->name, opt);
18305  
18306         /* Mandatory initialization */
18307  
18308 +       /* Reset */
18309 +       if (test_bit(HCI_QUIRK_RESET_ON_INIT, &hdev->quirks))
18310 +               hci_send_cmd(hdev, OGF_HOST_CTL, OCF_RESET, 0, NULL);
18311 +
18312         /* Read Local Supported Features */
18313         hci_send_cmd(hdev, OGF_INFO_PARAM, OCF_READ_LOCAL_FEATURES, 0, NULL);
18314  
18315         /* Read Buffer Size (ACL mtu, max pkt, etc.) */
18316         hci_send_cmd(hdev, OGF_INFO_PARAM, OCF_READ_BUFFER_SIZE, 0, NULL);
18317  
18318 +#if 0
18319 +       /* Host buffer size */
18320 +       {
18321 +               host_buffer_size_cp bs;
18322 +               bs.acl_mtu = __cpu_to_le16(HCI_MAX_ACL_SIZE);
18323 +               bs.sco_mtu = HCI_MAX_SCO_SIZE;
18324 +               bs.acl_max_pkt = __cpu_to_le16(0xffff);
18325 +               bs.sco_max_pkt = __cpu_to_le16(0xffff);
18326 +               hci_send_cmd(hdev, OGF_HOST_CTL, OCF_HOST_BUFFER_SIZE,
18327 +                               HOST_BUFFER_SIZE_CP_SIZE, &bs);
18328 +       }
18329 +#endif
18330 +
18331         /* Read BD Address */
18332         hci_send_cmd(hdev, OGF_INFO_PARAM, OCF_READ_BD_ADDR, 0, NULL);
18333  
18334         /* Optional initialization */
18335  
18336         /* Clear Event Filters */
18337 -       ec.flt_type  = FLT_CLEAR_ALL;
18338 -       hci_send_cmd(hdev, OGF_HOST_CTL, OCF_SET_EVENT_FLT, 1, &ec);
18339 +       ef.flt_type  = FLT_CLEAR_ALL;
18340 +       hci_send_cmd(hdev, OGF_HOST_CTL, OCF_SET_EVENT_FLT, 1, &ef);
18341  
18342         /* Page timeout ~20 secs */
18343         param = __cpu_to_le16(0x8000);
18344 @@ -458,7 +263,7 @@
18345  {
18346         __u8 scan = opt;
18347  
18348 -       DBG("%s %x", hdev->name, scan);
18349 +       BT_DBG("%s %x", hdev->name, scan);
18350  
18351         /* Inquiry and Page scans */
18352         hci_send_cmd(hdev, OGF_HOST_CTL, OCF_WRITE_SCAN_ENABLE, 1, &scan);
18353 @@ -468,116 +273,273 @@
18354  {
18355         __u8 auth = opt;
18356  
18357 -       DBG("%s %x", hdev->name, auth);
18358 +       BT_DBG("%s %x", hdev->name, auth);
18359  
18360         /* Authentication */
18361         hci_send_cmd(hdev, OGF_HOST_CTL, OCF_WRITE_AUTH_ENABLE, 1, &auth);
18362  }
18363  
18364 -static void hci_inq_req(struct hci_dev *hdev, unsigned long opt)
18365 +static void hci_encrypt_req(struct hci_dev *hdev, unsigned long opt)
18366  {
18367 -       struct hci_inquiry_req *ir = (struct hci_inquiry_req *) opt;
18368 -       inquiry_cp ic;
18369 +       __u8 encrypt = opt;
18370  
18371 -       DBG("%s", hdev->name);
18372 +       BT_DBG("%s %x", hdev->name, encrypt);
18373  
18374 -       /* Start Inquiry */
18375 -       memcpy(&ic.lap, &ir->lap, 3);
18376 -       ic.lenght  = ir->length;
18377 -       ic.num_rsp = ir->num_rsp;
18378 -       hci_send_cmd(hdev, OGF_LINK_CTL, OCF_INQUIRY, INQUIRY_CP_SIZE, &ic);
18379 +       /* Authentication */
18380 +       hci_send_cmd(hdev, OGF_HOST_CTL, OCF_WRITE_ENCRYPT_MODE, 1, &encrypt);
18381  }
18382  
18383 -/* HCI ioctl helpers */
18384 -int hci_dev_open(__u16 dev)
18385 +/* Get HCI device by index. 
18386 + * Device is locked on return. */
18387 +struct hci_dev *hci_dev_get(int index)
18388  {
18389         struct hci_dev *hdev;
18390 -       int ret = 0;
18391 -
18392 -       if (!(hdev = hci_dev_get(dev)))
18393 -               return -ENODEV;
18394 +       struct list_head *p;
18395  
18396 -       DBG("%s %p", hdev->name, hdev);
18397 +       BT_DBG("%d", index);
18398  
18399 -       hci_req_lock(hdev);
18400 +       if (index < 0)
18401 +               return NULL;
18402  
18403 -       if (hdev->flags & HCI_UP) {
18404 -               ret = -EALREADY;
18405 -               goto done;
18406 +       read_lock(&hdev_list_lock);
18407 +       list_for_each(p, &hdev_list) {
18408 +               hdev = list_entry(p, struct hci_dev, list);
18409 +               if (hdev->id == index) {
18410 +                       hci_dev_hold(hdev);
18411 +                       goto done;
18412 +               }
18413         }
18414 +       hdev = NULL;
18415 +done:
18416 +       read_unlock(&hdev_list_lock);
18417 +       return hdev;
18418 +}
18419  
18420 -       if (hdev->open(hdev)) {
18421 -               ret = -EIO;
18422 -               goto done;
18423 -       }
18424 +/* ---- Inquiry support ---- */
18425 +void inquiry_cache_flush(struct hci_dev *hdev)
18426 +{
18427 +       struct inquiry_cache *cache = &hdev->inq_cache;
18428 +       struct inquiry_entry *next  = cache->list, *e;
18429  
18430 -       if (hdev->flags & HCI_NORMAL) {
18431 -               atomic_set(&hdev->cmd_cnt, 1);
18432 -               hdev->flags |= HCI_INIT;
18433 +       BT_DBG("cache %p", cache);
18434  
18435 -               //__hci_request(hdev, hci_reset_req, 0, HZ);
18436 -               ret = __hci_request(hdev, hci_init_req, 0, HCI_INIT_TIMEOUT);
18437 -       
18438 -               hdev->flags &= ~HCI_INIT;
18439 +       cache->list = NULL;
18440 +       while ((e = next)) {
18441 +               next = e->next;
18442 +               kfree(e);
18443         }
18444 +}
18445  
18446 -       if (!ret) {
18447 -               hdev->flags |= HCI_UP;
18448 -               hci_notify(hdev, HCI_DEV_UP);
18449 -       } else {        
18450 -               /* Init failed, cleanup */
18451 -               tasklet_kill(&hdev->rx_task);
18452 -               tasklet_kill(&hdev->tx_task);
18453 -               tasklet_kill(&hdev->cmd_task);
18454 +struct inquiry_entry *inquiry_cache_lookup(struct hci_dev *hdev, bdaddr_t *bdaddr)
18455 +{
18456 +       struct inquiry_cache *cache = &hdev->inq_cache;
18457 +       struct inquiry_entry *e;
18458  
18459 -               skb_queue_purge(&hdev->cmd_q);
18460 -               skb_queue_purge(&hdev->rx_q);
18461 +       BT_DBG("cache %p, %s", cache, batostr(bdaddr));
18462  
18463 -               if (hdev->flush)
18464 -                       hdev->flush(hdev);
18465 +       for (e = cache->list; e; e = e->next)
18466 +               if (!bacmp(&e->info.bdaddr, bdaddr))
18467 +                       break;
18468 +       return e;
18469 +}
18470  
18471 -               if (hdev->sent_cmd) {
18472 -                       kfree_skb(hdev->sent_cmd);
18473 -                       hdev->sent_cmd = NULL;
18474 -               }
18475 +void inquiry_cache_update(struct hci_dev *hdev, inquiry_info *info)
18476 +{
18477 +       struct inquiry_cache *cache = &hdev->inq_cache;
18478 +       struct inquiry_entry *e;
18479  
18480 -               hdev->close(hdev);
18481 -       }
18482 +       BT_DBG("cache %p, %s", cache, batostr(&info->bdaddr));
18483  
18484 -done:
18485 -       hci_req_unlock(hdev);
18486 -       hci_dev_put(hdev);
18487 +       if (!(e = inquiry_cache_lookup(hdev, &info->bdaddr))) {
18488 +               /* Entry not in the cache. Add new one. */
18489 +               if (!(e = kmalloc(sizeof(struct inquiry_entry), GFP_ATOMIC)))
18490 +                       return;
18491 +               memset(e, 0, sizeof(struct inquiry_entry));
18492 +               e->next     = cache->list;
18493 +               cache->list = e;
18494 +       }
18495  
18496 -       return ret;
18497 +       memcpy(&e->info, info, sizeof(inquiry_info));
18498 +       e->timestamp = jiffies;
18499 +       cache->timestamp = jiffies;
18500  }
18501  
18502 -int hci_dev_close(__u16 dev)
18503 +int inquiry_cache_dump(struct hci_dev *hdev, int num, __u8 *buf)
18504  {
18505 -       struct hci_dev *hdev;
18506 -
18507 -       if (!(hdev = hci_dev_get(dev)))
18508 -               return -ENODEV;
18509 +       struct inquiry_cache *cache = &hdev->inq_cache;
18510 +       inquiry_info *info = (inquiry_info *) buf;
18511 +       struct inquiry_entry *e;
18512 +       int copied = 0;
18513  
18514 -       DBG("%s %p", hdev->name, hdev);
18515 +       for (e = cache->list; e && copied < num; e = e->next, copied++)
18516 +               memcpy(info++, &e->info, sizeof(inquiry_info));
18517  
18518 -       hci_req_cancel(hdev, ENODEV);
18519 -       hci_req_lock(hdev);
18520 +       BT_DBG("cache %p, copied %d", cache, copied);
18521 +       return copied;
18522 +}
18523  
18524 -       if (!(hdev->flags & HCI_UP))
18525 -               goto done;
18526 +static void hci_inq_req(struct hci_dev *hdev, unsigned long opt)
18527 +{
18528 +       struct hci_inquiry_req *ir = (struct hci_inquiry_req *) opt;
18529 +       inquiry_cp ic;
18530  
18531 -       /* Kill RX and TX tasks */
18532 -       tasklet_kill(&hdev->rx_task);
18533 -       tasklet_kill(&hdev->tx_task);
18534 +       BT_DBG("%s", hdev->name);
18535  
18536 -       inquiry_cache_flush(&hdev->inq_cache);
18537 +       if (test_bit(HCI_INQUIRY, &hdev->flags))
18538 +               return;
18539  
18540 -       hci_conn_hash_flush(hdev);
18541 +       /* Start Inquiry */
18542 +       memcpy(&ic.lap, &ir->lap, 3);
18543 +       ic.length  = ir->length;
18544 +       ic.num_rsp = ir->num_rsp;
18545 +       hci_send_cmd(hdev, OGF_LINK_CTL, OCF_INQUIRY, INQUIRY_CP_SIZE, &ic);
18546 +}
18547  
18548 -       /* Clear flags */
18549 -       hdev->flags &= HCI_SOCK;
18550 -       hdev->flags |= HCI_NORMAL;
18551 +int hci_inquiry(unsigned long arg)
18552 +{
18553 +       struct hci_inquiry_req ir;
18554 +       struct hci_dev *hdev;
18555 +       int err = 0, do_inquiry = 0, max_rsp;
18556 +       long timeo;
18557 +       __u8 *buf, *ptr;
18558  
18559 +       ptr = (void *) arg;
18560 +       if (copy_from_user(&ir, ptr, sizeof(ir)))
18561 +               return -EFAULT;
18562 +
18563 +       if (!(hdev = hci_dev_get(ir.dev_id)))
18564 +               return -ENODEV;
18565 +
18566 +       hci_dev_lock_bh(hdev);
18567 +       if (inquiry_cache_age(hdev) > INQUIRY_CACHE_AGE_MAX || 
18568 +                                       inquiry_cache_empty(hdev) ||
18569 +                                       ir.flags & IREQ_CACHE_FLUSH) {
18570 +               inquiry_cache_flush(hdev);
18571 +               do_inquiry = 1;
18572 +       }
18573 +       hci_dev_unlock_bh(hdev);
18574 +
18575 +       timeo = ir.length * 2 * HZ;
18576 +       if (do_inquiry && (err = hci_request(hdev, hci_inq_req, (unsigned long)&ir, timeo)) < 0)
18577 +               goto done;
18578 +
18579 +       /* for unlimited number of responses we will use buffer with 255 entries */
18580 +       max_rsp = (ir.num_rsp == 0) ? 255 : ir.num_rsp;
18581 +
18582 +       /* cache_dump can't sleep. Therefore we allocate temp buffer and then
18583 +        * copy it to the user space.
18584 +        */
18585 +       if (!(buf = kmalloc(sizeof(inquiry_info) * max_rsp, GFP_KERNEL))) {
18586 +               err = -ENOMEM;
18587 +               goto done;
18588 +       }
18589 +
18590 +       hci_dev_lock_bh(hdev);
18591 +       ir.num_rsp = inquiry_cache_dump(hdev, max_rsp, buf);
18592 +       hci_dev_unlock_bh(hdev);
18593 +
18594 +       BT_DBG("num_rsp %d", ir.num_rsp);
18595 +
18596 +       if (!verify_area(VERIFY_WRITE, ptr, sizeof(ir) + 
18597 +                               (sizeof(inquiry_info) * ir.num_rsp))) {
18598 +               copy_to_user(ptr, &ir, sizeof(ir));
18599 +               ptr += sizeof(ir);
18600 +               copy_to_user(ptr, buf, sizeof(inquiry_info) * ir.num_rsp);
18601 +       } else 
18602 +               err = -EFAULT;
18603 +
18604 +       kfree(buf);
18605 +
18606 +done:
18607 +       hci_dev_put(hdev);
18608 +       return err;
18609 +}
18610 +
18611 +/* ---- HCI ioctl helpers ---- */
18612 +
18613 +int hci_dev_open(__u16 dev)
18614 +{
18615 +       struct hci_dev *hdev;
18616 +       int ret = 0;
18617 +
18618 +       if (!(hdev = hci_dev_get(dev)))
18619 +               return -ENODEV;
18620 +
18621 +       BT_DBG("%s %p", hdev->name, hdev);
18622 +
18623 +       hci_req_lock(hdev);
18624 +
18625 +       if (test_bit(HCI_UP, &hdev->flags)) {
18626 +               ret = -EALREADY;
18627 +               goto done;
18628 +       }
18629 +
18630 +       if (hdev->open(hdev)) {
18631 +               ret = -EIO;
18632 +               goto done;
18633 +       }
18634 +
18635 +       if (!test_bit(HCI_RAW, &hdev->flags)) {
18636 +               atomic_set(&hdev->cmd_cnt, 1);
18637 +               set_bit(HCI_INIT, &hdev->flags);
18638 +
18639 +               //__hci_request(hdev, hci_reset_req, 0, HZ);
18640 +               ret = __hci_request(hdev, hci_init_req, 0, HCI_INIT_TIMEOUT);
18641 +       
18642 +               clear_bit(HCI_INIT, &hdev->flags);
18643 +       }
18644 +
18645 +       if (!ret) {
18646 +               set_bit(HCI_UP, &hdev->flags);
18647 +               hci_notify(hdev, HCI_DEV_UP);
18648 +       } else {        
18649 +               /* Init failed, cleanup */
18650 +               tasklet_kill(&hdev->rx_task);
18651 +               tasklet_kill(&hdev->tx_task);
18652 +               tasklet_kill(&hdev->cmd_task);
18653 +
18654 +               skb_queue_purge(&hdev->cmd_q);
18655 +               skb_queue_purge(&hdev->rx_q);
18656 +
18657 +               if (hdev->flush)
18658 +                       hdev->flush(hdev);
18659 +
18660 +               if (hdev->sent_cmd) {
18661 +                       kfree_skb(hdev->sent_cmd);
18662 +                       hdev->sent_cmd = NULL;
18663 +               }
18664 +
18665 +               hdev->close(hdev);
18666 +               hdev->flags = 0;
18667 +       }
18668 +
18669 +done:
18670 +       hci_req_unlock(hdev);
18671 +       hci_dev_put(hdev);
18672 +       return ret;
18673 +}
18674 +
18675 +static int hci_dev_do_close(struct hci_dev *hdev)
18676 +{
18677 +       BT_DBG("%s %p", hdev->name, hdev);
18678 +
18679 +       hci_req_cancel(hdev, ENODEV);
18680 +       hci_req_lock(hdev);
18681 +
18682 +       if (!test_and_clear_bit(HCI_UP, &hdev->flags)) {
18683 +               hci_req_unlock(hdev);
18684 +               return 0;
18685 +       }
18686 +
18687 +       /* Kill RX and TX tasks */
18688 +       tasklet_kill(&hdev->rx_task);
18689 +       tasklet_kill(&hdev->tx_task);
18690 +
18691 +       hci_dev_lock_bh(hdev);
18692 +       inquiry_cache_flush(hdev);
18693 +       hci_conn_hash_flush(hdev);
18694 +       hci_dev_unlock_bh(hdev);
18695 +       
18696         hci_notify(hdev, HCI_DEV_DOWN);
18697  
18698         if (hdev->flush)
18699 @@ -586,9 +548,9 @@
18700         /* Reset device */
18701         skb_queue_purge(&hdev->cmd_q);
18702         atomic_set(&hdev->cmd_cnt, 1);
18703 -       hdev->flags |= HCI_INIT;
18704 -       __hci_request(hdev, hci_reset_req, 0, HZ);
18705 -       hdev->flags &= ~HCI_INIT;
18706 +       set_bit(HCI_INIT, &hdev->flags);
18707 +       __hci_request(hdev, hci_reset_req, 0, HZ/4);
18708 +       clear_bit(HCI_INIT, &hdev->flags);
18709  
18710         /* Kill cmd task */
18711         tasklet_kill(&hdev->cmd_task);
18712 @@ -605,17 +567,28 @@
18713         }
18714  
18715         /* After this point our queues are empty
18716 -        * and no tasks are scheduled.
18717 -        */
18718 +        * and no tasks are scheduled. */
18719         hdev->close(hdev);
18720  
18721 -done:
18722 -       hci_req_unlock(hdev);
18723 -       hci_dev_put(hdev);
18724 +       /* Clear flags */
18725 +       hdev->flags = 0;
18726  
18727 +       hci_req_unlock(hdev);
18728         return 0;
18729  }
18730  
18731 +int hci_dev_close(__u16 dev)
18732 +{
18733 +       struct hci_dev *hdev;
18734 +       int err;
18735 +       
18736 +       if (!(hdev = hci_dev_get(dev)))
18737 +               return -ENODEV;
18738 +       err = hci_dev_do_close(hdev);
18739 +       hci_dev_put(hdev);
18740 +       return err;
18741 +}
18742 +
18743  int hci_dev_reset(__u16 dev)
18744  {
18745         struct hci_dev *hdev;
18746 @@ -627,16 +600,17 @@
18747         hci_req_lock(hdev);
18748         tasklet_disable(&hdev->tx_task);
18749  
18750 -       if (!(hdev->flags & HCI_UP))
18751 +       if (!test_bit(HCI_UP, &hdev->flags))
18752                 goto done;
18753  
18754         /* Drop queues */
18755         skb_queue_purge(&hdev->rx_q);
18756         skb_queue_purge(&hdev->cmd_q);
18757  
18758 -       inquiry_cache_flush(&hdev->inq_cache);
18759 -
18760 +       hci_dev_lock_bh(hdev);
18761 +       inquiry_cache_flush(hdev);
18762         hci_conn_hash_flush(hdev);
18763 +       hci_dev_unlock_bh(hdev);
18764  
18765         if (hdev->flush)
18766                 hdev->flush(hdev);
18767 @@ -650,7 +624,6 @@
18768         tasklet_enable(&hdev->tx_task);
18769         hci_req_unlock(hdev);
18770         hci_dev_put(hdev);
18771 -
18772         return ret;
18773  }
18774  
18775 @@ -669,30 +642,11 @@
18776         return ret;
18777  }
18778  
18779 -int hci_dev_setauth(unsigned long arg)
18780 -{
18781 -       struct hci_dev *hdev;
18782 -       struct hci_dev_req dr;
18783 -       int ret = 0;
18784 -
18785 -       if (copy_from_user(&dr, (void *) arg, sizeof(dr)))
18786 -               return -EFAULT;
18787 -
18788 -       if (!(hdev = hci_dev_get(dr.dev_id)))
18789 -               return -ENODEV;
18790 -
18791 -       ret = hci_request(hdev, hci_auth_req, dr.dev_opt, HCI_INIT_TIMEOUT);
18792 -
18793 -       hci_dev_put(hdev);
18794 -
18795 -       return ret;
18796 -}
18797 -
18798 -int hci_dev_setscan(unsigned long arg)
18799 +int hci_dev_cmd(unsigned int cmd, unsigned long arg)
18800  {
18801         struct hci_dev *hdev;
18802         struct hci_dev_req dr;
18803 -       int ret = 0;
18804 +       int err = 0;
18805  
18806         if (copy_from_user(&dr, (void *) arg, sizeof(dr)))
18807                 return -EFAULT;
18808 @@ -700,75 +654,105 @@
18809         if (!(hdev = hci_dev_get(dr.dev_id)))
18810                 return -ENODEV;
18811  
18812 -       ret = hci_request(hdev, hci_scan_req, dr.dev_opt, HCI_INIT_TIMEOUT);
18813 -
18814 -       hci_dev_put(hdev);
18815 +       switch (cmd) {
18816 +       case HCISETAUTH:
18817 +               err = hci_request(hdev, hci_auth_req, dr.dev_opt, HCI_INIT_TIMEOUT);
18818 +               break;
18819  
18820 -       return ret;
18821 -}
18822 +       case HCISETENCRYPT:
18823 +               if (!lmp_encrypt_capable(hdev)) {
18824 +                       err = -EOPNOTSUPP;
18825 +                       break;
18826 +               }
18827  
18828 -int hci_dev_setptype(unsigned long arg)
18829 -{
18830 -       struct hci_dev *hdev;
18831 -       struct hci_dev_req dr;
18832 -       int ret = 0;
18833 +               if (!test_bit(HCI_AUTH, &hdev->flags)) {
18834 +                       /* Auth must be enabled first */
18835 +                       err = hci_request(hdev, hci_auth_req,
18836 +                                       dr.dev_opt, HCI_INIT_TIMEOUT);
18837 +                       if (err)
18838 +                               break;
18839 +               }
18840 +                       
18841 +               err = hci_request(hdev, hci_encrypt_req,
18842 +                                       dr.dev_opt, HCI_INIT_TIMEOUT);
18843 +               break;
18844 +       
18845 +       case HCISETSCAN:
18846 +               err = hci_request(hdev, hci_scan_req, dr.dev_opt, HCI_INIT_TIMEOUT);
18847 +               break;
18848 +       
18849 +       case HCISETPTYPE:
18850 +               hdev->pkt_type = (__u16) dr.dev_opt;
18851 +               break;
18852 +               
18853 +       case HCISETLINKPOL:
18854 +               hdev->link_policy = (__u16) dr.dev_opt;
18855 +               break;
18856  
18857 -       if (copy_from_user(&dr, (void *) arg, sizeof(dr)))
18858 -               return -EFAULT;
18859 +       case HCISETLINKMODE:
18860 +               hdev->link_mode = ((__u16) dr.dev_opt) & (HCI_LM_MASTER | HCI_LM_ACCEPT);
18861 +               break;
18862  
18863 -       if (!(hdev = hci_dev_get(dr.dev_id)))
18864 -               return -ENODEV;
18865 +       case HCISETACLMTU:
18866 +               hdev->acl_mtu  = *((__u16 *)&dr.dev_opt + 1);
18867 +               hdev->acl_pkts = *((__u16 *)&dr.dev_opt + 0);
18868 +               break;
18869  
18870 -       hdev->pkt_type = (__u16) dr.dev_opt;
18871 +       case HCISETSCOMTU:
18872 +               hdev->sco_mtu  = *((__u16 *)&dr.dev_opt + 1);
18873 +               hdev->sco_pkts = *((__u16 *)&dr.dev_opt + 0);
18874 +               break;
18875  
18876 +       default:
18877 +               err = -EINVAL;
18878 +               break;
18879 +       }       
18880         hci_dev_put(hdev);
18881 -
18882 -       return ret;
18883 +       return err;
18884  }
18885  
18886 -int hci_dev_list(unsigned long arg)
18887 +int hci_get_dev_list(unsigned long arg)
18888  {
18889         struct hci_dev_list_req *dl;
18890         struct hci_dev_req *dr;
18891 -       struct hci_dev *hdev;
18892 -       int i, n, size;
18893 +       struct list_head *p;
18894 +       int n = 0, size, err;
18895         __u16 dev_num;
18896  
18897         if (get_user(dev_num, (__u16 *) arg))
18898                 return -EFAULT;
18899  
18900 -       /* Avoid long loop, overflow */
18901 -       if (dev_num > 2048)
18902 +       if (!dev_num || dev_num > (PAGE_SIZE * 2) / sizeof(*dr))
18903                 return -EINVAL;
18904 -       
18905 -       size = dev_num * sizeof(struct hci_dev_req) + sizeof(__u16);
18906  
18907 -       if (verify_area(VERIFY_WRITE, (void *) arg, size))
18908 -               return -EFAULT;
18909 +       size = sizeof(*dl) + dev_num * sizeof(*dr);
18910  
18911         if (!(dl = kmalloc(size, GFP_KERNEL)))
18912                 return -ENOMEM;
18913 +
18914         dr = dl->dev_req;
18915  
18916 -       spin_lock_bh(&hdev_list_lock);
18917 -       for (i = 0, n = 0; i < HCI_MAX_DEV && n < dev_num; i++) {
18918 -               if ((hdev = hdev_list[i])) {
18919 -                       (dr + n)->dev_id  = hdev->id;
18920 -                       (dr + n)->dev_opt = hdev->flags;
18921 -                       n++;
18922 -               }
18923 +       read_lock_bh(&hdev_list_lock);
18924 +       list_for_each(p, &hdev_list) {
18925 +               struct hci_dev *hdev;
18926 +               hdev = list_entry(p, struct hci_dev, list);
18927 +               (dr + n)->dev_id  = hdev->id;
18928 +               (dr + n)->dev_opt = hdev->flags;
18929 +               if (++n >= dev_num)
18930 +                       break;
18931         }
18932 -       spin_unlock_bh(&hdev_list_lock);
18933 +       read_unlock_bh(&hdev_list_lock);
18934  
18935         dl->dev_num = n;
18936 -       size = n * sizeof(struct hci_dev_req) + sizeof(__u16);
18937 +       size = sizeof(*dl) + n * sizeof(*dr);
18938  
18939 -       copy_to_user((void *) arg, dl, size);
18940 +       err = copy_to_user((void *) arg, dl, size);
18941 +       kfree(dl);
18942  
18943 -       return 0;
18944 +       return err ? -EFAULT : 0;
18945  }
18946  
18947 -int hci_dev_info(unsigned long arg)
18948 +int hci_get_dev_info(unsigned long arg)
18949  {
18950         struct hci_dev *hdev;
18951         struct hci_dev_info di;
18952 @@ -786,9 +770,11 @@
18953         di.flags    = hdev->flags;
18954         di.pkt_type = hdev->pkt_type;
18955         di.acl_mtu  = hdev->acl_mtu;
18956 -       di.acl_max  = hdev->acl_max;
18957 +       di.acl_pkts = hdev->acl_pkts;
18958         di.sco_mtu  = hdev->sco_mtu;
18959 -       di.sco_max  = hdev->sco_max;
18960 +       di.sco_pkts = hdev->sco_pkts;
18961 +       di.link_policy = hdev->link_policy;
18962 +       di.link_mode   = hdev->link_mode;
18963  
18964         memcpy(&di.stat, &hdev->stat, sizeof(di.stat));
18965         memcpy(&di.features, &hdev->features, sizeof(di.features));
18966 @@ -801,258 +787,168 @@
18967         return err;
18968  }
18969  
18970 -__u32 hci_dev_setmode(struct hci_dev *hdev, __u32 mode)
18971 -{
18972 -       __u32 omode = hdev->flags & HCI_MODE_MASK;
18973 -
18974 -       hdev->flags &= ~HCI_MODE_MASK;
18975 -       hdev->flags |= (mode & HCI_MODE_MASK);
18976  
18977 -       return omode;
18978 -}
18979 +/* ---- Interface to HCI drivers ---- */
18980  
18981 -__u32 hci_dev_getmode(struct hci_dev *hdev)
18982 +/* Register HCI device */
18983 +int hci_register_dev(struct hci_dev *hdev)
18984  {
18985 -       return hdev->flags & HCI_MODE_MASK;
18986 -}
18987 +       struct list_head *head = &hdev_list, *p;
18988 +       int id = 0;
18989  
18990 -int hci_conn_list(unsigned long arg)
18991 -{
18992 -       struct hci_conn_list_req req, *cl;
18993 -       struct hci_conn_info *ci;
18994 -       struct hci_dev *hdev;
18995 -       struct list_head *p;
18996 -       int n = 0, size;
18997 +       BT_DBG("%p name %s type %d", hdev, hdev->name, hdev->type);
18998  
18999 -       if (copy_from_user(&req, (void *) arg, sizeof(req)))
19000 -               return -EFAULT;
19001 +       if (!hdev->open || !hdev->close || !hdev->destruct)
19002 +               return -EINVAL;
19003  
19004 -       if (!(hdev = hci_dev_get(req.dev_id)))
19005 -               return -ENODEV;
19006 +       write_lock_bh(&hdev_list_lock);
19007  
19008 -       /* Set a limit to avoid overlong loops, and also numeric overflow - AC */
19009 -       if(req.conn_num < 2048)
19010 -               return -EINVAL;
19011 +       /* Find first available device id */
19012 +       list_for_each(p, &hdev_list) {
19013 +               if (list_entry(p, struct hci_dev, list)->id != id)
19014 +                       break;
19015 +               head = p; id++;
19016 +       }
19017         
19018 -       size = req.conn_num * sizeof(struct hci_conn_info) + sizeof(req);
19019 +       sprintf(hdev->name, "hci%d", id);
19020 +       hdev->id = id;
19021 +       list_add(&hdev->list, head);
19022  
19023 -       if (!(cl = kmalloc(size, GFP_KERNEL)))
19024 -               return -ENOMEM;
19025 -       ci = cl->conn_info;
19026 -
19027 -       local_bh_disable();
19028 -       conn_hash_lock(&hdev->conn_hash);
19029 -       list_for_each(p, &hdev->conn_hash.list) {
19030 -               register struct hci_conn *c;
19031 -               c = list_entry(p, struct hci_conn, list);
19032 +       atomic_set(&hdev->refcnt, 1);
19033 +       spin_lock_init(&hdev->lock);
19034 +                       
19035 +       hdev->flags = 0;
19036 +       hdev->pkt_type  = (HCI_DM1 | HCI_DH1 | HCI_HV1);
19037 +       hdev->link_mode = (HCI_LM_ACCEPT);
19038  
19039 -               (ci + n)->handle = c->handle;
19040 -               bacpy(&(ci + n)->bdaddr, &c->dst);
19041 -               n++;
19042 -       }
19043 -       conn_hash_unlock(&hdev->conn_hash);
19044 -       local_bh_enable();
19045 -
19046 -       cl->dev_id = hdev->id;
19047 -       cl->conn_num = n;
19048 -       size = n * sizeof(struct hci_conn_info) + sizeof(req);
19049 +       tasklet_init(&hdev->cmd_task, hci_cmd_task,(unsigned long) hdev);
19050 +       tasklet_init(&hdev->rx_task, hci_rx_task, (unsigned long) hdev);
19051 +       tasklet_init(&hdev->tx_task, hci_tx_task, (unsigned long) hdev);
19052  
19053 -       hci_dev_put(hdev);
19054 +       skb_queue_head_init(&hdev->rx_q);
19055 +       skb_queue_head_init(&hdev->cmd_q);
19056 +       skb_queue_head_init(&hdev->raw_q);
19057  
19058 -       if(copy_to_user((void *) arg, cl, size))
19059 -               return -EFAULT;
19060 -       return 0;
19061 -}
19062 +       init_waitqueue_head(&hdev->req_wait_q);
19063 +       init_MUTEX(&hdev->req_lock);
19064  
19065 -int hci_inquiry(unsigned long arg)
19066 -{
19067 -       struct inquiry_cache *cache;
19068 -       struct hci_inquiry_req ir;
19069 -       struct hci_dev *hdev;
19070 -       int err = 0, do_inquiry = 0;
19071 -       long timeo;
19072 -       __u8 *buf, *ptr;
19073 +       inquiry_cache_init(hdev);
19074  
19075 -       ptr = (void *) arg;
19076 -       if (copy_from_user(&ir, ptr, sizeof(ir)))
19077 -               return -EFAULT;
19078 +       conn_hash_init(hdev);
19079  
19080 -       if (!(hdev = hci_dev_get(ir.dev_id)))
19081 -               return -ENODEV;
19082 +       memset(&hdev->stat, 0, sizeof(struct hci_dev_stats));
19083  
19084 -       cache = &hdev->inq_cache;
19085 +       atomic_set(&hdev->promisc, 0);
19086  
19087 -       inquiry_cache_lock(cache);
19088 -       if (inquiry_cache_age(cache) > INQUIRY_CACHE_AGE_MAX || ir.flags & IREQ_CACHE_FLUSH) {
19089 -               inquiry_cache_flush(cache);
19090 -               do_inquiry = 1;
19091 -       }
19092 -       inquiry_cache_unlock(cache);
19093 +       MOD_INC_USE_COUNT;
19094  
19095 -       /* Limit inquiry time, also avoid overflows */
19096 +       write_unlock_bh(&hdev_list_lock);
19097  
19098 -       if(ir.length > 2048 || ir.num_rsp > 2048)
19099 -       {
19100 -               err = -EINVAL;
19101 -               goto done;
19102 -       }
19103 +       hci_notify(hdev, HCI_DEV_REG);
19104 +       hci_run_hotplug(hdev->name, "register");
19105  
19106 -       timeo = ir.length * 2 * HZ;
19107 -       if (do_inquiry && (err = hci_request(hdev, hci_inq_req, (unsigned long)&ir, timeo)) < 0)
19108 -               goto done;
19109 +       return id;
19110 +}
19111  
19112 -       /* cache_dump can't sleep. Therefore we allocate temp buffer and then
19113 -        * copy it to the user space.
19114 -        */
19115 -       if (!(buf = kmalloc(sizeof(inquiry_info) * ir.num_rsp, GFP_KERNEL))) {
19116 -               err = -ENOMEM;
19117 -               goto done;
19118 -       }
19119 -       ir.num_rsp = inquiry_cache_dump(cache, ir.num_rsp, buf);
19120 +/* Unregister HCI device */
19121 +int hci_unregister_dev(struct hci_dev *hdev)
19122 +{
19123 +       BT_DBG("%p name %s type %d", hdev, hdev->name, hdev->type);
19124  
19125 -       DBG("num_rsp %d", ir.num_rsp);
19126 +       write_lock_bh(&hdev_list_lock);
19127 +       list_del(&hdev->list);
19128 +       write_unlock_bh(&hdev_list_lock);
19129  
19130 -       if (!verify_area(VERIFY_WRITE, ptr, sizeof(ir) + (sizeof(inquiry_info) * ir.num_rsp))) {
19131 -               copy_to_user(ptr, &ir, sizeof(ir));
19132 -               ptr += sizeof(ir);
19133 -               copy_to_user(ptr, buf, sizeof(inquiry_info) * ir.num_rsp);
19134 -       } else 
19135 -               err = -EFAULT;
19136 +       hci_dev_do_close(hdev);
19137  
19138 -       kfree(buf);
19139 +       hci_notify(hdev, HCI_DEV_UNREG);
19140 +       hci_run_hotplug(hdev->name, "unregister");
19141  
19142 -done:
19143         hci_dev_put(hdev);
19144  
19145 -       return err;
19146 +       MOD_DEC_USE_COUNT;
19147 +       return 0;
19148  }
19149  
19150 -/* Interface to HCI drivers */
19151 -
19152 -/* Register HCI device */
19153 -int hci_register_dev(struct hci_dev *hdev)
19154 +/* Suspend HCI device */
19155 +int hci_suspend_dev(struct hci_dev *hdev)
19156  {
19157 -       int i;
19158 +       hci_notify(hdev, HCI_DEV_SUSPEND);
19159 +       hci_run_hotplug(hdev->name, "suspend");
19160 +       return 0;
19161 +}
19162  
19163 -       DBG("%p name %s type %d", hdev, hdev->name, hdev->type);
19164 +/* Resume HCI device */
19165 +int hci_resume_dev(struct hci_dev *hdev)
19166 +{
19167 +       hci_notify(hdev, HCI_DEV_RESUME);
19168 +       hci_run_hotplug(hdev->name, "resume");
19169 +       return 0;
19170 +}       
19171  
19172 -       /* Find free slot */
19173 -       spin_lock_bh(&hdev_list_lock);
19174 -       for (i = 0; i < HCI_MAX_DEV; i++) {
19175 -               if (!hdev_list[i]) {
19176 -                       hdev_list[i] = hdev;
19177 -
19178 -                       sprintf(hdev->name, "hci%d", i);
19179 -                       atomic_set(&hdev->refcnt, 0);
19180 -                       hdev->id    = i;
19181 -                       hdev->flags = HCI_NORMAL;
19182 -
19183 -                       hdev->pkt_type = (HCI_DM1 | HCI_DH1);
19184 -
19185 -                       tasklet_init(&hdev->cmd_task, hci_cmd_task, (unsigned long) hdev);
19186 -                       tasklet_init(&hdev->rx_task, hci_rx_task, (unsigned long) hdev);
19187 -                       tasklet_init(&hdev->tx_task, hci_tx_task, (unsigned long) hdev);
19188 -
19189 -                       skb_queue_head_init(&hdev->rx_q);
19190 -                       skb_queue_head_init(&hdev->cmd_q);
19191 -                       skb_queue_head_init(&hdev->raw_q);
19192 -
19193 -                       init_waitqueue_head(&hdev->req_wait_q);
19194 -                       init_MUTEX(&hdev->req_lock);
19195 -
19196 -                       inquiry_cache_init(&hdev->inq_cache);
19197 -
19198 -                       conn_hash_init(&hdev->conn_hash);
19199 -
19200 -                       memset(&hdev->stat, 0, sizeof(struct hci_dev_stats));
19201 -
19202 -                       hci_notify(hdev, HCI_DEV_REG);
19203 -
19204 -                       MOD_INC_USE_COUNT;
19205 -                       break;
19206 -               }
19207 -       }
19208 -       spin_unlock_bh(&hdev_list_lock);
19209 -
19210 -       return (i == HCI_MAX_DEV) ? -1 : i;
19211 -}
19212 -
19213 -/* Unregister HCI device */
19214 -int hci_unregister_dev(struct hci_dev *hdev)
19215 +/* Receive frame from HCI drivers */
19216 +int hci_recv_frame(struct sk_buff *skb)
19217  {
19218 -       int i;
19219 -
19220 -       DBG("%p name %s type %d", hdev, hdev->name, hdev->type);
19221 -
19222 -       if (hdev->flags & HCI_UP)
19223 -               hci_dev_close(hdev->id);
19224 +       struct hci_dev *hdev = (struct hci_dev *) skb->dev;
19225  
19226 -       /* Find device slot */
19227 -       spin_lock(&hdev_list_lock);
19228 -       for (i = 0; i < HCI_MAX_DEV; i++) {
19229 -               if (hdev_list[i] == hdev) {
19230 -                       hdev_list[i] = NULL;
19231 -                       MOD_DEC_USE_COUNT;
19232 -                       break;
19233 -               }
19234 +       if (!hdev || (!test_bit(HCI_UP, &hdev->flags) && 
19235 +                               !test_bit(HCI_INIT, &hdev->flags)) ) {
19236 +               kfree_skb(skb);
19237 +               return -1;
19238         }
19239 -       spin_unlock(&hdev_list_lock);
19240  
19241 -       hci_notify(hdev, HCI_DEV_UNREG);
19242 -
19243 -       /* Sleep while device is in use */
19244 -       while (atomic_read(&hdev->refcnt)) {
19245 -               int sleep_cnt = 100;
19246 +       BT_DBG("%s type %d len %d", hdev->name, skb->pkt_type, skb->len);
19247  
19248 -               DBG("%s sleeping on lock %d", hdev->name, atomic_read(&hdev->refcnt));
19249 +       /* Incomming skb */
19250 +       bluez_cb(skb)->incomming = 1;
19251  
19252 -               sleep_on_timeout(&hdev->req_wait_q, HZ*10);
19253 -               if (!(--sleep_cnt))
19254 -                       break;
19255 -       }
19256 +       /* Time stamp */
19257 +       do_gettimeofday(&skb->stamp);
19258  
19259 +       /* Queue frame for rx task */
19260 +       skb_queue_tail(&hdev->rx_q, skb);
19261 +       hci_sched_rx(hdev);
19262         return 0;
19263  }
19264  
19265 -/* Interface to upper protocols */
19266 +/* ---- Interface to upper protocols ---- */
19267  
19268  /* Register/Unregister protocols.
19269 - * hci_task_lock is used to ensure that no tasks are running.
19270 - */
19271 -int hci_register_proto(struct hci_proto *hproto)
19272 + * hci_task_lock is used to ensure that no tasks are running. */
19273 +int hci_register_proto(struct hci_proto *hp)
19274  {
19275         int err = 0;
19276  
19277 -       DBG("%p name %s", hproto, hproto->name);
19278 +       BT_DBG("%p name %s id %d", hp, hp->name, hp->id);
19279  
19280 -       if (hproto->id >= HCI_MAX_PROTO)
19281 +       if (hp->id >= HCI_MAX_PROTO)
19282                 return -EINVAL;
19283  
19284         write_lock_bh(&hci_task_lock);
19285  
19286 -       if (!hproto_list[hproto->id])
19287 -               hproto_list[hproto->id] = hproto;
19288 +       if (!hci_proto[hp->id])
19289 +               hci_proto[hp->id] = hp;
19290         else
19291 -               err = -1;
19292 +               err = -EEXIST;
19293  
19294         write_unlock_bh(&hci_task_lock);
19295  
19296         return err;
19297  }
19298  
19299 -int hci_unregister_proto(struct hci_proto *hproto)
19300 +int hci_unregister_proto(struct hci_proto *hp)
19301  {
19302         int err = 0;
19303  
19304 -       DBG("%p name %s", hproto, hproto->name);
19305 +       BT_DBG("%p name %s id %d", hp, hp->name, hp->id);
19306  
19307 -       if (hproto->id > HCI_MAX_PROTO)
19308 +       if (hp->id >= HCI_MAX_PROTO)
19309                 return -EINVAL;
19310  
19311         write_lock_bh(&hci_task_lock);
19312  
19313 -       if (hproto_list[hproto->id])
19314 -               hproto_list[hproto->id] = NULL;
19315 +       if (hci_proto[hp->id])
19316 +               hci_proto[hp->id] = NULL;
19317         else
19318                 err = -ENOENT;
19319  
19320 @@ -1070,10 +966,14 @@
19321                 return -ENODEV;
19322         }
19323  
19324 -       DBG("%s type %d len %d", hdev->name, skb->pkt_type, skb->len);
19325 +       BT_DBG("%s type %d len %d", hdev->name, skb->pkt_type, skb->len);
19326 +
19327 +       if (atomic_read(&hdev->promisc)) {
19328 +               /* Time stamp */
19329 +               do_gettimeofday(&skb->stamp);
19330  
19331 -       if (hdev->flags & HCI_SOCK)
19332                 hci_send_to_sock(hdev, skb);
19333 +       }
19334  
19335         /* Get rid of skb owner, prior to sending to the driver. */
19336         skb_orphan(skb);
19337 @@ -1081,128 +981,6 @@
19338         return hdev->send(skb);
19339  }
19340  
19341 -/* Connection scheduler */
19342 -static inline struct hci_conn *hci_low_sent(struct hci_dev *hdev, __u8 type, int *quote)
19343 -{
19344 -       struct conn_hash *h = &hdev->conn_hash;
19345 -       struct hci_conn *conn = NULL;
19346 -       int num = 0, min = 0xffff;
19347 -        struct list_head *p;
19348 -
19349 -       conn_hash_lock(h);
19350 -       list_for_each(p, &h->list) {
19351 -               register struct hci_conn *c;
19352 -
19353 -               c = list_entry(p, struct hci_conn, list);
19354 -
19355 -               if (c->type != type || skb_queue_empty(&c->data_q))
19356 -                       continue;
19357 -               num++;
19358 -
19359 -               if (c->sent < min) {
19360 -                       min  = c->sent;
19361 -                       conn = c;
19362 -               }
19363 -       }
19364 -       conn_hash_unlock(h);
19365 -
19366 -       if (conn) {
19367 -               int q = hdev->acl_cnt / num;
19368 -               *quote = q ? q : 1;
19369 -       } else
19370 -               *quote = 0;
19371 -
19372 -       DBG("conn %p quote %d", conn, *quote);
19373 -
19374 -       return conn;
19375 -}
19376 -
19377 -static inline void hci_sched_acl(struct hci_dev *hdev)
19378 -{
19379 -       struct hci_conn *conn;
19380 -       struct sk_buff *skb;
19381 -       int quote;
19382 -
19383 -       DBG("%s", hdev->name);
19384 -
19385 -       while (hdev->acl_cnt && (conn = hci_low_sent(hdev, ACL_LINK, &quote))) {
19386 -               while (quote && (skb = skb_dequeue(&conn->data_q))) {
19387 -                       DBG("skb %p len %d", skb, skb->len);
19388 -
19389 -                       hci_send_frame(skb);
19390 -
19391 -                       conn->sent++;
19392 -                       hdev->acl_cnt--;
19393 -                       quote--;
19394 -               }
19395 -       }
19396 -}
19397 -
19398 -/* Schedule SCO */
19399 -static inline void hci_sched_sco(struct hci_dev *hdev)
19400 -{
19401 -       /* FIXME: For now we queue SCO packets to the raw queue 
19402 -
19403 -               while (hdev->sco_cnt && (skb = skb_dequeue(&conn->data_q))) {
19404 -                       hci_send_frame(skb);
19405 -                       conn->sco_sent++;
19406 -                       hdev->sco_cnt--;
19407 -               }
19408 -       */
19409 -}
19410 -
19411 -/* Get data from the previously sent command */
19412 -static void * hci_sent_cmd_data(struct hci_dev *hdev, __u16 ogf, __u16 ocf)
19413 -{
19414 -       hci_command_hdr *hc;
19415 -
19416 -       if (!hdev->sent_cmd)
19417 -               return NULL;
19418 -
19419 -       hc = (void *) hdev->sent_cmd->data;
19420 -
19421 -       if (hc->opcode != __cpu_to_le16(cmd_opcode_pack(ogf, ocf)))
19422 -               return NULL;
19423 -
19424 -       DBG("%s ogf 0x%x ocf 0x%x", hdev->name, ogf, ocf);
19425 -
19426 -       return hdev->sent_cmd->data + HCI_COMMAND_HDR_SIZE;
19427 -}
19428 -
19429 -/* Send raw HCI frame */
19430 -int hci_send_raw(struct sk_buff *skb)
19431 -{
19432 -       struct hci_dev *hdev = (struct hci_dev *) skb->dev;
19433 -
19434 -       if (!hdev) {
19435 -               kfree_skb(skb);
19436 -               return -ENODEV;
19437 -       }
19438 -
19439 -       DBG("%s type %d len %d", hdev->name, skb->pkt_type, skb->len);
19440 -
19441 -       if (hdev->flags & HCI_NORMAL) {
19442 -               /* Queue frame according it's type */
19443 -               switch (skb->pkt_type) {
19444 -               case HCI_COMMAND_PKT:
19445 -                       skb_queue_tail(&hdev->cmd_q, skb);
19446 -                       hci_sched_cmd(hdev);
19447 -                       return 0;
19448 -
19449 -               case HCI_ACLDATA_PKT:
19450 -               case HCI_SCODATA_PKT:
19451 -                       /* FIXME:
19452 -                        * Check header here and queue to apropriate connection.
19453 -                        */
19454 -                       break;
19455 -               }
19456 -       }
19457 -
19458 -       skb_queue_tail(&hdev->raw_q, skb);
19459 -       hci_sched_tx(hdev);
19460 -       return 0;
19461 -}
19462 -
19463  /* Send HCI command */
19464  int hci_send_cmd(struct hci_dev *hdev, __u16 ogf, __u16 ocf, __u32 plen, void *param)
19465  {
19466 @@ -1210,10 +988,10 @@
19467         hci_command_hdr *hc;
19468         struct sk_buff *skb;
19469  
19470 -       DBG("%s ogf 0x%x ocf 0x%x plen %d", hdev->name, ogf, ocf, plen);
19471 +       BT_DBG("%s ogf 0x%x ocf 0x%x plen %d", hdev->name, ogf, ocf, plen);
19472  
19473         if (!(skb = bluez_skb_alloc(len, GFP_ATOMIC))) {
19474 -               ERR("%s Can't allocate memory for HCI command", hdev->name);
19475 +               BT_ERR("%s Can't allocate memory for HCI command", hdev->name);
19476                 return -ENOMEM;
19477         }
19478         
19479 @@ -1224,7 +1002,7 @@
19480         if (plen)
19481                 memcpy(skb_put(skb, plen), param, plen);
19482  
19483 -       DBG("skb len %d", skb->len);
19484 +       BT_DBG("skb len %d", skb->len);
19485  
19486         skb->pkt_type = HCI_COMMAND_PKT;
19487         skb->dev = (void *) hdev;
19488 @@ -1234,10 +1012,28 @@
19489         return 0;
19490  }
19491  
19492 +/* Get data from the previously sent command */
19493 +void *hci_sent_cmd_data(struct hci_dev *hdev, __u16 ogf, __u16 ocf)
19494 +{
19495 +       hci_command_hdr *hc;
19496 +
19497 +       if (!hdev->sent_cmd)
19498 +               return NULL;
19499 +
19500 +       hc = (void *) hdev->sent_cmd->data;
19501 +
19502 +       if (hc->opcode != __cpu_to_le16(cmd_opcode_pack(ogf, ocf)))
19503 +               return NULL;
19504 +
19505 +       BT_DBG("%s ogf 0x%x ocf 0x%x", hdev->name, ogf, ocf);
19506 +
19507 +       return hdev->sent_cmd->data + HCI_COMMAND_HDR_SIZE;
19508 +}
19509 +
19510  /* Send ACL data */
19511  static void hci_add_acl_hdr(struct sk_buff *skb, __u16 handle, __u16 flags)
19512  {
19513 -       int len = skb->len;     
19514 +       int len = skb->len;
19515         hci_acl_hdr *ah;
19516  
19517         ah = (hci_acl_hdr *) skb_push(skb, HCI_ACL_HDR_SIZE);
19518 @@ -1252,7 +1048,7 @@
19519         struct hci_dev *hdev = conn->hdev;
19520         struct sk_buff *list;
19521  
19522 -       DBG("%s conn %p flags 0x%x", hdev->name, conn, flags);
19523 +       BT_DBG("%s conn %p flags 0x%x", hdev->name, conn, flags);
19524  
19525         skb->dev = (void *) hdev;
19526         skb->pkt_type = HCI_ACLDATA_PKT;
19527 @@ -1260,12 +1056,12 @@
19528  
19529         if (!(list = skb_shinfo(skb)->frag_list)) {
19530                 /* Non fragmented */
19531 -               DBG("%s nonfrag skb %p len %d", hdev->name, skb, skb->len);
19532 +               BT_DBG("%s nonfrag skb %p len %d", hdev->name, skb, skb->len);
19533                 
19534                 skb_queue_tail(&conn->data_q, skb);
19535         } else {
19536                 /* Fragmented */
19537 -               DBG("%s frag %p len %d", hdev->name, skb, skb->len);
19538 +               BT_DBG("%s frag %p len %d", hdev->name, skb, skb->len);
19539  
19540                 skb_shinfo(skb)->frag_list = NULL;
19541  
19542 @@ -1280,7 +1076,7 @@
19543                         skb->pkt_type = HCI_ACLDATA_PKT;
19544                         hci_add_acl_hdr(skb, conn->handle, flags | ACL_CONT);
19545                 
19546 -                       DBG("%s frag %p len %d", hdev->name, skb, skb->len);
19547 +                       BT_DBG("%s frag %p len %d", hdev->name, skb, skb->len);
19548  
19549                         __skb_queue_tail(&conn->data_q, skb);
19550                 } while (list);
19551 @@ -1298,7 +1094,7 @@
19552         struct hci_dev *hdev = conn->hdev;
19553         hci_sco_hdr hs;
19554  
19555 -       DBG("%s len %d", hdev->name, skb->len);
19556 +       BT_DBG("%s len %d", hdev->name, skb->len);
19557  
19558         if (skb->len > hdev->sco_mtu) {
19559                 kfree_skb(skb);
19560 @@ -1315,544 +1111,136 @@
19561         skb->pkt_type = HCI_SCODATA_PKT;
19562         skb_queue_tail(&conn->data_q, skb);
19563         hci_sched_tx(hdev);
19564 -
19565         return 0;
19566  }
19567  
19568 -/* Handle HCI Event packets */
19569 -
19570 -/* Command Complete OGF LINK_CTL  */
19571 -static void hci_cc_link_ctl(struct hci_dev *hdev, __u16 ocf, struct sk_buff *skb)
19572 -{
19573 -       DBG("%s ocf 0x%x", hdev->name, ocf);
19574 -
19575 -       switch (ocf) {
19576 -       default:
19577 -               DBG("%s Command complete: ogf LINK_CTL ocf %x", hdev->name, ocf);
19578 -               break;
19579 -       };
19580 -}
19581 -
19582 -/* Command Complete OGF LINK_POLICY  */
19583 -static void hci_cc_link_policy(struct hci_dev *hdev, __u16 ocf, struct sk_buff *skb)
19584 -{
19585 -       DBG("%s ocf 0x%x", hdev->name, ocf);
19586 -
19587 -       switch (ocf) {
19588 -       default:
19589 -               DBG("%s: Command complete: ogf LINK_POLICY ocf %x", hdev->name, ocf);
19590 -               break;
19591 -       };
19592 -}
19593 -
19594 -/* Command Complete OGF HOST_CTL  */
19595 -static void hci_cc_host_ctl(struct hci_dev *hdev, __u16 ocf, struct sk_buff *skb)
19596 -{
19597 -       __u8 status, param;
19598 -       void *sent;
19599 -
19600 -
19601 -       DBG("%s ocf 0x%x", hdev->name, ocf);
19602 -
19603 -       switch (ocf) {
19604 -       case OCF_RESET:
19605 -               status = *((__u8 *) skb->data);
19606 -
19607 -               hci_req_complete(hdev, status);
19608 -               break;
19609 -
19610 -       case OCF_SET_EVENT_FLT:
19611 -               status = *((__u8 *) skb->data);
19612 -
19613 -               if (status) {
19614 -                       DBG("%s SET_EVENT_FLT failed %d", hdev->name, status);
19615 -               } else {
19616 -                       DBG("%s SET_EVENT_FLT succeseful", hdev->name);
19617 -               }
19618 -               break;
19619 -
19620 -       case OCF_WRITE_AUTH_ENABLE:
19621 -               if (!(sent = hci_sent_cmd_data(hdev, OGF_HOST_CTL, OCF_WRITE_AUTH_ENABLE)))
19622 -                       break;
19623 -
19624 -               status = *((__u8 *) skb->data);
19625 -               param  = *((__u8 *) sent);
19626 -
19627 -               if (!status) {
19628 -                       if (param == AUTH_ENABLED)
19629 -                               hdev->flags |= HCI_AUTH;
19630 -                       else
19631 -                               hdev->flags &= ~HCI_AUTH;
19632 -               }
19633 -               hci_req_complete(hdev, status);
19634 -               break;
19635 -
19636 -       case OCF_WRITE_CA_TIMEOUT:
19637 -               status = *((__u8 *) skb->data);
19638 -
19639 -               if (status) {
19640 -                       DBG("%s OCF_WRITE_CA_TIMEOUT failed %d", hdev->name, status);
19641 -               } else {
19642 -                       DBG("%s OCF_WRITE_CA_TIMEOUT succeseful", hdev->name);
19643 -               }
19644 -               break;
19645 -
19646 -       case OCF_WRITE_PG_TIMEOUT:
19647 -               status = *((__u8 *) skb->data);
19648 -
19649 -               if (status) {
19650 -                       DBG("%s OCF_WRITE_PG_TIMEOUT failed %d", hdev->name, status);
19651 -               } else {
19652 -                       DBG("%s: OCF_WRITE_PG_TIMEOUT succeseful", hdev->name);
19653 -               }
19654 -               break;
19655 -
19656 -       case OCF_WRITE_SCAN_ENABLE:
19657 -               if (!(sent = hci_sent_cmd_data(hdev, OGF_HOST_CTL, OCF_WRITE_SCAN_ENABLE)))
19658 -                       break;
19659 -               status = *((__u8 *) skb->data);
19660 -               param  = *((__u8 *) sent);
19661 -
19662 -               DBG("param 0x%x", param);
19663 -
19664 -               if (!status) {
19665 -                       switch (param) {
19666 -                       case IS_ENA_PS_ENA:
19667 -                               hdev->flags |=  HCI_PSCAN | HCI_ISCAN;
19668 -                               break;
19669 -
19670 -                       case IS_ENA_PS_DIS:
19671 -                               hdev->flags &= ~HCI_PSCAN;
19672 -                               hdev->flags |=  HCI_ISCAN;
19673 -                               break;
19674 +/* ---- HCI TX task (outgoing data) ---- */
19675  
19676 -                       case IS_DIS_PS_ENA:
19677 -                               hdev->flags &= ~HCI_ISCAN;
19678 -                               hdev->flags |=  HCI_PSCAN;
19679 -                               break;
19680 -
19681 -                       default:
19682 -                               hdev->flags &= ~(HCI_ISCAN | HCI_PSCAN);
19683 -                               break;
19684 -                       };
19685 -               }
19686 -               hci_req_complete(hdev, status);
19687 -               break;
19688 -
19689 -       default:
19690 -               DBG("%s Command complete: ogf HOST_CTL ocf %x", hdev->name, ocf);
19691 -               break;
19692 -       };
19693 -}
19694 -
19695 -/* Command Complete OGF INFO_PARAM  */
19696 -static void hci_cc_info_param(struct hci_dev *hdev, __u16 ocf, struct sk_buff *skb)
19697 +/* HCI Connection scheduler */
19698 +static inline struct hci_conn *hci_low_sent(struct hci_dev *hdev, __u8 type, int *quote)
19699  {
19700 -       read_local_features_rp *lf;
19701 -       read_buffer_size_rp *bs;
19702 -       read_bd_addr_rp *ba;
19703 -
19704 -       DBG("%s ocf 0x%x", hdev->name, ocf);
19705 -
19706 -       switch (ocf) {
19707 -       case OCF_READ_LOCAL_FEATURES:
19708 -               lf = (read_local_features_rp *) skb->data;
19709 -
19710 -               if (lf->status) {
19711 -                       DBG("%s READ_LOCAL_FEATURES failed %d", hdev->name, lf->status);
19712 -                       break;
19713 -               }
19714 -
19715 -               memcpy(hdev->features, lf->features, sizeof(hdev->features));
19716 -
19717 -               /* Adjust default settings according to features 
19718 -                * supported by device. */
19719 -               if (hdev->features[0] & LMP_3SLOT)
19720 -                       hdev->pkt_type |= (HCI_DM3 | HCI_DH3);
19721 -
19722 -               if (hdev->features[0] & LMP_5SLOT)
19723 -                       hdev->pkt_type |= (HCI_DM5 | HCI_DH5);
19724 -
19725 -               DBG("%s: features 0x%x 0x%x 0x%x", hdev->name, lf->features[0], lf->features[1], lf->features[2]);
19726 -
19727 -               break;
19728 -
19729 -       case OCF_READ_BUFFER_SIZE:
19730 -               bs = (read_buffer_size_rp *) skb->data;
19731 -
19732 -               if (bs->status) {
19733 -                       DBG("%s READ_BUFFER_SIZE failed %d", hdev->name, bs->status);
19734 -                       break;
19735 -               }
19736 -
19737 -               hdev->acl_mtu = __le16_to_cpu(bs->acl_mtu);
19738 -               hdev->sco_mtu = bs->sco_mtu;
19739 -               hdev->acl_max = hdev->acl_cnt = __le16_to_cpu(bs->acl_max_pkt);
19740 -               hdev->sco_max = hdev->sco_cnt = __le16_to_cpu(bs->sco_max_pkt);
19741 -
19742 -               DBG("%s mtu: acl %d, sco %d max_pkt: acl %d, sco %d", hdev->name,
19743 -                   hdev->acl_mtu, hdev->sco_mtu, hdev->acl_max, hdev->sco_max);
19744 +       struct conn_hash *h = &hdev->conn_hash;
19745 +       struct hci_conn  *conn = NULL;
19746 +       int num = 0, min = ~0;
19747 +        struct list_head *p;
19748  
19749 -               break;
19750 +       /* We don't have to lock device here. Connections are always 
19751 +        * added and removed with TX task disabled. */
19752 +       list_for_each(p, &h->list) {
19753 +               struct hci_conn *c;
19754 +               c = list_entry(p, struct hci_conn, list);
19755  
19756 -       case OCF_READ_BD_ADDR:
19757 -               ba = (read_bd_addr_rp *) skb->data;
19758 +               if (c->type != type || c->state != BT_CONNECTED
19759 +                               || skb_queue_empty(&c->data_q))
19760 +                       continue;
19761 +               num++;
19762  
19763 -               if (!ba->status) {
19764 -                       bacpy(&hdev->bdaddr, &ba->bdaddr);
19765 -               } else {
19766 -                       DBG("%s: READ_BD_ADDR failed %d", hdev->name, ba->status);
19767 +               if (c->sent < min) {
19768 +                       min  = c->sent;
19769 +                       conn = c;
19770                 }
19771 +       }
19772  
19773 -               hci_req_complete(hdev, ba->status);
19774 -               break;
19775 +       if (conn) {
19776 +               int cnt = (type == ACL_LINK ? hdev->acl_cnt : hdev->sco_cnt);
19777 +               int q = cnt / num;
19778 +               *quote = q ? q : 1;
19779 +       } else
19780 +               *quote = 0;
19781  
19782 -       default:
19783 -               DBG("%s Command complete: ogf INFO_PARAM ocf %x", hdev->name, ocf);
19784 -               break;
19785 -       };
19786 +       BT_DBG("conn %p quote %d", conn, *quote);
19787 +       return conn;
19788  }
19789  
19790 -/* Command Status OGF LINK_CTL  */
19791 -static void hci_cs_link_ctl(struct hci_dev *hdev, __u16 ocf, __u8 status)
19792 +static inline void hci_acl_tx_to(struct hci_dev *hdev)
19793  {
19794 -       struct hci_proto * hp;
19795 -
19796 -       DBG("%s ocf 0x%x", hdev->name, ocf);
19797 -
19798 -       switch (ocf) {
19799 -       case OCF_CREATE_CONN:
19800 -               if (status) {
19801 -                       create_conn_cp *cc = hci_sent_cmd_data(hdev, OGF_LINK_CTL, OCF_CREATE_CONN);
19802 -
19803 -                       if (!cc)
19804 -                               break;
19805 -
19806 -                       DBG("%s Create connection error: status 0x%x %s", hdev->name,
19807 -                           status, batostr(&cc->bdaddr));
19808 +       struct conn_hash *h = &hdev->conn_hash;
19809 +       struct list_head *p;
19810 +       struct hci_conn  *c;
19811  
19812 -                       /* Notify upper protocols */
19813 -                       if ((hp = GET_HPROTO(HCI_PROTO_L2CAP)) && hp->connect_cfm) {
19814 -                               tasklet_disable(&hdev->tx_task);
19815 -                               hp->connect_cfm(hdev, &cc->bdaddr, status, NULL);
19816 -                               tasklet_enable(&hdev->tx_task);
19817 -                       }
19818 -               }
19819 -               break;
19820 +       BT_ERR("%s ACL tx timeout", hdev->name);
19821  
19822 -       case OCF_INQUIRY:
19823 -               if (status) {
19824 -                       DBG("%s Inquiry error: status 0x%x", hdev->name, status);
19825 -                       hci_req_complete(hdev, status);
19826 +       /* Kill stalled connections */
19827 +       list_for_each(p, &h->list) {
19828 +               c = list_entry(p, struct hci_conn, list);
19829 +               if (c->type == ACL_LINK && c->sent) {
19830 +                       BT_ERR("%s killing stalled ACL connection %s",
19831 +                               hdev->name, batostr(&c->dst));
19832 +                       hci_acl_disconn(c, 0x13);
19833                 }
19834 -               break;
19835 -
19836 -       default:
19837 -               DBG("%s Command status: ogf LINK_CTL ocf %x", hdev->name, ocf);
19838 -               break;
19839 -       };
19840 -}
19841 -
19842 -/* Command Status OGF LINK_POLICY */
19843 -static void hci_cs_link_policy(struct hci_dev *hdev, __u16 ocf, __u8 status)
19844 -{
19845 -       DBG("%s ocf 0x%x", hdev->name, ocf);
19846 -
19847 -       switch (ocf) {
19848 -       default:
19849 -               DBG("%s Command status: ogf HOST_POLICY ocf %x", hdev->name, ocf);
19850 -               break;
19851 -       };
19852 -}
19853 -
19854 -/* Command Status OGF HOST_CTL */
19855 -static void hci_cs_host_ctl(struct hci_dev *hdev, __u16 ocf, __u8 status)
19856 -{
19857 -       DBG("%s ocf 0x%x", hdev->name, ocf);
19858 -
19859 -       switch (ocf) {
19860 -       default:
19861 -               DBG("%s Command status: ogf HOST_CTL ocf %x", hdev->name, ocf);
19862 -               break;
19863 -       };
19864 -}
19865 -
19866 -/* Command Status OGF INFO_PARAM  */
19867 -static void hci_cs_info_param(struct hci_dev *hdev, __u16 ocf, __u8 status)
19868 -{
19869 -       DBG("%s: hci_cs_info_param: ocf 0x%x", hdev->name, ocf);
19870 -
19871 -       switch (ocf) {
19872 -       default:
19873 -               DBG("%s Command status: ogf INFO_PARAM ocf %x", hdev->name, ocf);
19874 -               break;
19875 -       };
19876 -}
19877 -
19878 -/* Inquiry Complete */
19879 -static void hci_inquiry_complete_evt(struct hci_dev *hdev, struct sk_buff *skb)
19880 -{
19881 -       __u8 status = *((__u8 *) skb->data);
19882 -
19883 -       DBG("%s status %d", hdev->name, status);
19884 -
19885 -       hci_req_complete(hdev, status);
19886 +       }
19887  }
19888  
19889 -/* Inquiry Result */
19890 -static void hci_inquiry_result_evt(struct hci_dev *hdev, struct sk_buff *skb)
19891 +static inline void hci_sched_acl(struct hci_dev *hdev)
19892  {
19893 -       inquiry_info *info = (inquiry_info *) (skb->data + 1);
19894 -       int num_rsp = *((__u8 *) skb->data);
19895 -
19896 -       DBG("%s num_rsp %d", hdev->name, num_rsp);
19897 +       struct hci_conn *conn;
19898 +       struct sk_buff *skb;
19899 +       int quote;
19900  
19901 -       for (; num_rsp; num_rsp--)
19902 -               inquiry_cache_update(&hdev->inq_cache, info++);
19903 -}
19904 +       BT_DBG("%s", hdev->name);
19905  
19906 -/* Connect Request */
19907 -static void hci_conn_request_evt(struct hci_dev *hdev, struct sk_buff *skb)
19908 -{
19909 -       evt_conn_request *cr = (evt_conn_request *) skb->data;
19910 -       struct hci_proto *hp;
19911 -       accept_conn_req_cp ac;
19912 -       int accept = 0;
19913 +       /* ACL tx timeout must be longer than maximum
19914 +        * link supervision timeout (40.9 seconds) */
19915 +       if (!hdev->acl_cnt && (jiffies - hdev->acl_last_tx) > (HZ * 45))
19916 +               hci_acl_tx_to(hdev);
19917  
19918 -       DBG("%s Connection request: %s type 0x%x", hdev->name, batostr(&cr->bdaddr), cr->link_type);
19919 +       while (hdev->acl_cnt && (conn = hci_low_sent(hdev, ACL_LINK, &quote))) {
19920 +               while (quote-- && (skb = skb_dequeue(&conn->data_q))) {
19921 +                       BT_DBG("skb %p len %d", skb, skb->len);
19922 +                       hci_send_frame(skb);
19923 +                       hdev->acl_last_tx = jiffies;
19924  
19925 -       /* Notify upper protocols */
19926 -       if (cr->link_type == ACL_LINK) {
19927 -               /* ACL link notify L2CAP */
19928 -               if ((hp = GET_HPROTO(HCI_PROTO_L2CAP)) && hp->connect_ind) {
19929 -                       tasklet_disable(&hdev->tx_task);
19930 -                       accept = hp->connect_ind(hdev, &cr->bdaddr);
19931 -                       tasklet_enable(&hdev->tx_task);
19932 +                       hdev->acl_cnt--;
19933 +                       conn->sent++;
19934                 }
19935 -       } else {
19936 -               /* SCO link (no notification) */
19937 -               /* FIXME: Should be accept it here or let the requester (app) accept it ? */
19938 -               accept = 1;
19939 -       }
19940 -
19941 -       if (accept) {
19942 -               /* Connection accepted by upper layer */
19943 -               bacpy(&ac.bdaddr, &cr->bdaddr);
19944 -               ac.role = 0x01; /* Remain slave */
19945 -               hci_send_cmd(hdev, OGF_LINK_CTL, OCF_ACCEPT_CONN_REQ, ACCEPT_CONN_REQ_CP_SIZE, &ac);
19946 -       } else {
19947 -               /* Connection rejected by upper layer */
19948 -               /* FIXME: 
19949 -                * Should we use HCI reject here ?
19950 -                */
19951 -               return;
19952         }
19953  }
19954  
19955 -/* Connect Complete */
19956 -static void hci_conn_complete_evt(struct hci_dev *hdev, struct sk_buff *skb)
19957 +/* Schedule SCO */
19958 +static inline void hci_sched_sco(struct hci_dev *hdev)
19959  {
19960 -       evt_conn_complete *cc = (evt_conn_complete *) skb->data;
19961 -       struct hci_conn *conn = NULL;
19962 -       struct hci_proto *hp;
19963 -
19964 -       DBG("%s", hdev->name);
19965 -
19966 -       tasklet_disable(&hdev->tx_task);
19967 -
19968 -       if (!cc->status)
19969 -               conn = hci_conn_add(hdev, __le16_to_cpu(cc->handle), cc->link_type, &cc->bdaddr);
19970 +       struct hci_conn *conn;
19971 +       struct sk_buff *skb;
19972 +       int quote;
19973  
19974 -       /* Notify upper protocols */
19975 -       if (cc->link_type == ACL_LINK) {
19976 -               /* ACL link notify L2CAP layer */
19977 -               if ((hp = GET_HPROTO(HCI_PROTO_L2CAP)) && hp->connect_cfm)
19978 -                       hp->connect_cfm(hdev, &cc->bdaddr, cc->status, conn);
19979 -       } else {
19980 -               /* SCO link (no notification) */
19981 -       }
19982 +       BT_DBG("%s", hdev->name);
19983  
19984 -       tasklet_enable(&hdev->tx_task);
19985 -}
19986 +       while (hdev->sco_cnt && (conn = hci_low_sent(hdev, SCO_LINK, &quote))) {
19987 +               while (quote-- && (skb = skb_dequeue(&conn->data_q))) {
19988 +                       BT_DBG("skb %p len %d", skb, skb->len);
19989 +                       hci_send_frame(skb);
19990  
19991 -/* Disconnect Complete */
19992 -static void hci_disconn_complete_evt(struct hci_dev *hdev, struct sk_buff *skb)
19993 -{
19994 -       evt_disconn_complete *dc = (evt_disconn_complete *) skb->data;
19995 -       struct hci_conn *conn = NULL;
19996 -       struct hci_proto *hp;
19997 -       __u16 handle = __le16_to_cpu(dc->handle);
19998 -
19999 -       DBG("%s", hdev->name);
20000 -
20001 -       if (!dc->status && (conn = conn_hash_lookup(&hdev->conn_hash, handle))) {
20002 -               tasklet_disable(&hdev->tx_task);
20003 -
20004 -               /* Notify upper protocols */
20005 -               if (conn->type == ACL_LINK) {
20006 -                       /* ACL link notify L2CAP layer */
20007 -                       if ((hp = GET_HPROTO(HCI_PROTO_L2CAP)) && hp->disconn_ind)
20008 -                               hp->disconn_ind(conn, dc->reason);
20009 -               } else {
20010 -                       /* SCO link (no notification) */
20011 +                       conn->sent++;
20012 +                       if (conn->sent == ~0)
20013 +                               conn->sent = 0;
20014                 }
20015 -
20016 -               hci_conn_del(hdev, conn);
20017 -
20018 -               tasklet_enable(&hdev->tx_task);
20019         }
20020  }
20021  
20022 -/* Number of completed packets */
20023 -static void hci_num_comp_pkts_evt(struct hci_dev *hdev, struct sk_buff *skb)
20024 +static void hci_tx_task(unsigned long arg)
20025  {
20026 -       evt_num_comp_pkts *nc = (evt_num_comp_pkts *) skb->data;
20027 -       __u16 *ptr;
20028 -       int i;
20029 -
20030 -       skb_pull(skb, EVT_NUM_COMP_PKTS_SIZE);
20031 -
20032 -       DBG("%s num_hndl %d", hdev->name, nc->num_hndl);
20033 +       struct hci_dev *hdev = (struct hci_dev *) arg;
20034 +       struct sk_buff *skb;
20035  
20036 -       if (skb->len < nc->num_hndl * 4) {
20037 -               DBG("%s bad parameters", hdev->name);
20038 -               return;
20039 -       }
20040 +       read_lock(&hci_task_lock);
20041  
20042 -       tasklet_disable(&hdev->tx_task);
20043 +       BT_DBG("%s acl %d sco %d", hdev->name, hdev->acl_cnt, hdev->sco_cnt);
20044  
20045 -       for (i = 0, ptr = (__u16 *) skb->data; i < nc->num_hndl; i++) {
20046 -               struct hci_conn *conn;
20047 -               __u16 handle, count;
20048 +       /* Schedule queues and send stuff to HCI driver */
20049  
20050 -               handle = __le16_to_cpu(get_unaligned(ptr++));
20051 -               count  = __le16_to_cpu(get_unaligned(ptr++));
20052 +       hci_sched_acl(hdev);
20053  
20054 -               hdev->acl_cnt += count;
20055 +       hci_sched_sco(hdev);
20056  
20057 -               if ((conn = conn_hash_lookup(&hdev->conn_hash, handle)))
20058 -                       conn->sent -= count;
20059 -       }
20060 +       /* Send next queued raw (unknown type) packet */
20061 +       while ((skb = skb_dequeue(&hdev->raw_q)))
20062 +               hci_send_frame(skb);
20063  
20064 -       tasklet_enable(&hdev->tx_task);
20065 -       
20066 -       hci_sched_tx(hdev);
20067 +       read_unlock(&hci_task_lock);
20068  }
20069  
20070 -static inline void hci_event_packet(struct hci_dev *hdev, struct sk_buff *skb)
20071 -{
20072 -       hci_event_hdr *he = (hci_event_hdr *) skb->data;
20073 -       evt_cmd_status *cs;
20074 -       evt_cmd_complete *ec;
20075 -       __u16 opcode, ocf, ogf;
20076 -
20077 -       skb_pull(skb, HCI_EVENT_HDR_SIZE);
20078 -
20079 -       DBG("%s evt 0x%x", hdev->name, he->evt);
20080 -
20081 -       switch (he->evt) {
20082 -       case EVT_NUM_COMP_PKTS:
20083 -               hci_num_comp_pkts_evt(hdev, skb);
20084 -               break;
20085 -
20086 -       case EVT_INQUIRY_COMPLETE:
20087 -               hci_inquiry_complete_evt(hdev, skb);
20088 -               break;
20089  
20090 -       case EVT_INQUIRY_RESULT:
20091 -               hci_inquiry_result_evt(hdev, skb);
20092 -               break;
20093 -
20094 -       case EVT_CONN_REQUEST:
20095 -               hci_conn_request_evt(hdev, skb);
20096 -               break;
20097 -
20098 -       case EVT_CONN_COMPLETE:
20099 -               hci_conn_complete_evt(hdev, skb);
20100 -               break;
20101 -
20102 -       case EVT_DISCONN_COMPLETE:
20103 -               hci_disconn_complete_evt(hdev, skb);
20104 -               break;
20105 -
20106 -       case EVT_CMD_STATUS:
20107 -               cs = (evt_cmd_status *) skb->data;
20108 -               skb_pull(skb, EVT_CMD_STATUS_SIZE);
20109 -                               
20110 -               opcode = __le16_to_cpu(cs->opcode);
20111 -               ogf = cmd_opcode_ogf(opcode);
20112 -               ocf = cmd_opcode_ocf(opcode);
20113 -
20114 -               switch (ogf) {
20115 -               case OGF_INFO_PARAM:
20116 -                       hci_cs_info_param(hdev, ocf, cs->status);
20117 -                       break;
20118 -
20119 -               case OGF_HOST_CTL:
20120 -                       hci_cs_host_ctl(hdev, ocf, cs->status);
20121 -                       break;
20122 -
20123 -               case OGF_LINK_CTL:
20124 -                       hci_cs_link_ctl(hdev, ocf, cs->status);
20125 -                       break;
20126 -
20127 -               case OGF_LINK_POLICY:
20128 -                       hci_cs_link_policy(hdev, ocf, cs->status);
20129 -                       break;
20130 -
20131 -               default:
20132 -                       DBG("%s Command Status OGF %x", hdev->name, ogf);
20133 -                       break;
20134 -               };
20135 -
20136 -               if (cs->ncmd) {
20137 -                       atomic_set(&hdev->cmd_cnt, 1);
20138 -                       if (!skb_queue_empty(&hdev->cmd_q))
20139 -                               hci_sched_cmd(hdev);
20140 -               }
20141 -               break;
20142 -
20143 -       case EVT_CMD_COMPLETE:
20144 -               ec = (evt_cmd_complete *) skb->data;
20145 -               skb_pull(skb, EVT_CMD_COMPLETE_SIZE);
20146 -
20147 -               opcode = __le16_to_cpu(ec->opcode);
20148 -               ogf = cmd_opcode_ogf(opcode);
20149 -               ocf = cmd_opcode_ocf(opcode);
20150 -
20151 -               switch (ogf) {
20152 -               case OGF_INFO_PARAM:
20153 -                       hci_cc_info_param(hdev, ocf, skb);
20154 -                       break;
20155 -
20156 -               case OGF_HOST_CTL:
20157 -                       hci_cc_host_ctl(hdev, ocf, skb);
20158 -                       break;
20159 -
20160 -               case OGF_LINK_CTL:
20161 -                       hci_cc_link_ctl(hdev, ocf, skb);
20162 -                       break;
20163 -
20164 -               case OGF_LINK_POLICY:
20165 -                       hci_cc_link_policy(hdev, ocf, skb);
20166 -                       break;
20167 -
20168 -               default:
20169 -                       DBG("%s Command Completed OGF %x", hdev->name, ogf);
20170 -                       break;
20171 -               };
20172 -
20173 -               if (ec->ncmd) {
20174 -                       atomic_set(&hdev->cmd_cnt, 1);
20175 -                       if (!skb_queue_empty(&hdev->cmd_q))
20176 -                               hci_sched_cmd(hdev);
20177 -               }
20178 -               break;
20179 -       };
20180 -
20181 -       kfree_skb(skb);
20182 -       hdev->stat.evt_rx++;
20183 -}
20184 +/* ----- HCI RX task (incomming data proccessing) ----- */
20185  
20186  /* ACL data packet */
20187  static inline void hci_acldata_packet(struct hci_dev *hdev, struct sk_buff *skb)
20188 @@ -1867,51 +1255,86 @@
20189         flags  = acl_flags(handle);
20190         handle = acl_handle(handle);
20191  
20192 -       DBG("%s len %d handle 0x%x flags 0x%x", hdev->name, skb->len, handle, flags);
20193 +       BT_DBG("%s len %d handle 0x%x flags 0x%x", hdev->name, skb->len, handle, flags);
20194 +
20195 +       hdev->stat.acl_rx++;
20196  
20197 -       if ((conn = conn_hash_lookup(&hdev->conn_hash, handle))) {
20198 +       hci_dev_lock(hdev);
20199 +       conn = conn_hash_lookup_handle(hdev, handle);
20200 +       hci_dev_unlock(hdev);
20201 +       
20202 +       if (conn) {
20203                 register struct hci_proto *hp;
20204  
20205                 /* Send to upper protocol */
20206 -               if ((hp = GET_HPROTO(HCI_PROTO_L2CAP)) && hp->recv_acldata) {
20207 +               if ((hp = hci_proto[HCI_PROTO_L2CAP]) && hp->recv_acldata) {
20208                         hp->recv_acldata(conn, skb, flags);
20209 -                       goto sent;
20210 +                       return;
20211                 }
20212         } else {
20213 -               ERR("%s ACL packet for unknown connection handle %d", hdev->name, handle);
20214 +               BT_ERR("%s ACL packet for unknown connection handle %d", 
20215 +                       hdev->name, handle);
20216         }
20217  
20218         kfree_skb(skb);
20219 -sent:
20220 -       hdev->stat.acl_rx++;
20221  }
20222  
20223  /* SCO data packet */
20224  static inline void hci_scodata_packet(struct hci_dev *hdev, struct sk_buff *skb)
20225  {
20226 -       DBG("%s len %d", hdev->name, skb->len);
20227 +       hci_sco_hdr *sh = (void *) skb->data;
20228 +       struct hci_conn *conn;
20229 +       __u16 handle;
20230 +
20231 +       skb_pull(skb, HCI_SCO_HDR_SIZE);
20232 +
20233 +       handle = __le16_to_cpu(sh->handle);
20234 +
20235 +       BT_DBG("%s len %d handle 0x%x", hdev->name, skb->len, handle);
20236  
20237 -       kfree_skb(skb);
20238         hdev->stat.sco_rx++;
20239 +
20240 +       hci_dev_lock(hdev);
20241 +       conn = conn_hash_lookup_handle(hdev, handle);
20242 +       hci_dev_unlock(hdev);
20243 +       
20244 +       if (conn) {
20245 +               register struct hci_proto *hp;
20246 +
20247 +               /* Send to upper protocol */
20248 +               if ((hp = hci_proto[HCI_PROTO_SCO]) && hp->recv_scodata) {
20249 +                       hp->recv_scodata(conn, skb);
20250 +                       return;
20251 +               }
20252 +       } else {
20253 +               BT_ERR("%s SCO packet for unknown connection handle %d", 
20254 +                       hdev->name, handle);
20255 +       }
20256 +
20257 +       kfree_skb(skb);
20258  }
20259  
20260 -/* ----- HCI tasks ----- */
20261  void hci_rx_task(unsigned long arg)
20262  {
20263         struct hci_dev *hdev = (struct hci_dev *) arg;
20264         struct sk_buff *skb;
20265  
20266 -       DBG("%s", hdev->name);
20267 +       BT_DBG("%s", hdev->name);
20268  
20269         read_lock(&hci_task_lock);
20270  
20271         while ((skb = skb_dequeue(&hdev->rx_q))) {
20272 -               if (hdev->flags & HCI_SOCK) {
20273 +               if (atomic_read(&hdev->promisc)) {
20274                         /* Send copy to the sockets */
20275                         hci_send_to_sock(hdev, skb);
20276                 }
20277  
20278 -               if (hdev->flags & HCI_INIT) {
20279 +               if (test_bit(HCI_RAW, &hdev->flags)) {
20280 +                       kfree_skb(skb);
20281 +                       continue;
20282 +               }
20283 +
20284 +               if (test_bit(HCI_INIT, &hdev->flags)) {
20285                         /* Don't process data packets in this states. */
20286                         switch (skb->pkt_type) {
20287                         case HCI_ACLDATA_PKT:
20288 @@ -1921,64 +1344,43 @@
20289                         };
20290                 }
20291  
20292 -               if (hdev->flags & HCI_NORMAL) {
20293 -                       /* Process frame */
20294 -                       switch (skb->pkt_type) {
20295 -                       case HCI_EVENT_PKT:
20296 -                               hci_event_packet(hdev, skb);
20297 -                               break;
20298 +               /* Process frame */
20299 +               switch (skb->pkt_type) {
20300 +               case HCI_EVENT_PKT:
20301 +                       hci_event_packet(hdev, skb);
20302 +                       break;
20303  
20304 -                       case HCI_ACLDATA_PKT:
20305 -                               DBG("%s ACL data packet", hdev->name);
20306 -                               hci_acldata_packet(hdev, skb);
20307 -                               break;
20308 +               case HCI_ACLDATA_PKT:
20309 +                       BT_DBG("%s ACL data packet", hdev->name);
20310 +                       hci_acldata_packet(hdev, skb);
20311 +                       break;
20312  
20313 -                       case HCI_SCODATA_PKT:
20314 -                               DBG("%s SCO data packet", hdev->name);
20315 -                               hci_scodata_packet(hdev, skb);
20316 -                               break;
20317 +               case HCI_SCODATA_PKT:
20318 +                       BT_DBG("%s SCO data packet", hdev->name);
20319 +                       hci_scodata_packet(hdev, skb);
20320 +                       break;
20321  
20322 -                       default:
20323 -                               kfree_skb(skb);
20324 -                               break;
20325 -                       };
20326 -               } else {
20327 +               default:
20328                         kfree_skb(skb);
20329 +                       break;
20330                 }
20331         }
20332  
20333         read_unlock(&hci_task_lock);
20334  }
20335  
20336 -static void hci_tx_task(unsigned long arg)
20337 -{
20338 -       struct hci_dev *hdev = (struct hci_dev *) arg;
20339 -       struct sk_buff *skb;
20340 -
20341 -       read_lock(&hci_task_lock);
20342 -
20343 -       DBG("%s acl %d sco %d", hdev->name, hdev->acl_cnt, hdev->sco_cnt);
20344 -
20345 -       /* Schedule queues and send stuff to HCI driver */
20346 -
20347 -       hci_sched_acl(hdev);
20348 -
20349 -       hci_sched_sco(hdev);
20350 -
20351 -       /* Send next queued raw (unknown type) packet */
20352 -       while ((skb = skb_dequeue(&hdev->raw_q)))
20353 -               hci_send_frame(skb);
20354 -
20355 -       read_unlock(&hci_task_lock);
20356 -}
20357 -
20358  static void hci_cmd_task(unsigned long arg)
20359  {
20360         struct hci_dev *hdev = (struct hci_dev *) arg;
20361         struct sk_buff *skb;
20362  
20363 -       DBG("%s cmd %d", hdev->name, atomic_read(&hdev->cmd_cnt));
20364 +       BT_DBG("%s cmd %d", hdev->name, atomic_read(&hdev->cmd_cnt));
20365  
20366 +       if (!atomic_read(&hdev->cmd_cnt) && (jiffies - hdev->cmd_last_tx) > HZ) {
20367 +               BT_ERR("%s command tx timeout", hdev->name);
20368 +               atomic_set(&hdev->cmd_cnt, 1);
20369 +       }
20370 +       
20371         /* Send queued commands */
20372         if (atomic_read(&hdev->cmd_cnt) && (skb = skb_dequeue(&hdev->cmd_q))) {
20373                 if (hdev->sent_cmd)
20374 @@ -1987,6 +1389,7 @@
20375                 if ((hdev->sent_cmd = skb_clone(skb, GFP_ATOMIC))) {
20376                         atomic_dec(&hdev->cmd_cnt);
20377                         hci_send_frame(skb);
20378 +                       hdev->cmd_last_tx = jiffies;
20379                 } else {
20380                         skb_queue_head(&hdev->cmd_q, skb);
20381                         hci_sched_cmd(hdev);
20382 @@ -1994,33 +1397,10 @@
20383         }
20384  }
20385  
20386 -/* Receive frame from HCI drivers */
20387 -int hci_recv_frame(struct sk_buff *skb)
20388 -{
20389 -       struct hci_dev *hdev = (struct hci_dev *) skb->dev;
20390 -
20391 -       if (!hdev || !(hdev->flags & (HCI_UP | HCI_INIT))) {
20392 -               kfree_skb(skb);
20393 -               return -1;
20394 -       }
20395 -
20396 -       DBG("%s type %d len %d", hdev->name, skb->pkt_type, skb->len);
20397 -
20398 -       /* Incomming skb */
20399 -       bluez_cb(skb)->incomming = 1;
20400 -
20401 -       /* Queue frame for rx task */
20402 -       skb_queue_tail(&hdev->rx_q, skb);
20403 -       hci_sched_rx(hdev);
20404 -
20405 -       return 0;
20406 -}
20407 +/* ---- Initialization ---- */
20408  
20409  int hci_core_init(void)
20410  {
20411 -       /* Init locks */
20412 -       spin_lock_init(&hdev_list_lock);
20413 -
20414         return 0;
20415  }
20416  
20417 @@ -2028,5 +1408,3 @@
20418  {
20419         return 0;
20420  }
20421 -
20422 -MODULE_LICENSE("GPL");
20423 diff -urN linux-2.4.18/net/bluetooth/hci_event.c linux-2.4.18-mh15/net/bluetooth/hci_event.c
20424 --- linux-2.4.18/net/bluetooth/hci_event.c      1970-01-01 01:00:00.000000000 +0100
20425 +++ linux-2.4.18-mh15/net/bluetooth/hci_event.c 2004-08-01 16:26:23.000000000 +0200
20426 @@ -0,0 +1,910 @@
20427 +/* 
20428 +   BlueZ - Bluetooth protocol stack for Linux
20429 +   Copyright (C) 2000-2001 Qualcomm Incorporated
20430 +
20431 +   Written 2000,2001 by Maxim Krasnyansky <maxk@qualcomm.com>
20432 +
20433 +   This program is free software; you can redistribute it and/or modify
20434 +   it under the terms of the GNU General Public License version 2 as
20435 +   published by the Free Software Foundation;
20436 +
20437 +   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
20438 +   OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20439 +   FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS.
20440 +   IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) AND AUTHOR(S) BE LIABLE FOR ANY
20441 +   CLAIM, OR ANY SPECIAL INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES 
20442 +   WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 
20443 +   ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 
20444 +   OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
20445 +
20446 +   ALL LIABILITY, INCLUDING LIABILITY FOR INFRINGEMENT OF ANY PATENTS, 
20447 +   COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS, RELATING TO USE OF THIS 
20448 +   SOFTWARE IS DISCLAIMED.
20449 +*/
20450 +
20451 +/*
20452 + * HCI Events.
20453 + *
20454 + * $Id: hci_event.c,v 1.4 2002/07/27 18:14:38 maxk Exp $
20455 + */
20456 +
20457 +#include <linux/config.h>
20458 +#include <linux/module.h>
20459 +
20460 +#include <linux/types.h>
20461 +#include <linux/errno.h>
20462 +#include <linux/kernel.h>
20463 +#include <linux/major.h>
20464 +#include <linux/sched.h>
20465 +#include <linux/slab.h>
20466 +#include <linux/poll.h>
20467 +#include <linux/fcntl.h>
20468 +#include <linux/init.h>
20469 +#include <linux/skbuff.h>
20470 +#include <linux/interrupt.h>
20471 +#include <linux/notifier.h>
20472 +#include <net/sock.h>
20473 +
20474 +#include <asm/system.h>
20475 +#include <asm/uaccess.h>
20476 +#include <asm/unaligned.h>
20477 +
20478 +#include <net/bluetooth/bluetooth.h>
20479 +#include <net/bluetooth/hci_core.h>
20480 +
20481 +#ifndef HCI_CORE_DEBUG
20482 +#undef  BT_DBG
20483 +#define BT_DBG( A... )
20484 +#endif
20485 +
20486 +/* Handle HCI Event packets */
20487 +
20488 +/* Command Complete OGF LINK_CTL  */
20489 +static void hci_cc_link_ctl(struct hci_dev *hdev, __u16 ocf, struct sk_buff *skb)
20490 +{
20491 +       __u8 status;
20492 +
20493 +       BT_DBG("%s ocf 0x%x", hdev->name, ocf);
20494 +
20495 +       switch (ocf) {
20496 +       case OCF_INQUIRY_CANCEL:
20497 +               status = *((__u8 *) skb->data);
20498 +
20499 +               if (status) {
20500 +                       BT_DBG("%s Inquiry cancel error: status 0x%x", hdev->name, status);
20501 +               } else {
20502 +                       clear_bit(HCI_INQUIRY, &hdev->flags);
20503 +                       hci_req_complete(hdev, status);
20504 +               }
20505 +               break;
20506 +
20507 +       default:
20508 +               BT_DBG("%s Command complete: ogf LINK_CTL ocf %x", hdev->name, ocf);
20509 +               break;
20510 +       };
20511 +}
20512 +
20513 +/* Command Complete OGF LINK_POLICY  */
20514 +static void hci_cc_link_policy(struct hci_dev *hdev, __u16 ocf, struct sk_buff *skb)
20515 +{
20516 +       struct hci_conn *conn;
20517 +       role_discovery_rp *rd;
20518 +
20519 +       BT_DBG("%s ocf 0x%x", hdev->name, ocf);
20520 +
20521 +       switch (ocf) {
20522 +       case OCF_ROLE_DISCOVERY: 
20523 +               rd = (void *) skb->data;
20524 +
20525 +               if (rd->status)
20526 +                       break;
20527 +               
20528 +               hci_dev_lock(hdev);
20529 +       
20530 +               conn = conn_hash_lookup_handle(hdev, __le16_to_cpu(rd->handle));
20531 +               if (conn) {
20532 +                       if (rd->role)
20533 +                               conn->link_mode &= ~HCI_LM_MASTER;
20534 +                       else
20535 +                               conn->link_mode |= HCI_LM_MASTER;
20536 +               }
20537 +                       
20538 +               hci_dev_unlock(hdev);
20539 +               break;
20540 +
20541 +       default:
20542 +               BT_DBG("%s: Command complete: ogf LINK_POLICY ocf %x", 
20543 +                               hdev->name, ocf);
20544 +               break;
20545 +       };
20546 +}
20547 +
20548 +/* Command Complete OGF HOST_CTL  */
20549 +static void hci_cc_host_ctl(struct hci_dev *hdev, __u16 ocf, struct sk_buff *skb)
20550 +{
20551 +       __u8 status, param;
20552 +       void *sent;
20553 +
20554 +       BT_DBG("%s ocf 0x%x", hdev->name, ocf);
20555 +
20556 +       switch (ocf) {
20557 +       case OCF_RESET:
20558 +               status = *((__u8 *) skb->data);
20559 +               hci_req_complete(hdev, status);
20560 +               break;
20561 +
20562 +       case OCF_SET_EVENT_FLT:
20563 +               status = *((__u8 *) skb->data);
20564 +               if (status) {
20565 +                       BT_DBG("%s SET_EVENT_FLT failed %d", hdev->name, status);
20566 +               } else {
20567 +                       BT_DBG("%s SET_EVENT_FLT succeseful", hdev->name);
20568 +               }
20569 +               break;
20570 +
20571 +       case OCF_WRITE_AUTH_ENABLE:
20572 +               sent = hci_sent_cmd_data(hdev, OGF_HOST_CTL, OCF_WRITE_AUTH_ENABLE);
20573 +               if (!sent)
20574 +                       break;
20575 +
20576 +               status = *((__u8 *) skb->data);
20577 +               param  = *((__u8 *) sent);
20578 +
20579 +               if (!status) {
20580 +                       if (param == AUTH_ENABLED)
20581 +                               set_bit(HCI_AUTH, &hdev->flags);
20582 +                       else
20583 +                               clear_bit(HCI_AUTH, &hdev->flags);
20584 +               }
20585 +               hci_req_complete(hdev, status);
20586 +               break;
20587 +
20588 +       case OCF_WRITE_ENCRYPT_MODE:
20589 +               sent = hci_sent_cmd_data(hdev, OGF_HOST_CTL, OCF_WRITE_ENCRYPT_MODE);
20590 +               if (!sent)
20591 +                       break;
20592 +
20593 +               status = *((__u8 *) skb->data);
20594 +               param  = *((__u8 *) sent);
20595 +
20596 +               if (!status) {
20597 +                       if (param)
20598 +                               set_bit(HCI_ENCRYPT, &hdev->flags);
20599 +                       else
20600 +                               clear_bit(HCI_ENCRYPT, &hdev->flags);
20601 +               }
20602 +               hci_req_complete(hdev, status);
20603 +               break;
20604 +
20605 +       case OCF_WRITE_CA_TIMEOUT:
20606 +               status = *((__u8 *) skb->data);
20607 +               if (status) {
20608 +                       BT_DBG("%s OCF_WRITE_CA_TIMEOUT failed %d", hdev->name, status);
20609 +               } else {
20610 +                       BT_DBG("%s OCF_WRITE_CA_TIMEOUT succeseful", hdev->name);
20611 +               }
20612 +               break;
20613 +
20614 +       case OCF_WRITE_PG_TIMEOUT:
20615 +               status = *((__u8 *) skb->data);
20616 +               if (status) {
20617 +                       BT_DBG("%s OCF_WRITE_PG_TIMEOUT failed %d", hdev->name, status);
20618 +               } else {
20619 +                       BT_DBG("%s: OCF_WRITE_PG_TIMEOUT succeseful", hdev->name);
20620 +               }
20621 +               break;
20622 +
20623 +       case OCF_WRITE_SCAN_ENABLE:
20624 +               sent = hci_sent_cmd_data(hdev, OGF_HOST_CTL, OCF_WRITE_SCAN_ENABLE);
20625 +               if (!sent)
20626 +                       break;
20627 +               status = *((__u8 *) skb->data);
20628 +               param  = *((__u8 *) sent);
20629 +
20630 +               BT_DBG("param 0x%x", param);
20631 +
20632 +               if (!status) {
20633 +                       clear_bit(HCI_PSCAN, &hdev->flags);
20634 +                       clear_bit(HCI_ISCAN, &hdev->flags);
20635 +                       if (param & SCAN_INQUIRY) 
20636 +                               set_bit(HCI_ISCAN, &hdev->flags);
20637 +
20638 +                       if (param & SCAN_PAGE) 
20639 +                               set_bit(HCI_PSCAN, &hdev->flags);
20640 +               }
20641 +               hci_req_complete(hdev, status);
20642 +               break;
20643 +
20644 +       case OCF_HOST_BUFFER_SIZE:
20645 +               status = *((__u8 *) skb->data);
20646 +               if (status) {
20647 +                       BT_DBG("%s OCF_BUFFER_SIZE failed %d", hdev->name, status);
20648 +                       hci_req_complete(hdev, status);
20649 +               }
20650 +               break;
20651 +
20652 +       default:
20653 +               BT_DBG("%s Command complete: ogf HOST_CTL ocf %x", hdev->name, ocf);
20654 +               break;
20655 +       };
20656 +}
20657 +
20658 +/* Command Complete OGF INFO_PARAM  */
20659 +static void hci_cc_info_param(struct hci_dev *hdev, __u16 ocf, struct sk_buff *skb)
20660 +{
20661 +       read_local_features_rp *lf;
20662 +       read_buffer_size_rp *bs;
20663 +       read_bd_addr_rp *ba;
20664 +
20665 +       BT_DBG("%s ocf 0x%x", hdev->name, ocf);
20666 +
20667 +       switch (ocf) {
20668 +       case OCF_READ_LOCAL_FEATURES:
20669 +               lf = (read_local_features_rp *) skb->data;
20670 +
20671 +               if (lf->status) {
20672 +                       BT_DBG("%s READ_LOCAL_FEATURES failed %d", hdev->name, lf->status);
20673 +                       break;
20674 +               }
20675 +
20676 +               memcpy(hdev->features, lf->features, sizeof(hdev->features));
20677 +
20678 +               /* Adjust default settings according to features 
20679 +                * supported by device. */
20680 +               if (hdev->features[0] & LMP_3SLOT)
20681 +                       hdev->pkt_type |= (HCI_DM3 | HCI_DH3);
20682 +
20683 +               if (hdev->features[0] & LMP_5SLOT)
20684 +                       hdev->pkt_type |= (HCI_DM5 | HCI_DH5);
20685 +
20686 +               if (hdev->features[1] & LMP_HV2)
20687 +                       hdev->pkt_type |= (HCI_HV2);
20688 +
20689 +               if (hdev->features[1] & LMP_HV3)
20690 +                       hdev->pkt_type |= (HCI_HV3);
20691 +
20692 +               BT_DBG("%s: features 0x%x 0x%x 0x%x", hdev->name, lf->features[0], lf->features[1], lf->features[2]);
20693 +
20694 +               break;
20695 +
20696 +       case OCF_READ_BUFFER_SIZE:
20697 +               bs = (read_buffer_size_rp *) skb->data;
20698 +
20699 +               if (bs->status) {
20700 +                       BT_DBG("%s READ_BUFFER_SIZE failed %d", hdev->name, bs->status);
20701 +                       hci_req_complete(hdev, bs->status);
20702 +                       break;
20703 +               }
20704 +
20705 +               hdev->acl_mtu  = __le16_to_cpu(bs->acl_mtu);
20706 +               hdev->sco_mtu  = bs->sco_mtu ? bs->sco_mtu : 64;
20707 +               hdev->acl_pkts = hdev->acl_cnt = __le16_to_cpu(bs->acl_max_pkt);
20708 +               hdev->sco_pkts = hdev->sco_cnt = __le16_to_cpu(bs->sco_max_pkt);
20709 +
20710 +               BT_DBG("%s mtu: acl %d, sco %d max_pkt: acl %d, sco %d", hdev->name,
20711 +                   hdev->acl_mtu, hdev->sco_mtu, hdev->acl_pkts, hdev->sco_pkts);
20712 +               break;
20713 +
20714 +       case OCF_READ_BD_ADDR:
20715 +               ba = (read_bd_addr_rp *) skb->data;
20716 +
20717 +               if (!ba->status) {
20718 +                       bacpy(&hdev->bdaddr, &ba->bdaddr);
20719 +               } else {
20720 +                       BT_DBG("%s: READ_BD_ADDR failed %d", hdev->name, ba->status);
20721 +               }
20722 +
20723 +               hci_req_complete(hdev, ba->status);
20724 +               break;
20725 +
20726 +       default:
20727 +               BT_DBG("%s Command complete: ogf INFO_PARAM ocf %x", hdev->name, ocf);
20728 +               break;
20729 +       };
20730 +}
20731 +
20732 +/* Command Status OGF LINK_CTL  */
20733 +static inline void hci_cs_create_conn(struct hci_dev *hdev, __u8 status)
20734 +{
20735 +       struct hci_conn *conn;
20736 +       create_conn_cp *cc = hci_sent_cmd_data(hdev, OGF_LINK_CTL, OCF_CREATE_CONN);
20737 +
20738 +       if (!cc)
20739 +               return;
20740 +
20741 +       hci_dev_lock(hdev);
20742 +       
20743 +       conn = conn_hash_lookup_ba(hdev, ACL_LINK, &cc->bdaddr);
20744 +
20745 +       BT_DBG("%s status 0x%x bdaddr %s conn %p", hdev->name, 
20746 +                       status, batostr(&cc->bdaddr), conn);
20747 +
20748 +       if (status) {
20749 +               if (conn && conn->state == BT_CONNECT) {
20750 +                       conn->state = BT_CLOSED;
20751 +                       hci_proto_connect_cfm(conn, status);
20752 +                       hci_conn_del(conn);
20753 +               }
20754 +       } else {
20755 +               if (!conn) {
20756 +                       conn = hci_conn_add(hdev, ACL_LINK, &cc->bdaddr);
20757 +                       if (conn) {
20758 +                               conn->out = 1;
20759 +                               conn->link_mode |= HCI_LM_MASTER;
20760 +                       } else
20761 +                               BT_ERR("No memmory for new connection");
20762 +               }
20763 +       }
20764 +
20765 +       hci_dev_unlock(hdev);
20766 +}
20767 +
20768 +static void hci_cs_link_ctl(struct hci_dev *hdev, __u16 ocf, __u8 status)
20769 +{
20770 +       BT_DBG("%s ocf 0x%x", hdev->name, ocf);
20771 +
20772 +       switch (ocf) {
20773 +       case OCF_CREATE_CONN:
20774 +               hci_cs_create_conn(hdev, status);
20775 +               break;
20776 +
20777 +       case OCF_ADD_SCO:
20778 +               if (status) {
20779 +                       struct hci_conn *acl, *sco;
20780 +                       add_sco_cp *cp = hci_sent_cmd_data(hdev, 
20781 +                                               OGF_LINK_CTL, OCF_ADD_SCO);
20782 +                       __u16 handle;
20783 +
20784 +                       if (!cp)
20785 +                               break;
20786 +
20787 +                       handle = __le16_to_cpu(cp->handle);
20788 +
20789 +                       BT_DBG("%s Add SCO error: handle %d status 0x%x", hdev->name, handle, status);
20790 +
20791 +                       hci_dev_lock(hdev);
20792 +       
20793 +                       acl = conn_hash_lookup_handle(hdev, handle);
20794 +                       if (acl && (sco = acl->link)) {
20795 +                               sco->state = BT_CLOSED;
20796 +                               hci_proto_connect_cfm(sco, status);
20797 +                               hci_conn_del(sco);
20798 +                       }
20799 +
20800 +                       hci_dev_unlock(hdev);
20801 +               }
20802 +               break;
20803 +
20804 +       case OCF_INQUIRY:
20805 +               if (status) {
20806 +                       BT_DBG("%s Inquiry error: status 0x%x", hdev->name, status);
20807 +                       hci_req_complete(hdev, status);
20808 +               } else {
20809 +                       set_bit(HCI_INQUIRY, &hdev->flags);
20810 +               }
20811 +               break;
20812 +
20813 +       default:
20814 +               BT_DBG("%s Command status: ogf LINK_CTL ocf %x status %d", 
20815 +                       hdev->name, ocf, status);
20816 +               break;
20817 +       };
20818 +}
20819 +
20820 +/* Command Status OGF LINK_POLICY */
20821 +static void hci_cs_link_policy(struct hci_dev *hdev, __u16 ocf, __u8 status)
20822 +{
20823 +       BT_DBG("%s ocf 0x%x", hdev->name, ocf);
20824 +
20825 +       switch (ocf) {
20826 +       default:
20827 +               BT_DBG("%s Command status: ogf HOST_POLICY ocf %x", hdev->name, ocf);
20828 +               break;
20829 +       };
20830 +}
20831 +
20832 +/* Command Status OGF HOST_CTL */
20833 +static void hci_cs_host_ctl(struct hci_dev *hdev, __u16 ocf, __u8 status)
20834 +{
20835 +       BT_DBG("%s ocf 0x%x", hdev->name, ocf);
20836 +
20837 +       switch (ocf) {
20838 +       default:
20839 +               BT_DBG("%s Command status: ogf HOST_CTL ocf %x", hdev->name, ocf);
20840 +               break;
20841 +       };
20842 +}
20843 +
20844 +/* Command Status OGF INFO_PARAM  */
20845 +static void hci_cs_info_param(struct hci_dev *hdev, __u16 ocf, __u8 status)
20846 +{
20847 +       BT_DBG("%s: hci_cs_info_param: ocf 0x%x", hdev->name, ocf);
20848 +
20849 +       switch (ocf) {
20850 +       default:
20851 +               BT_DBG("%s Command status: ogf INFO_PARAM ocf %x", hdev->name, ocf);
20852 +               break;
20853 +       };
20854 +}
20855 +
20856 +/* Inquiry Complete */
20857 +static inline void hci_inquiry_complete_evt(struct hci_dev *hdev, struct sk_buff *skb)
20858 +{
20859 +       __u8 status = *((__u8 *) skb->data);
20860 +
20861 +       BT_DBG("%s status %d", hdev->name, status);
20862 +
20863 +       clear_bit(HCI_INQUIRY, &hdev->flags);
20864 +       hci_req_complete(hdev, status);
20865 +}
20866 +
20867 +/* Inquiry Result */
20868 +static inline void hci_inquiry_result_evt(struct hci_dev *hdev, struct sk_buff *skb)
20869 +{
20870 +       inquiry_info *info = (inquiry_info *) (skb->data + 1);
20871 +       int num_rsp = *((__u8 *) skb->data);
20872 +
20873 +       BT_DBG("%s num_rsp %d", hdev->name, num_rsp);
20874 +
20875 +       hci_dev_lock(hdev);
20876 +       for (; num_rsp; num_rsp--)
20877 +               inquiry_cache_update(hdev, info++);
20878 +       hci_dev_unlock(hdev);
20879 +}
20880 +
20881 +/* Inquiry Result With RSSI */
20882 +static inline void hci_inquiry_result_with_rssi_evt(struct hci_dev *hdev, struct sk_buff *skb)
20883 +{
20884 +       inquiry_info_with_rssi *info = (inquiry_info_with_rssi *) (skb->data + 1);
20885 +       int num_rsp = *((__u8 *) skb->data);
20886 +
20887 +       BT_DBG("%s num_rsp %d", hdev->name, num_rsp);
20888 +
20889 +       hci_dev_lock(hdev);
20890 +       for (; num_rsp; num_rsp--) {
20891 +               inquiry_info tmp;
20892 +               bacpy(&tmp.bdaddr, &info->bdaddr);
20893 +               tmp.pscan_rep_mode    = info->pscan_rep_mode;
20894 +               tmp.pscan_period_mode = info->pscan_period_mode;
20895 +               tmp.pscan_mode        = 0x00;
20896 +               memcpy(tmp.dev_class, &info->dev_class, 3);
20897 +               tmp.clock_offset      = info->clock_offset;
20898 +               info++;
20899 +               inquiry_cache_update(hdev, &tmp);
20900 +       }
20901 +       hci_dev_unlock(hdev);
20902 +}
20903 +
20904 +/* Connect Request */
20905 +static inline void hci_conn_request_evt(struct hci_dev *hdev, struct sk_buff *skb)
20906 +{
20907 +       evt_conn_request *cr = (evt_conn_request *) skb->data;
20908 +       int mask = hdev->link_mode;
20909 +
20910 +       BT_DBG("%s Connection request: %s type 0x%x", hdev->name,
20911 +                       batostr(&cr->bdaddr), cr->link_type);
20912 +
20913 +       mask |= hci_proto_connect_ind(hdev, &cr->bdaddr, cr->link_type);
20914 +
20915 +       if (mask & HCI_LM_ACCEPT) {
20916 +               /* Connection accepted */
20917 +               struct hci_conn *conn;
20918 +               accept_conn_req_cp ac;
20919 +
20920 +               hci_dev_lock(hdev);
20921 +               conn = conn_hash_lookup_ba(hdev, cr->link_type, &cr->bdaddr);
20922 +               if (!conn) {
20923 +                       if (!(conn = hci_conn_add(hdev, cr->link_type, &cr->bdaddr))) {
20924 +                               BT_ERR("No memmory for new connection");
20925 +                               hci_dev_unlock(hdev);
20926 +                               return;
20927 +                       }
20928 +               }
20929 +               conn->state = BT_CONNECT;
20930 +               hci_dev_unlock(hdev);
20931 +
20932 +               bacpy(&ac.bdaddr, &cr->bdaddr);
20933 +       
20934 +               if (lmp_rswitch_capable(hdev) && (mask & HCI_LM_MASTER))
20935 +                       ac.role = 0x00; /* Become master */
20936 +               else
20937 +                       ac.role = 0x01; /* Remain slave */
20938 +
20939 +               hci_send_cmd(hdev, OGF_LINK_CTL, OCF_ACCEPT_CONN_REQ, 
20940 +                               ACCEPT_CONN_REQ_CP_SIZE, &ac);
20941 +       } else {
20942 +               /* Connection rejected */
20943 +               reject_conn_req_cp rc;
20944 +
20945 +               bacpy(&rc.bdaddr, &cr->bdaddr);
20946 +               rc.reason = 0x0f;
20947 +               hci_send_cmd(hdev, OGF_LINK_CTL, OCF_REJECT_CONN_REQ,
20948 +                               REJECT_CONN_REQ_CP_SIZE, &rc);
20949 +       }
20950 +}
20951 +
20952 +/* Connect Complete */
20953 +static inline void hci_conn_complete_evt(struct hci_dev *hdev, struct sk_buff *skb)
20954 +{
20955 +       evt_conn_complete *cc = (evt_conn_complete *) skb->data;
20956 +       struct hci_conn *conn = NULL;
20957 +
20958 +       BT_DBG("%s", hdev->name);
20959 +
20960 +       hci_dev_lock(hdev);
20961 +       
20962 +       conn = conn_hash_lookup_ba(hdev, cc->link_type, &cc->bdaddr);
20963 +       if (!conn) {
20964 +               hci_dev_unlock(hdev);
20965 +               return;
20966 +       }
20967 +
20968 +       if (!cc->status) {
20969 +               conn->handle = __le16_to_cpu(cc->handle);
20970 +               conn->state  = BT_CONNECTED;
20971 +
20972 +               if (test_bit(HCI_AUTH, &hdev->flags))
20973 +                       conn->link_mode |= HCI_LM_AUTH;
20974 +               
20975 +               if (test_bit(HCI_ENCRYPT, &hdev->flags))
20976 +                       conn->link_mode |= HCI_LM_ENCRYPT;
20977 +
20978 +
20979 +               /* Set link policy */
20980 +               if (conn->type == ACL_LINK && hdev->link_policy) {
20981 +                       write_link_policy_cp lp;
20982 +                       lp.handle = cc->handle;
20983 +                       lp.policy = __cpu_to_le16(hdev->link_policy);
20984 +                       hci_send_cmd(hdev, OGF_LINK_POLICY, OCF_WRITE_LINK_POLICY,
20985 +                               WRITE_LINK_POLICY_CP_SIZE, &lp);
20986 +               }
20987 +
20988 +               /* Set packet type for incomming connection */
20989 +               if (!conn->out) {
20990 +                       change_conn_ptype_cp cp;
20991 +                       cp.handle = cc->handle;
20992 +                       cp.pkt_type = (conn->type == ACL_LINK) ? 
20993 +                               __cpu_to_le16(hdev->pkt_type & ACL_PTYPE_MASK):
20994 +                               __cpu_to_le16(hdev->pkt_type & SCO_PTYPE_MASK);
20995 +
20996 +                       hci_send_cmd(hdev, OGF_LINK_CTL, OCF_CHANGE_CONN_PTYPE,
20997 +                               CHANGE_CONN_PTYPE_CP_SIZE, &cp);
20998 +               }
20999 +       } else
21000 +               conn->state = BT_CLOSED;
21001 +
21002 +       if (conn->type == ACL_LINK) {
21003 +               struct hci_conn *sco = conn->link;
21004 +               if (sco) {
21005 +                       if (!cc->status)
21006 +                               hci_add_sco(sco, conn->handle);
21007 +                       else {
21008 +                               hci_proto_connect_cfm(sco, cc->status);
21009 +                               hci_conn_del(sco);
21010 +                       }
21011 +               }
21012 +       }
21013 +
21014 +       hci_proto_connect_cfm(conn, cc->status);
21015 +       if (cc->status)
21016 +               hci_conn_del(conn);
21017 +
21018 +       hci_dev_unlock(hdev);
21019 +}
21020 +
21021 +/* Disconnect Complete */
21022 +static inline void hci_disconn_complete_evt(struct hci_dev *hdev, struct sk_buff *skb)
21023 +{
21024 +       evt_disconn_complete *dc = (evt_disconn_complete *) skb->data;
21025 +       struct hci_conn *conn = NULL;
21026 +       __u16 handle = __le16_to_cpu(dc->handle);
21027 +
21028 +       BT_DBG("%s status %d", hdev->name, dc->status);
21029 +
21030 +       if (dc->status)
21031 +               return;
21032 +
21033 +       hci_dev_lock(hdev);
21034 +       
21035 +       conn = conn_hash_lookup_handle(hdev, handle);
21036 +       if (conn) {
21037 +               conn->state = BT_CLOSED;
21038 +               hci_proto_disconn_ind(conn, dc->reason);
21039 +               hci_conn_del(conn);
21040 +       }
21041 +
21042 +       hci_dev_unlock(hdev);
21043 +}
21044 +
21045 +/* Number of completed packets */
21046 +static inline void hci_num_comp_pkts_evt(struct hci_dev *hdev, struct sk_buff *skb)
21047 +{
21048 +       evt_num_comp_pkts *nc = (evt_num_comp_pkts *) skb->data;
21049 +       __u16 *ptr;
21050 +       int i;
21051 +
21052 +       skb_pull(skb, EVT_NUM_COMP_PKTS_SIZE);
21053 +
21054 +       BT_DBG("%s num_hndl %d", hdev->name, nc->num_hndl);
21055 +
21056 +       if (skb->len < nc->num_hndl * 4) {
21057 +               BT_DBG("%s bad parameters", hdev->name);
21058 +               return;
21059 +       }
21060 +
21061 +       tasklet_disable(&hdev->tx_task);
21062 +
21063 +       for (i = 0, ptr = (__u16 *) skb->data; i < nc->num_hndl; i++) {
21064 +               struct hci_conn *conn;
21065 +               __u16  handle, count;
21066 +
21067 +               handle = __le16_to_cpu(get_unaligned(ptr++));
21068 +               count  = __le16_to_cpu(get_unaligned(ptr++));
21069 +
21070 +               conn = conn_hash_lookup_handle(hdev, handle);
21071 +               if (conn) {
21072 +                       conn->sent -= count;
21073 +
21074 +                       if (conn->type == SCO_LINK) {
21075 +                               if ((hdev->sco_cnt += count) > hdev->sco_pkts)
21076 +                                       hdev->sco_cnt = hdev->sco_pkts;
21077 +                       } else {
21078 +                               if ((hdev->acl_cnt += count) > hdev->acl_pkts)
21079 +                                       hdev->acl_cnt = hdev->acl_pkts;
21080 +                       }
21081 +               }
21082 +       }
21083 +       hci_sched_tx(hdev);
21084 +
21085 +       tasklet_enable(&hdev->tx_task);
21086 +}
21087 +
21088 +/* Role Change */
21089 +static inline void hci_role_change_evt(struct hci_dev *hdev, struct sk_buff *skb)
21090 +{
21091 +       evt_role_change *rc = (evt_role_change *) skb->data;
21092 +       struct hci_conn *conn = NULL;
21093 +
21094 +       BT_DBG("%s status %d", hdev->name, rc->status);
21095 +
21096 +       if (rc->status)
21097 +               return;
21098 +
21099 +       hci_dev_lock(hdev);
21100 +       
21101 +       conn = conn_hash_lookup_ba(hdev, ACL_LINK, &rc->bdaddr);
21102 +       if (conn) {
21103 +               if (rc->role)
21104 +                       conn->link_mode &= ~HCI_LM_MASTER;
21105 +               else 
21106 +                       conn->link_mode |= HCI_LM_MASTER;
21107 +       }
21108 +
21109 +       hci_dev_unlock(hdev);
21110 +}
21111 +
21112 +/* Authentication Complete */
21113 +static inline void hci_auth_complete_evt(struct hci_dev *hdev, struct sk_buff *skb)
21114 +{
21115 +       evt_auth_complete *ac = (evt_auth_complete *) skb->data;
21116 +       struct hci_conn *conn = NULL;
21117 +       __u16 handle = __le16_to_cpu(ac->handle);
21118 +
21119 +       BT_DBG("%s status %d", hdev->name, ac->status);
21120 +
21121 +       hci_dev_lock(hdev);
21122 +       
21123 +       conn = conn_hash_lookup_handle(hdev, handle);
21124 +       if (conn) {
21125 +               if (!ac->status)
21126 +                       conn->link_mode |= HCI_LM_AUTH;
21127 +               clear_bit(HCI_CONN_AUTH_PEND, &conn->pend);
21128 +
21129 +               hci_proto_auth_cfm(conn, ac->status);
21130 +               
21131 +               if (test_bit(HCI_CONN_ENCRYPT_PEND, &conn->pend)) {
21132 +                       if (!ac->status) {
21133 +                               set_conn_encrypt_cp ce;
21134 +                               ce.handle  = __cpu_to_le16(conn->handle);
21135 +                               ce.encrypt = 1;
21136 +                               hci_send_cmd(conn->hdev, OGF_LINK_CTL,
21137 +                                               OCF_SET_CONN_ENCRYPT,
21138 +                                               SET_CONN_ENCRYPT_CP_SIZE, &ce);
21139 +                       } else {
21140 +                               clear_bit(HCI_CONN_ENCRYPT_PEND, &conn->pend);
21141 +                               hci_proto_encrypt_cfm(conn, ac->status);
21142 +                       }
21143 +               }
21144 +       }
21145 +
21146 +       hci_dev_unlock(hdev);
21147 +}
21148 +
21149 +/* Encryption Change */
21150 +static inline void hci_encrypt_change_evt(struct hci_dev *hdev, struct sk_buff *skb)
21151 +{
21152 +       evt_encrypt_change *ec = (evt_encrypt_change *) skb->data;
21153 +       struct hci_conn *conn = NULL;
21154 +       __u16 handle = __le16_to_cpu(ec->handle);
21155 +
21156 +       BT_DBG("%s status %d", hdev->name, ec->status);
21157 +
21158 +       hci_dev_lock(hdev);
21159 +       
21160 +       conn = conn_hash_lookup_handle(hdev, handle);
21161 +       if (conn) {
21162 +               if (!ec->status) {
21163 +                       if (ec->encrypt)
21164 +                               conn->link_mode |= HCI_LM_ENCRYPT;
21165 +                       else
21166 +                               conn->link_mode &= ~HCI_LM_ENCRYPT;
21167 +               }
21168 +               clear_bit(HCI_CONN_ENCRYPT_PEND, &conn->pend);
21169 +               
21170 +               hci_proto_encrypt_cfm(conn, ec->status);
21171 +       }
21172 +
21173 +       hci_dev_unlock(hdev);
21174 +}
21175 +
21176 +void hci_event_packet(struct hci_dev *hdev, struct sk_buff *skb)
21177 +{
21178 +       hci_event_hdr *he = (hci_event_hdr *) skb->data;
21179 +       evt_cmd_status *cs;
21180 +       evt_cmd_complete *ec;
21181 +       __u16 opcode, ocf, ogf;
21182 +
21183 +       skb_pull(skb, HCI_EVENT_HDR_SIZE);
21184 +
21185 +       BT_DBG("%s evt 0x%x", hdev->name, he->evt);
21186 +
21187 +       switch (he->evt) {
21188 +       case EVT_NUM_COMP_PKTS:
21189 +               hci_num_comp_pkts_evt(hdev, skb);
21190 +               break;
21191 +
21192 +       case EVT_INQUIRY_COMPLETE:
21193 +               hci_inquiry_complete_evt(hdev, skb);
21194 +               break;
21195 +
21196 +       case EVT_INQUIRY_RESULT:
21197 +               hci_inquiry_result_evt(hdev, skb);
21198 +               break;
21199 +
21200 +       case EVT_INQUIRY_RESULT_WITH_RSSI:
21201 +               hci_inquiry_result_with_rssi_evt(hdev, skb);
21202 +               break;
21203 +
21204 +       case EVT_CONN_REQUEST:
21205 +               hci_conn_request_evt(hdev, skb);
21206 +               break;
21207 +
21208 +       case EVT_CONN_COMPLETE:
21209 +               hci_conn_complete_evt(hdev, skb);
21210 +               break;
21211 +
21212 +       case EVT_DISCONN_COMPLETE:
21213 +               hci_disconn_complete_evt(hdev, skb);
21214 +               break;
21215 +
21216 +       case EVT_ROLE_CHANGE:
21217 +               hci_role_change_evt(hdev, skb);
21218 +               break;
21219 +
21220 +       case EVT_AUTH_COMPLETE:
21221 +               hci_auth_complete_evt(hdev, skb);
21222 +               break;
21223 +
21224 +       case EVT_ENCRYPT_CHANGE:
21225 +               hci_encrypt_change_evt(hdev, skb);
21226 +               break;
21227 +
21228 +       case EVT_CMD_STATUS:
21229 +               cs = (evt_cmd_status *) skb->data;
21230 +               skb_pull(skb, EVT_CMD_STATUS_SIZE);
21231 +                               
21232 +               opcode = __le16_to_cpu(cs->opcode);
21233 +               ogf = cmd_opcode_ogf(opcode);
21234 +               ocf = cmd_opcode_ocf(opcode);
21235 +
21236 +               switch (ogf) {
21237 +               case OGF_INFO_PARAM:
21238 +                       hci_cs_info_param(hdev, ocf, cs->status);
21239 +                       break;
21240 +
21241 +               case OGF_HOST_CTL:
21242 +                       hci_cs_host_ctl(hdev, ocf, cs->status);
21243 +                       break;
21244 +
21245 +               case OGF_LINK_CTL:
21246 +                       hci_cs_link_ctl(hdev, ocf, cs->status);
21247 +                       break;
21248 +
21249 +               case OGF_LINK_POLICY:
21250 +                       hci_cs_link_policy(hdev, ocf, cs->status);
21251 +                       break;
21252 +
21253 +               default:
21254 +                       BT_DBG("%s Command Status OGF %x", hdev->name, ogf);
21255 +                       break;
21256 +               };
21257 +
21258 +               if (cs->ncmd) {
21259 +                       atomic_set(&hdev->cmd_cnt, 1);
21260 +                       if (!skb_queue_empty(&hdev->cmd_q))
21261 +                               hci_sched_cmd(hdev);
21262 +               }
21263 +               break;
21264 +
21265 +       case EVT_CMD_COMPLETE:
21266 +               ec = (evt_cmd_complete *) skb->data;
21267 +               skb_pull(skb, EVT_CMD_COMPLETE_SIZE);
21268 +
21269 +               opcode = __le16_to_cpu(ec->opcode);
21270 +               ogf = cmd_opcode_ogf(opcode);
21271 +               ocf = cmd_opcode_ocf(opcode);
21272 +
21273 +               switch (ogf) {
21274 +               case OGF_INFO_PARAM:
21275 +                       hci_cc_info_param(hdev, ocf, skb);
21276 +                       break;
21277 +
21278 +               case OGF_HOST_CTL:
21279 +                       hci_cc_host_ctl(hdev, ocf, skb);
21280 +                       break;
21281 +
21282 +               case OGF_LINK_CTL:
21283 +                       hci_cc_link_ctl(hdev, ocf, skb);
21284 +                       break;
21285 +
21286 +               case OGF_LINK_POLICY:
21287 +                       hci_cc_link_policy(hdev, ocf, skb);
21288 +                       break;
21289 +
21290 +               default:
21291 +                       BT_DBG("%s Command Completed OGF %x", hdev->name, ogf);
21292 +                       break;
21293 +               };
21294 +
21295 +               if (ec->ncmd) {
21296 +                       atomic_set(&hdev->cmd_cnt, 1);
21297 +                       if (!skb_queue_empty(&hdev->cmd_q))
21298 +                               hci_sched_cmd(hdev);
21299 +               }
21300 +               break;
21301 +       };
21302 +
21303 +       kfree_skb(skb);
21304 +       hdev->stat.evt_rx++;
21305 +}
21306 +
21307 +/* General internal stack event */
21308 +void hci_si_event(struct hci_dev *hdev, int type, int dlen, void *data)
21309 +{
21310 +       hci_event_hdr *eh;
21311 +       evt_stack_internal *si;
21312 +       struct sk_buff *skb;
21313 +       int size;
21314 +       void *ptr;
21315 +
21316 +       size = HCI_EVENT_HDR_SIZE + EVT_STACK_INTERNAL_SIZE + dlen;
21317 +       skb  = bluez_skb_alloc(size, GFP_ATOMIC);
21318 +       if (!skb)
21319 +               return;
21320 +
21321 +       ptr = skb_put(skb, size);
21322 +
21323 +       eh = ptr;
21324 +               eh->evt  = EVT_STACK_INTERNAL;
21325 +       eh->plen = EVT_STACK_INTERNAL_SIZE + dlen;
21326 +       ptr += HCI_EVENT_HDR_SIZE;
21327 +
21328 +       si = ptr;
21329 +       si->type = type;
21330 +       memcpy(si->data, data, dlen);
21331 +       
21332 +       skb->pkt_type = HCI_EVENT_PKT;
21333 +       skb->dev = (void *) hdev;
21334 +       hci_send_to_sock(hdev, skb);
21335 +       kfree_skb(skb);
21336 +}
21337 diff -urN linux-2.4.18/net/bluetooth/hci_sock.c linux-2.4.18-mh15/net/bluetooth/hci_sock.c
21338 --- linux-2.4.18/net/bluetooth/hci_sock.c       2001-09-07 18:28:38.000000000 +0200
21339 +++ linux-2.4.18-mh15/net/bluetooth/hci_sock.c  2004-08-01 16:26:23.000000000 +0200
21340 @@ -25,7 +25,7 @@
21341  /*
21342   * BlueZ HCI socket layer.
21343   *
21344 - * $Id: hci_sock.c,v 1.9 2001/08/05 06:02:16 maxk Exp $
21345 + * $Id: hci_sock.c,v 1.5 2002/07/22 20:32:54 maxk Exp $
21346   */
21347  
21348  #include <linux/config.h>
21349 @@ -49,45 +49,54 @@
21350  
21351  #include <asm/system.h>
21352  #include <asm/uaccess.h>
21353 +#include <asm/unaligned.h>
21354  
21355  #include <net/bluetooth/bluetooth.h>
21356 -#include <net/bluetooth/bluez.h>
21357  #include <net/bluetooth/hci_core.h>
21358  
21359  #ifndef HCI_SOCK_DEBUG
21360 -#undef  DBG
21361 -#define DBG( A... )
21362 +#undef  BT_DBG
21363 +#define BT_DBG( A... )
21364  #endif
21365  
21366 -/* HCI socket interface */
21367 +/* ----- HCI socket interface ----- */
21368 +
21369 +/* Security filter */
21370 +static struct hci_sec_filter hci_sec_filter = {
21371 +       /* Packet types */
21372 +       0x10,
21373 +       /* Events */
21374 +       { 0x1000d9fe, 0x0000300c },
21375 +       /* Commands */
21376 +       {
21377 +               { 0x0 },
21378 +               /* OGF_LINK_CTL */
21379 +               { 0xbe000006, 0x00000001, 0x0000, 0x00 },
21380 +               /* OGF_LINK_POLICY */
21381 +               { 0x00005200, 0x00000000, 0x0000, 0x00 },
21382 +               /* OGF_HOST_CTL */
21383 +               { 0xaab00200, 0x2b402aaa, 0x0154, 0x00 },
21384 +               /* OGF_INFO_PARAM */
21385 +               { 0x000002be, 0x00000000, 0x0000, 0x00 },
21386 +               /* OGF_STATUS_PARAM */
21387 +               { 0x000000ea, 0x00000000, 0x0000, 0x00 }
21388 +       }
21389 +};
21390  
21391  static struct bluez_sock_list hci_sk_list = {
21392         lock: RW_LOCK_UNLOCKED
21393  };
21394  
21395 -static struct sock *hci_sock_lookup(struct hci_dev *hdev)
21396 -{
21397 -       struct sock *sk;
21398 -
21399 -       read_lock(&hci_sk_list.lock);
21400 -       for (sk = hci_sk_list.head; sk; sk = sk->next) {
21401 -               if (hci_pi(sk)->hdev == hdev)
21402 -                       break;
21403 -       }
21404 -       read_unlock(&hci_sk_list.lock);
21405 -       return sk;
21406 -}
21407 -
21408  /* Send frame to RAW socket */
21409  void hci_send_to_sock(struct hci_dev *hdev, struct sk_buff *skb)
21410  {
21411         struct sock * sk;
21412  
21413 -       DBG("hdev %p len %d", hdev, skb->len);
21414 +       BT_DBG("hdev %p len %d", hdev, skb->len);
21415  
21416         read_lock(&hci_sk_list.lock);
21417         for (sk = hci_sk_list.head; sk; sk = sk->next) {
21418 -               struct hci_filter *flt; 
21419 +               struct hci_filter *flt;
21420                 struct sk_buff *nskb;
21421  
21422                 if (sk->state != BT_BOUND || hci_pi(sk)->hdev != hdev)
21423 @@ -100,13 +109,19 @@
21424                 /* Apply filter */
21425                 flt = &hci_pi(sk)->filter;
21426  
21427 -               if (!test_bit(skb->pkt_type, &flt->type_mask))
21428 +               if (!hci_test_bit((skb->pkt_type & HCI_FLT_TYPE_BITS), &flt->type_mask))
21429                         continue;
21430  
21431                 if (skb->pkt_type == HCI_EVENT_PKT) {
21432 -                       register int evt = (*(__u8 *)skb->data & 63);
21433 +                       register int evt = (*(__u8 *)skb->data & HCI_FLT_EVENT_BITS);
21434 +                       
21435 +                       if (!hci_test_bit(evt, &flt->event_mask))
21436 +                               continue;
21437  
21438 -                       if (!test_bit(evt, &flt->event_mask))
21439 +                       if (flt->opcode && ((evt == EVT_CMD_COMPLETE && 
21440 +                                       flt->opcode != *(__u16 *)(skb->data + 3)) ||
21441 +                                       (evt == EVT_CMD_STATUS && 
21442 +                                       flt->opcode != *(__u16 *)(skb->data + 4))))
21443                                 continue;
21444                 }
21445  
21446 @@ -116,8 +131,8 @@
21447                 /* Put type byte before the data */
21448                 memcpy(skb_push(nskb, 1), &nskb->pkt_type, 1);
21449  
21450 -               skb_queue_tail(&sk->receive_queue, nskb);
21451 -               sk->data_ready(sk, nskb->len);
21452 +               if (sock_queue_rcv_skb(sk, nskb))
21453 +                       kfree_skb(nskb);
21454         }
21455         read_unlock(&hci_sk_list.lock);
21456  }
21457 @@ -127,7 +142,7 @@
21458         struct sock *sk = sock->sk;
21459         struct hci_dev *hdev = hci_pi(sk)->hdev;
21460  
21461 -       DBG("sock %p sk %p", sock, sk);
21462 +       BT_DBG("sock %p sk %p", sock, sk);
21463  
21464         if (!sk)
21465                 return 0;
21466 @@ -135,9 +150,7 @@
21467         bluez_sock_unlink(&hci_sk_list, sk);
21468  
21469         if (hdev) {
21470 -               if (!hci_sock_lookup(hdev))
21471 -                       hdev->flags &= ~HCI_SOCK;
21472 -
21473 +               atomic_dec(&hdev->promisc);
21474                 hci_dev_put(hdev);
21475         }
21476  
21477 @@ -149,24 +162,55 @@
21478         sock_put(sk);
21479  
21480         MOD_DEC_USE_COUNT;
21481 -
21482         return 0;
21483  }
21484  
21485 -static int hci_sock_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
21486 +/* Ioctls that require bound socket */ 
21487 +static inline int hci_sock_bound_ioctl(struct sock *sk, unsigned int cmd, unsigned long arg)
21488  {
21489 -       struct sock *sk = sock->sk;
21490         struct hci_dev *hdev = hci_pi(sk)->hdev;
21491 -       __u32 mode;
21492  
21493 -       DBG("cmd %x arg %lx", cmd, arg);
21494 +       if (!hdev)
21495 +               return -EBADFD;
21496  
21497         switch (cmd) {
21498 -       case HCIGETINFO:
21499 -               return hci_dev_info(arg);
21500 +       case HCISETRAW:
21501 +               if (!capable(CAP_NET_ADMIN))
21502 +                       return -EACCES;
21503  
21504 +               if (arg)
21505 +                       set_bit(HCI_RAW, &hdev->flags);
21506 +               else
21507 +                       clear_bit(HCI_RAW, &hdev->flags);
21508 +
21509 +               return 0;
21510 +
21511 +       case HCIGETCONNINFO:
21512 +               return hci_get_conn_info(hdev, arg);
21513 +
21514 +       default:
21515 +               if (hdev->ioctl)
21516 +                       return hdev->ioctl(hdev, cmd, arg);
21517 +               return -EINVAL;
21518 +       }
21519 +}
21520 +
21521 +static int hci_sock_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
21522 +{
21523 +       struct sock *sk = sock->sk;
21524 +       int err;
21525 +
21526 +       BT_DBG("cmd %x arg %lx", cmd, arg);
21527 +
21528 +       switch (cmd) {
21529         case HCIGETDEVLIST:
21530 -               return hci_dev_list(arg);
21531 +               return hci_get_dev_list(arg);
21532 +
21533 +       case HCIGETDEVINFO:
21534 +               return hci_get_dev_info(arg);
21535 +
21536 +       case HCIGETCONNLIST:
21537 +               return hci_get_conn_list(arg);
21538  
21539         case HCIDEVUP:
21540                 if (!capable(CAP_NET_ADMIN))
21541 @@ -183,48 +227,31 @@
21542                         return -EACCES;
21543                 return hci_dev_reset(arg);
21544  
21545 -       case HCIRESETSTAT:
21546 +       case HCIDEVRESTAT:
21547                 if (!capable(CAP_NET_ADMIN))
21548                         return -EACCES;
21549                 return hci_dev_reset_stat(arg);
21550  
21551         case HCISETSCAN:
21552 -               if (!capable(CAP_NET_ADMIN))
21553 -                       return -EACCES;
21554 -               return hci_dev_setscan(arg);
21555 -
21556         case HCISETAUTH:
21557 -               if (!capable(CAP_NET_ADMIN))
21558 -                       return -EACCES;
21559 -               return hci_dev_setauth(arg);
21560 -
21561 -       case HCISETRAW:
21562 -               if (!capable(CAP_NET_ADMIN))
21563 -                       return -EACCES;
21564 -
21565 -               if (!hdev)
21566 -                       return -EBADFD;
21567 -
21568 -               if (arg)
21569 -                       mode = HCI_RAW;
21570 -               else
21571 -                       mode = HCI_NORMAL;
21572 -
21573 -               return hci_dev_setmode(hdev, mode);
21574 -
21575 +       case HCISETENCRYPT:
21576         case HCISETPTYPE:
21577 +       case HCISETLINKPOL:
21578 +       case HCISETLINKMODE:
21579 +       case HCISETACLMTU:
21580 +       case HCISETSCOMTU:
21581                 if (!capable(CAP_NET_ADMIN))
21582                         return -EACCES;
21583 -               return hci_dev_setptype(arg);
21584 +               return hci_dev_cmd(cmd, arg);
21585  
21586         case HCIINQUIRY:
21587                 return hci_inquiry(arg);
21588  
21589 -       case HCIGETCONNLIST:
21590 -               return hci_conn_list(arg);
21591 -
21592         default:
21593 -               return -EINVAL;
21594 +               lock_sock(sk);
21595 +               err = hci_sock_bound_ioctl(sk, cmd, arg);
21596 +               release_sock(sk);
21597 +               return err;
21598         };
21599  }
21600  
21601 @@ -233,28 +260,35 @@
21602         struct sockaddr_hci *haddr = (struct sockaddr_hci *) addr;
21603         struct sock *sk = sock->sk;
21604         struct hci_dev *hdev = NULL;
21605 +       int err = 0;
21606  
21607 -       DBG("sock %p sk %p", sock, sk);
21608 +       BT_DBG("sock %p sk %p", sock, sk);
21609  
21610         if (!haddr || haddr->hci_family != AF_BLUETOOTH)
21611                 return -EINVAL;
21612  
21613 +       lock_sock(sk);
21614 +
21615         if (hci_pi(sk)->hdev) {
21616 -               /* Already bound */
21617 -               return 0;
21618 +               err = -EALREADY;
21619 +               goto done;
21620         }
21621  
21622         if (haddr->hci_dev != HCI_DEV_NONE) {
21623 -               if (!(hdev = hci_dev_get(haddr->hci_dev)))
21624 -                       return -ENODEV;
21625 +               if (!(hdev = hci_dev_get(haddr->hci_dev))) {
21626 +                       err = -ENODEV;
21627 +                       goto done;
21628 +               }
21629  
21630 -               hdev->flags |= HCI_SOCK;
21631 +               atomic_inc(&hdev->promisc);
21632         }
21633  
21634         hci_pi(sk)->hdev = hdev;
21635         sk->state = BT_BOUND;
21636  
21637 -       return 0;
21638 +done:
21639 +       release_sock(sk);
21640 +       return err;
21641  }
21642  
21643  static int hci_sock_getname(struct socket *sock, struct sockaddr *addr, int *addr_len, int peer)
21644 @@ -262,73 +296,44 @@
21645         struct sockaddr_hci *haddr = (struct sockaddr_hci *) addr;
21646         struct sock *sk = sock->sk;
21647  
21648 -       DBG("sock %p sk %p", sock, sk);
21649 +       BT_DBG("sock %p sk %p", sock, sk);
21650 +
21651 +       lock_sock(sk);
21652  
21653         *addr_len = sizeof(*haddr);
21654         haddr->hci_family = AF_BLUETOOTH;
21655         haddr->hci_dev    = hci_pi(sk)->hdev->id;
21656  
21657 +       release_sock(sk);
21658         return 0;
21659  }
21660  
21661 -static int hci_sock_sendmsg(struct socket *sock, struct msghdr *msg, int len,
21662 -                            struct scm_cookie *scm)
21663 -{
21664 -       struct sock *sk = sock->sk;
21665 -       struct hci_dev *hdev = hci_pi(sk)->hdev;
21666 -       struct sk_buff *skb;
21667 -       int err;
21668 -
21669 -       DBG("sock %p sk %p", sock, sk);
21670 -
21671 -       if (msg->msg_flags & MSG_OOB)
21672 -               return -EOPNOTSUPP;
21673 -
21674 -       if (msg->msg_flags & ~(MSG_DONTWAIT|MSG_NOSIGNAL|MSG_ERRQUEUE))
21675 -               return -EINVAL;
21676 -
21677 -       if (!hdev)
21678 -               return -EBADFD;
21679 -
21680 -       if (!(skb = bluez_skb_send_alloc(sk, len, msg->msg_flags & MSG_DONTWAIT, &err)))
21681 -               return err;
21682 -
21683 -       if (memcpy_fromiovec(skb_put(skb, len), msg->msg_iov, len)) {
21684 -               kfree_skb(skb);
21685 -               return -EFAULT;
21686 -       }
21687 -
21688 -       skb->dev = (void *) hdev;
21689 -       skb->pkt_type = *((unsigned char *) skb->data);
21690 -       skb_pull(skb, 1);
21691 -
21692 -       /* Send frame to HCI core */
21693 -       hci_send_raw(skb);
21694 -
21695 -       return len;
21696 -}
21697 -
21698  static inline void hci_sock_cmsg(struct sock *sk, struct msghdr *msg, struct sk_buff *skb)
21699  {
21700         __u32 mask = hci_pi(sk)->cmsg_mask;
21701  
21702         if (mask & HCI_CMSG_DIR)
21703                 put_cmsg(msg, SOL_HCI, HCI_CMSG_DIR, sizeof(int), &bluez_cb(skb)->incomming);
21704 +
21705 +       if (mask & HCI_CMSG_TSTAMP)
21706 +               put_cmsg(msg, SOL_HCI, HCI_CMSG_TSTAMP, sizeof(skb->stamp), &skb->stamp);
21707  }
21708   
21709 -static int hci_sock_recvmsg(struct socket *sock, struct msghdr *msg, int len,
21710 -                            int flags, struct scm_cookie *scm)
21711 +static int hci_sock_recvmsg(struct socket *sock, struct msghdr *msg, int len, int flags, struct scm_cookie *scm)
21712  {
21713         int noblock = flags & MSG_DONTWAIT;
21714         struct sock *sk = sock->sk;
21715         struct sk_buff *skb;
21716         int copied, err;
21717  
21718 -       DBG("sock %p sk %p", sock, sk);
21719 +       BT_DBG("sock %p, sk %p", sock, sk);
21720  
21721 -       if (flags & (MSG_OOB | MSG_PEEK))
21722 +       if (flags & (MSG_OOB))
21723                 return -EOPNOTSUPP;
21724  
21725 +       if (sk->state == BT_CLOSED)
21726 +               return 0;
21727 +
21728         if (!(skb = skb_recv_datagram(sk, flags, noblock, &err)))
21729                 return err;
21730  
21731 @@ -343,28 +348,107 @@
21732         skb->h.raw = skb->data;
21733         err = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied);
21734  
21735 -       if (hci_pi(sk)->cmsg_mask)
21736 -               hci_sock_cmsg(sk, msg, skb);
21737 -
21738 +       hci_sock_cmsg(sk, msg, skb);
21739 +       
21740         skb_free_datagram(sk, skb);
21741  
21742         return err ? : copied;
21743  }
21744  
21745 +static int hci_sock_sendmsg(struct socket *sock, struct msghdr *msg, int len,
21746 +                            struct scm_cookie *scm)
21747 +{
21748 +       struct sock *sk = sock->sk;
21749 +       struct hci_dev *hdev;
21750 +       struct sk_buff *skb;
21751 +       int err;
21752 +
21753 +       BT_DBG("sock %p sk %p", sock, sk);
21754 +
21755 +       if (msg->msg_flags & MSG_OOB)
21756 +               return -EOPNOTSUPP;
21757 +
21758 +       if (msg->msg_flags & ~(MSG_DONTWAIT|MSG_NOSIGNAL|MSG_ERRQUEUE))
21759 +               return -EINVAL;
21760 +
21761 +       if (len < 4)
21762 +               return -EINVAL;
21763 +       
21764 +       lock_sock(sk);
21765 +
21766 +       if (!(hdev = hci_pi(sk)->hdev)) {
21767 +               err = -EBADFD;
21768 +               goto done;
21769 +       }
21770 +
21771 +       if (!(skb = bluez_skb_send_alloc(sk, len, msg->msg_flags & MSG_DONTWAIT, &err)))
21772 +               goto done;
21773 +
21774 +       if (memcpy_fromiovec(skb_put(skb, len), msg->msg_iov, len)) {
21775 +               err = -EFAULT;
21776 +               goto drop;
21777 +       }
21778 +
21779 +       skb->pkt_type = *((unsigned char *) skb->data);
21780 +       skb_pull(skb, 1);
21781 +       skb->dev = (void *) hdev;
21782 +
21783 +       if (skb->pkt_type == HCI_COMMAND_PKT) {
21784 +               u16 opcode = __le16_to_cpu(get_unaligned((u16 *)skb->data));
21785 +               u16 ogf = cmd_opcode_ogf(opcode);
21786 +               u16 ocf = cmd_opcode_ocf(opcode);
21787 +
21788 +               if (((ogf > HCI_SFLT_MAX_OGF) || 
21789 +                               !hci_test_bit(ocf & HCI_FLT_OCF_BITS, &hci_sec_filter.ocf_mask[ogf])) &&
21790 +                                       !capable(CAP_NET_RAW)) {
21791 +                       err = -EPERM;
21792 +                       goto drop;
21793 +               }
21794 +
21795 +               if (test_bit(HCI_RAW, &hdev->flags) || (ogf == OGF_VENDOR_CMD)) {
21796 +                       skb_queue_tail(&hdev->raw_q, skb);
21797 +                       hci_sched_tx(hdev);
21798 +               } else {
21799 +                       skb_queue_tail(&hdev->cmd_q, skb);
21800 +                       hci_sched_cmd(hdev);
21801 +               }
21802 +       } else {
21803 +               if (!capable(CAP_NET_RAW)) {
21804 +                       err = -EPERM;
21805 +                       goto drop;
21806 +               }
21807 +
21808 +               skb_queue_tail(&hdev->raw_q, skb);
21809 +               hci_sched_tx(hdev);
21810 +       }
21811 +
21812 +       err = len;
21813 +
21814 +done:
21815 +       release_sock(sk);
21816 +       return err;
21817 +
21818 +drop:
21819 +       kfree_skb(skb);
21820 +       goto done;
21821 +}
21822 +
21823  int hci_sock_setsockopt(struct socket *sock, int level, int optname, char *optval, int len)
21824  {
21825         struct sock *sk = sock->sk;
21826 -       struct hci_filter flt;
21827 +       struct hci_filter flt = { opcode: 0 };
21828         int err = 0, opt = 0;
21829  
21830 -       DBG("sk %p, opt %d", sk, optname);
21831 +       BT_DBG("sk %p, opt %d", sk, optname);
21832  
21833         lock_sock(sk);
21834  
21835         switch (optname) {
21836         case HCI_DATA_DIR:
21837 -               if (get_user(opt, (int *)optval))
21838 -                       return -EFAULT;
21839 +               if (get_user(opt, (int *)optval)) {
21840 +                       err = -EFAULT;
21841 +                       break;
21842 +               }
21843  
21844                 if (opt)
21845                         hci_pi(sk)->cmsg_mask |= HCI_CMSG_DIR;
21846 @@ -372,12 +456,31 @@
21847                         hci_pi(sk)->cmsg_mask &= ~HCI_CMSG_DIR;
21848                 break;
21849  
21850 +       case HCI_TIME_STAMP:
21851 +               if (get_user(opt, (int *)optval)) {
21852 +                       err = -EFAULT;
21853 +                       break;
21854 +               }
21855 +
21856 +               if (opt)
21857 +                       hci_pi(sk)->cmsg_mask |= HCI_CMSG_TSTAMP;
21858 +               else
21859 +                       hci_pi(sk)->cmsg_mask &= ~HCI_CMSG_TSTAMP;
21860 +               break;
21861 +
21862         case HCI_FILTER:
21863                 len = MIN(len, sizeof(struct hci_filter));
21864                 if (copy_from_user(&flt, optval, len)) {
21865                         err = -EFAULT;
21866                         break;
21867                 }
21868 +
21869 +               if (!capable(CAP_NET_RAW)) {
21870 +                       flt.type_mask     &= hci_sec_filter.type_mask;
21871 +                       flt.event_mask[0] &= hci_sec_filter.event_mask[0];
21872 +                       flt.event_mask[1] &= hci_sec_filter.event_mask[1];
21873 +               }
21874 +               
21875                 memcpy(&hci_pi(sk)->filter, &flt, len);
21876                 break;
21877  
21878 @@ -409,6 +512,16 @@
21879                         return -EFAULT;
21880                 break;
21881  
21882 +       case HCI_TIME_STAMP:
21883 +               if (hci_pi(sk)->cmsg_mask & HCI_CMSG_TSTAMP)
21884 +                       opt = 1;
21885 +               else 
21886 +                       opt = 0;
21887 +
21888 +               if (put_user(opt, optval))
21889 +                       return -EFAULT;
21890 +               break;
21891 +
21892         case HCI_FILTER:
21893                 len = MIN(len, sizeof(struct hci_filter));
21894                 if (copy_to_user(optval, &hci_pi(sk)->filter, len))
21895 @@ -446,7 +559,7 @@
21896  {
21897         struct sock *sk;
21898  
21899 -       DBG("sock %p", sock);
21900 +       BT_DBG("sock %p", sock);
21901  
21902         if (sock->type != SOCK_RAW)
21903                 return -ESOCKTNOSUPPORT;
21904 @@ -464,44 +577,31 @@
21905         sk->protocol = protocol;
21906         sk->state    = BT_OPEN;
21907  
21908 -       /* Initialize filter */
21909 -       hci_pi(sk)->filter.type_mask  = (1<<HCI_EVENT_PKT);
21910 -       hci_pi(sk)->filter.event_mask[0] = ~0L;
21911 -       hci_pi(sk)->filter.event_mask[1] = ~0L;
21912 -
21913         bluez_sock_link(&hci_sk_list, sk);
21914  
21915         MOD_INC_USE_COUNT;
21916 -
21917         return 0;
21918  }
21919  
21920  static int hci_sock_dev_event(struct notifier_block *this, unsigned long event, void *ptr)
21921  {
21922         struct hci_dev *hdev = (struct hci_dev *) ptr;
21923 -       struct sk_buff *skb;
21924 -
21925 -       DBG("hdev %s event %ld", hdev->name, event);
21926 +       evt_si_device sd;
21927 +       
21928 +       BT_DBG("hdev %s event %ld", hdev->name, event);
21929  
21930         /* Send event to sockets */
21931 -       if ((skb = bluez_skb_alloc(HCI_EVENT_HDR_SIZE + EVT_HCI_DEV_EVENT_SIZE, GFP_ATOMIC))) {
21932 -               hci_event_hdr eh = { EVT_HCI_DEV_EVENT, EVT_HCI_DEV_EVENT_SIZE };
21933 -               evt_hci_dev_event he = { event, hdev->id };
21934 -
21935 -               skb->pkt_type = HCI_EVENT_PKT;
21936 -               memcpy(skb_put(skb, HCI_EVENT_HDR_SIZE), &eh, HCI_EVENT_HDR_SIZE);
21937 -               memcpy(skb_put(skb, EVT_HCI_DEV_EVENT_SIZE), &he, EVT_HCI_DEV_EVENT_SIZE);
21938 -
21939 -               hci_send_to_sock(NULL, skb);
21940 -               kfree_skb(skb);
21941 -       }
21942 -
21943 +       sd.event  = event;
21944 +       sd.dev_id = hdev->id;
21945 +       hci_si_event(NULL, EVT_SI_DEVICE, EVT_SI_DEVICE_SIZE, &sd);
21946 +       
21947         if (event == HCI_DEV_UNREG) {
21948                 struct sock *sk;
21949  
21950                 /* Detach sockets from device */
21951                 read_lock(&hci_sk_list.lock);
21952                 for (sk = hci_sk_list.head; sk; sk = sk->next) {
21953 +                       bh_lock_sock(sk);
21954                         if (hci_pi(sk)->hdev == hdev) {
21955                                 hci_pi(sk)->hdev = NULL;
21956                                 sk->err = EPIPE;
21957 @@ -510,6 +610,7 @@
21958  
21959                                 hci_dev_put(hdev);
21960                         }
21961 +                       bh_unlock_sock(sk);
21962                 }
21963                 read_unlock(&hci_sk_list.lock);
21964         }
21965 @@ -529,21 +630,19 @@
21966  int hci_sock_init(void)
21967  {
21968         if (bluez_sock_register(BTPROTO_HCI, &hci_sock_family_ops)) {
21969 -               ERR("Can't register HCI socket");
21970 +               BT_ERR("Can't register HCI socket");
21971                 return -EPROTO;
21972         }
21973  
21974         hci_register_notifier(&hci_sock_nblock);
21975 -
21976         return 0;
21977  }
21978  
21979  int hci_sock_cleanup(void)
21980  {
21981         if (bluez_sock_unregister(BTPROTO_HCI))
21982 -               ERR("Can't unregister HCI socket");
21983 +               BT_ERR("Can't unregister HCI socket");
21984  
21985         hci_unregister_notifier(&hci_sock_nblock);
21986 -
21987         return 0;
21988  }
21989 diff -urN linux-2.4.18/net/bluetooth/hidp/Config.in linux-2.4.18-mh15/net/bluetooth/hidp/Config.in
21990 --- linux-2.4.18/net/bluetooth/hidp/Config.in   1970-01-01 01:00:00.000000000 +0100
21991 +++ linux-2.4.18-mh15/net/bluetooth/hidp/Config.in      2004-08-01 16:26:23.000000000 +0200
21992 @@ -0,0 +1,5 @@
21993 +#
21994 +# Bluetooth HIDP layer configuration
21995 +#
21996 +
21997 +dep_tristate 'HIDP protocol support' CONFIG_BLUEZ_HIDP $CONFIG_INPUT $CONFIG_BLUEZ_L2CAP
21998 diff -urN linux-2.4.18/net/bluetooth/hidp/core.c linux-2.4.18-mh15/net/bluetooth/hidp/core.c
21999 --- linux-2.4.18/net/bluetooth/hidp/core.c      1970-01-01 01:00:00.000000000 +0100
22000 +++ linux-2.4.18-mh15/net/bluetooth/hidp/core.c 2004-08-01 16:26:23.000000000 +0200
22001 @@ -0,0 +1,655 @@
22002 +/* 
22003 +   HIDP implementation for Linux Bluetooth stack (BlueZ).
22004 +   Copyright (C) 2003-2004 Marcel Holtmann <marcel@holtmann.org>
22005 +
22006 +   This program is free software; you can redistribute it and/or modify
22007 +   it under the terms of the GNU General Public License version 2 as
22008 +   published by the Free Software Foundation;
22009 +
22010 +   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
22011 +   OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22012 +   FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS.
22013 +   IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) AND AUTHOR(S) BE LIABLE FOR ANY
22014 +   CLAIM, OR ANY SPECIAL INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES 
22015 +   WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 
22016 +   ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 
22017 +   OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
22018 +
22019 +   ALL LIABILITY, INCLUDING LIABILITY FOR INFRINGEMENT OF ANY PATENTS, 
22020 +   COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS, RELATING TO USE OF THIS 
22021 +   SOFTWARE IS DISCLAIMED.
22022 +*/
22023 +
22024 +#include <linux/config.h>
22025 +#include <linux/module.h>
22026 +
22027 +#include <linux/types.h>
22028 +#include <linux/errno.h>
22029 +#include <linux/kernel.h>
22030 +#include <linux/major.h>
22031 +#include <linux/sched.h>
22032 +#include <linux/slab.h>
22033 +#include <linux/poll.h>
22034 +#include <linux/fcntl.h>
22035 +#include <linux/skbuff.h>
22036 +#include <linux/socket.h>
22037 +#include <linux/ioctl.h>
22038 +#include <linux/file.h>
22039 +#include <linux/init.h>
22040 +#include <net/sock.h>
22041 +
22042 +#include <linux/input.h>
22043 +
22044 +#include <net/bluetooth/bluetooth.h>
22045 +#include <net/bluetooth/l2cap.h>
22046 +
22047 +#include "hidp.h"
22048 +
22049 +#ifndef CONFIG_BT_HIDP_DEBUG
22050 +#undef  BT_DBG
22051 +#define BT_DBG(D...)
22052 +#endif
22053 +
22054 +#define VERSION "1.0"
22055 +
22056 +static DECLARE_RWSEM(hidp_session_sem);
22057 +static LIST_HEAD(hidp_session_list);
22058 +
22059 +static unsigned char hidp_keycode[256] = {
22060 +         0,  0,  0,  0, 30, 48, 46, 32, 18, 33, 34, 35, 23, 36, 37, 38,
22061 +        50, 49, 24, 25, 16, 19, 31, 20, 22, 47, 17, 45, 21, 44,  2,  3,
22062 +         4,  5,  6,  7,  8,  9, 10, 11, 28,  1, 14, 15, 57, 12, 13, 26,
22063 +        27, 43, 43, 39, 40, 41, 51, 52, 53, 58, 59, 60, 61, 62, 63, 64,
22064 +        65, 66, 67, 68, 87, 88, 99, 70,119,110,102,104,111,107,109,106,
22065 +       105,108,103, 69, 98, 55, 74, 78, 96, 79, 80, 81, 75, 76, 77, 71,
22066 +        72, 73, 82, 83, 86,127,116,117,183,184,185,186,187,188,189,190,
22067 +       191,192,193,194,134,138,130,132,128,129,131,137,133,135,136,113,
22068 +       115,114,  0,  0,  0,121,  0, 89, 93,124, 92, 94, 95,  0,  0,  0,
22069 +       122,123, 90, 91, 85,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
22070 +         0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
22071 +         0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
22072 +         0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
22073 +         0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
22074 +        29, 42, 56,125, 97, 54,100,126,164,166,165,163,161,115,114,113,
22075 +       150,158,159,128,136,177,178,176,142,152,173,140
22076 +};
22077 +
22078 +static struct hidp_session *__hidp_get_session(bdaddr_t *bdaddr)
22079 +{
22080 +       struct hidp_session *session;
22081 +       struct list_head *p;
22082 +
22083 +       BT_DBG("");
22084 +
22085 +       list_for_each(p, &hidp_session_list) {
22086 +               session = list_entry(p, struct hidp_session, list);
22087 +               if (!bacmp(bdaddr, &session->bdaddr))
22088 +                       return session;
22089 +       }
22090 +       return NULL;
22091 +}
22092 +
22093 +static void __hidp_link_session(struct hidp_session *session)
22094 +{
22095 +       MOD_INC_USE_COUNT;
22096 +       list_add(&session->list, &hidp_session_list);
22097 +}
22098 +
22099 +static void __hidp_unlink_session(struct hidp_session *session)
22100 +{
22101 +       list_del(&session->list);
22102 +       MOD_DEC_USE_COUNT;
22103 +}
22104 +
22105 +static void __hidp_copy_session(struct hidp_session *session, struct hidp_conninfo *ci)
22106 +{
22107 +       bacpy(&ci->bdaddr, &session->bdaddr);
22108 +
22109 +       ci->flags = session->flags;
22110 +       ci->state = session->state;
22111 +
22112 +       ci->vendor  = 0x0000;
22113 +       ci->product = 0x0000;
22114 +       ci->version = 0x0000;
22115 +       memset(ci->name, 0, 128);
22116 +
22117 +       if (session->input) {
22118 +               ci->vendor  = session->input->idvendor;
22119 +               ci->product = session->input->idproduct;
22120 +               ci->version = session->input->idversion;
22121 +               if (session->input->name)
22122 +                       strncpy(ci->name, session->input->name, 128);
22123 +               else
22124 +                       strncpy(ci->name, "HID Boot Device", 128);
22125 +       }
22126 +}
22127 +
22128 +static int hidp_input_event(struct input_dev *dev, unsigned int type, unsigned int code, int value)
22129 +{
22130 +       struct hidp_session *session = dev->private;
22131 +       struct sk_buff *skb;
22132 +       unsigned char newleds;
22133 +
22134 +       BT_DBG("session %p hid %p data %p size %d", session, device, data, size);
22135 +
22136 +       if (type != EV_LED)
22137 +               return -1;
22138 +
22139 +       newleds = (!!test_bit(LED_KANA,    dev->led) << 3) |
22140 +                 (!!test_bit(LED_COMPOSE, dev->led) << 3) |
22141 +                 (!!test_bit(LED_SCROLLL, dev->led) << 2) |
22142 +                 (!!test_bit(LED_CAPSL,   dev->led) << 1) |
22143 +                 (!!test_bit(LED_NUML,    dev->led));
22144 +
22145 +       if (session->leds == newleds)
22146 +               return 0;
22147 +
22148 +       session->leds = newleds;
22149 +
22150 +       if (!(skb = alloc_skb(3, GFP_ATOMIC))) {
22151 +               BT_ERR("Can't allocate memory for new frame");
22152 +               return -ENOMEM;
22153 +       }
22154 +
22155 +       *skb_put(skb, 1) = 0xa2;
22156 +       *skb_put(skb, 1) = 0x01;
22157 +       *skb_put(skb, 1) = newleds;
22158 +
22159 +       skb_queue_tail(&session->intr_transmit, skb);
22160 +
22161 +       hidp_schedule(session);
22162 +
22163 +       return 0;
22164 +}
22165 +
22166 +static void hidp_input_report(struct hidp_session *session, struct sk_buff *skb)
22167 +{
22168 +       struct input_dev *dev = session->input;
22169 +       unsigned char *keys = session->keys;
22170 +       unsigned char *udata = skb->data + 1;
22171 +       signed char *sdata = skb->data + 1;
22172 +       int i, size = skb->len - 1;
22173 +
22174 +       switch (skb->data[0]) {
22175 +       case 0x01:      /* Keyboard report */
22176 +               for (i = 0; i < 8; i++)
22177 +                       input_report_key(dev, hidp_keycode[i + 224], (udata[0] >> i) & 1);
22178 +
22179 +               for (i = 2; i < 8; i++) {
22180 +                       if (keys[i] > 3 && memscan(udata + 2, keys[i], 6) == udata + 8) {
22181 +                               if (hidp_keycode[keys[i]])
22182 +                                       input_report_key(dev, hidp_keycode[keys[i]], 0);
22183 +                               else
22184 +                                       BT_ERR("Unknown key (scancode %#x) released.", keys[i]);
22185 +                       }
22186 +
22187 +                       if (udata[i] > 3 && memscan(keys + 2, udata[i], 6) == keys + 8) {
22188 +                               if (hidp_keycode[udata[i]])
22189 +                                       input_report_key(dev, hidp_keycode[udata[i]], 1);
22190 +                               else
22191 +                                       BT_ERR("Unknown key (scancode %#x) pressed.", udata[i]);
22192 +                       }
22193 +               }
22194 +
22195 +               memcpy(keys, udata, 8);
22196 +               break;
22197 +
22198 +       case 0x02:      /* Mouse report */
22199 +               input_report_key(dev, BTN_LEFT,   sdata[0] & 0x01);
22200 +               input_report_key(dev, BTN_RIGHT,  sdata[0] & 0x02);
22201 +               input_report_key(dev, BTN_MIDDLE, sdata[0] & 0x04);
22202 +               input_report_key(dev, BTN_SIDE,   sdata[0] & 0x08);
22203 +               input_report_key(dev, BTN_EXTRA,  sdata[0] & 0x10);
22204 +
22205 +               input_report_rel(dev, REL_X, sdata[1]);
22206 +               input_report_rel(dev, REL_Y, sdata[2]);
22207 +
22208 +               if (size > 3)
22209 +                       input_report_rel(dev, REL_WHEEL, sdata[3]);
22210 +               break;
22211 +       }
22212 +
22213 +       input_event(dev, EV_RST, 0, 0);
22214 +}
22215 +
22216 +static void hidp_idle_timeout(unsigned long arg)
22217 +{
22218 +       struct hidp_session *session = (struct hidp_session *) arg;
22219 +
22220 +       atomic_inc(&session->terminate);
22221 +       hidp_schedule(session);
22222 +}
22223 +
22224 +static inline void hidp_set_timer(struct hidp_session *session)
22225 +{
22226 +       if (session->idle_to > 0)
22227 +               mod_timer(&session->timer, jiffies + HZ * session->idle_to);
22228 +}
22229 +
22230 +static inline void hidp_del_timer(struct hidp_session *session)
22231 +{
22232 +       if (session->idle_to > 0)
22233 +               del_timer(&session->timer);
22234 +}
22235 +
22236 +static inline void hidp_send_message(struct hidp_session *session, unsigned char hdr)
22237 +{
22238 +       struct sk_buff *skb;
22239 +
22240 +       BT_DBG("session %p", session);
22241 +
22242 +       if (!(skb = alloc_skb(1, GFP_ATOMIC))) {
22243 +               BT_ERR("Can't allocate memory for message");
22244 +               return;
22245 +       }
22246 +
22247 +       *skb_put(skb, 1) = hdr;
22248 +
22249 +       skb_queue_tail(&session->ctrl_transmit, skb);
22250 +
22251 +       hidp_schedule(session);
22252 +}
22253 +
22254 +static inline int hidp_recv_frame(struct hidp_session *session, struct sk_buff *skb)
22255 +{
22256 +       __u8 hdr;
22257 +
22258 +       BT_DBG("session %p skb %p len %d", session, skb, skb->len);
22259 +
22260 +       hdr = skb->data[0];
22261 +       skb_pull(skb, 1);
22262 +
22263 +       if (hdr == 0xa1) {
22264 +               hidp_set_timer(session);
22265 +
22266 +               if (session->input)
22267 +                       hidp_input_report(session, skb);
22268 +       } else {
22269 +               BT_DBG("Unsupported protocol header 0x%02x", hdr);
22270 +       }
22271 +
22272 +       kfree_skb(skb);
22273 +       return 0;
22274 +}
22275 +
22276 +static int hidp_send_frame(struct socket *sock, unsigned char *data, int len)
22277 +{
22278 +       struct iovec iv = { data, len };
22279 +       struct msghdr msg;
22280 +
22281 +       BT_DBG("sock %p data %p len %d", sock, data, len);
22282 +
22283 +       if (!len)
22284 +               return 0;
22285 +
22286 +       memset(&msg, 0, sizeof(msg));
22287 +       msg.msg_iovlen = 1;
22288 +       msg.msg_iov = &iv;
22289 +
22290 +       return sock_sendmsg(sock, &msg, len);
22291 +}
22292 +
22293 +static int hidp_process_transmit(struct hidp_session *session)
22294 +{
22295 +       struct sk_buff *skb;
22296 +
22297 +       BT_DBG("session %p", session);
22298 +
22299 +       while ((skb = skb_dequeue(&session->ctrl_transmit))) {
22300 +               if (hidp_send_frame(session->ctrl_sock, skb->data, skb->len) < 0) {
22301 +                       skb_queue_head(&session->ctrl_transmit, skb);
22302 +                       break;
22303 +               }
22304 +
22305 +               hidp_set_timer(session);
22306 +               kfree_skb(skb);
22307 +       }
22308 +
22309 +       while ((skb = skb_dequeue(&session->intr_transmit))) {
22310 +               if (hidp_send_frame(session->intr_sock, skb->data, skb->len) < 0) {
22311 +                       skb_queue_head(&session->intr_transmit, skb);
22312 +                       break;
22313 +               }
22314 +
22315 +               hidp_set_timer(session);
22316 +               kfree_skb(skb);
22317 +       }
22318 +
22319 +       return skb_queue_len(&session->ctrl_transmit) +
22320 +                               skb_queue_len(&session->intr_transmit);
22321 +}
22322 +
22323 +static int hidp_session(void *arg)
22324 +{
22325 +       struct hidp_session *session = arg;
22326 +       struct sock *ctrl_sk = session->ctrl_sock->sk;
22327 +       struct sock *intr_sk = session->intr_sock->sk;
22328 +       struct sk_buff *skb;
22329 +       int vendor = 0x0000, product = 0x0000;
22330 +       wait_queue_t ctrl_wait, intr_wait;
22331 +       unsigned long timeo = HZ;
22332 +
22333 +       BT_DBG("session %p", session);
22334 +
22335 +       if (session->input) {
22336 +               vendor  = session->input->idvendor;
22337 +               product = session->input->idproduct;
22338 +       }
22339 +
22340 +       daemonize(); reparent_to_init();
22341 +
22342 +       sprintf(current->comm, "khidpd_%04x%04x", vendor, product);
22343 +
22344 +       sigfillset(&current->blocked);
22345 +       flush_signals(current);
22346 +
22347 +       current->nice = -15;
22348 +
22349 +       set_fs(KERNEL_DS);
22350 +
22351 +       init_waitqueue_entry(&ctrl_wait, current);
22352 +       init_waitqueue_entry(&intr_wait, current);
22353 +       add_wait_queue(ctrl_sk->sleep, &ctrl_wait);
22354 +       add_wait_queue(intr_sk->sleep, &intr_wait);
22355 +       while (!atomic_read(&session->terminate)) {
22356 +               set_current_state(TASK_INTERRUPTIBLE);
22357 +
22358 +               if (ctrl_sk->state != BT_CONNECTED || intr_sk->state != BT_CONNECTED)
22359 +                       break;
22360 +
22361 +               while ((skb = skb_dequeue(&ctrl_sk->receive_queue))) {
22362 +                       skb_orphan(skb);
22363 +                       hidp_recv_frame(session, skb);
22364 +               }
22365 +
22366 +               while ((skb = skb_dequeue(&intr_sk->receive_queue))) {
22367 +                       skb_orphan(skb);
22368 +                       hidp_recv_frame(session, skb);
22369 +               }
22370 +
22371 +               hidp_process_transmit(session);
22372 +
22373 +               schedule();
22374 +       }
22375 +       set_current_state(TASK_RUNNING);
22376 +       remove_wait_queue(intr_sk->sleep, &intr_wait);
22377 +       remove_wait_queue(ctrl_sk->sleep, &ctrl_wait);
22378 +
22379 +       down_write(&hidp_session_sem);
22380 +
22381 +       hidp_del_timer(session);
22382 +
22383 +       if (intr_sk->state != BT_CONNECTED) {
22384 +               init_waitqueue_entry(&ctrl_wait, current);
22385 +               add_wait_queue(ctrl_sk->sleep, &ctrl_wait);
22386 +               while (timeo && ctrl_sk->state != BT_CLOSED) {
22387 +                       set_current_state(TASK_INTERRUPTIBLE);
22388 +                       timeo = schedule_timeout(timeo);
22389 +               }
22390 +               set_current_state(TASK_RUNNING);
22391 +               remove_wait_queue(ctrl_sk->sleep, &ctrl_wait);
22392 +               timeo = HZ;
22393 +       }
22394 +
22395 +       fput(session->ctrl_sock->file);
22396 +
22397 +       init_waitqueue_entry(&intr_wait, current);
22398 +       add_wait_queue(intr_sk->sleep, &intr_wait);
22399 +       while (timeo && intr_sk->state != BT_CLOSED) {
22400 +               set_current_state(TASK_INTERRUPTIBLE);
22401 +               timeo = schedule_timeout(timeo);
22402 +       }
22403 +       set_current_state(TASK_RUNNING);
22404 +       remove_wait_queue(intr_sk->sleep, &intr_wait);
22405 +
22406 +       fput(session->intr_sock->file);
22407 +
22408 +       __hidp_unlink_session(session);
22409 +
22410 +       if (session->input) {
22411 +               input_unregister_device(session->input);
22412 +               kfree(session->input);
22413 +       }
22414 +
22415 +       up_write(&hidp_session_sem);
22416 +
22417 +       kfree(session);
22418 +       return 0;
22419 +}
22420 +
22421 +static inline void hidp_setup_input(struct hidp_session *session, struct hidp_connadd_req *req)
22422 +{
22423 +       struct input_dev *input = session->input;
22424 +       int i;
22425 +
22426 +       input->private = session;
22427 +
22428 +       input->idbus     = BUS_BLUETOOTH;
22429 +       input->idvendor  = req->vendor;
22430 +       input->idproduct = req->product;
22431 +       input->idversion = req->version;
22432 +
22433 +       if (req->subclass & 0x40) {
22434 +               set_bit(EV_KEY, input->evbit);
22435 +               set_bit(EV_LED, input->evbit);
22436 +               set_bit(EV_REP, input->evbit);
22437 +
22438 +               set_bit(LED_NUML,    input->ledbit);
22439 +               set_bit(LED_CAPSL,   input->ledbit);
22440 +               set_bit(LED_SCROLLL, input->ledbit);
22441 +               set_bit(LED_COMPOSE, input->ledbit);
22442 +               set_bit(LED_KANA,    input->ledbit);
22443 +
22444 +               for (i = 0; i < sizeof(hidp_keycode); i++)
22445 +                       set_bit(hidp_keycode[i], input->keybit);
22446 +               clear_bit(0, input->keybit);
22447 +       }
22448 +
22449 +       if (req->subclass & 0x80) {
22450 +               input->evbit[0] = BIT(EV_KEY) | BIT(EV_REL);
22451 +               input->keybit[LONG(BTN_MOUSE)] = BIT(BTN_LEFT) | BIT(BTN_RIGHT) | BIT(BTN_MIDDLE);
22452 +               input->relbit[0] = BIT(REL_X) | BIT(REL_Y);
22453 +               input->keybit[LONG(BTN_MOUSE)] |= BIT(BTN_SIDE) | BIT(BTN_EXTRA);
22454 +               input->relbit[0] |= BIT(REL_WHEEL);
22455 +       }
22456 +
22457 +       input->event = hidp_input_event;
22458 +
22459 +       input_register_device(input);
22460 +}
22461 +
22462 +int hidp_add_connection(struct hidp_connadd_req *req, struct socket *ctrl_sock, struct socket *intr_sock)
22463 +{
22464 +       struct hidp_session *session, *s;
22465 +       int err;
22466 +
22467 +       BT_DBG("");
22468 +
22469 +       if (bacmp(&bluez_pi(ctrl_sock->sk)->src, &bluez_pi(intr_sock->sk)->src) ||
22470 +                       bacmp(&bluez_pi(ctrl_sock->sk)->dst, &bluez_pi(intr_sock->sk)->dst))
22471 +               return -ENOTUNIQ;
22472 +
22473 +       session = kmalloc(sizeof(struct hidp_session), GFP_KERNEL);
22474 +       if (!session) 
22475 +               return -ENOMEM;
22476 +       memset(session, 0, sizeof(struct hidp_session));
22477 +
22478 +       session->input = kmalloc(sizeof(struct input_dev), GFP_KERNEL);
22479 +       if (!session->input) {
22480 +               kfree(session);
22481 +               return -ENOMEM;
22482 +       }
22483 +       memset(session->input, 0, sizeof(struct input_dev));
22484 +
22485 +       down_write(&hidp_session_sem);
22486 +
22487 +       s = __hidp_get_session(&bluez_pi(ctrl_sock->sk)->dst);
22488 +       if (s && s->state == BT_CONNECTED) {
22489 +               err = -EEXIST;
22490 +               goto failed;
22491 +       }
22492 +
22493 +       bacpy(&session->bdaddr, &bluez_pi(ctrl_sock->sk)->dst);
22494 +
22495 +       session->ctrl_mtu = min_t(uint, l2cap_pi(ctrl_sock->sk)->omtu, l2cap_pi(ctrl_sock->sk)->imtu);
22496 +       session->intr_mtu = min_t(uint, l2cap_pi(intr_sock->sk)->omtu, l2cap_pi(intr_sock->sk)->imtu);
22497 +
22498 +       BT_DBG("ctrl mtu %d intr mtu %d", session->ctrl_mtu, session->intr_mtu);
22499 +
22500 +       session->ctrl_sock = ctrl_sock;
22501 +       session->intr_sock = intr_sock;
22502 +       session->state     = BT_CONNECTED;
22503 +
22504 +       init_timer(&session->timer);
22505 +
22506 +       session->timer.function = hidp_idle_timeout;
22507 +       session->timer.data     = (unsigned long) session;
22508 +
22509 +       skb_queue_head_init(&session->ctrl_transmit);
22510 +       skb_queue_head_init(&session->intr_transmit);
22511 +
22512 +       session->flags   = req->flags & (1 << HIDP_BLUETOOTH_VENDOR_ID);
22513 +       session->idle_to = req->idle_to;
22514 +
22515 +       if (session->input)
22516 +               hidp_setup_input(session, req);
22517 +
22518 +       __hidp_link_session(session);
22519 +
22520 +       hidp_set_timer(session);
22521 +
22522 +       err = kernel_thread(hidp_session, session, CLONE_FS | CLONE_FILES | CLONE_SIGHAND);
22523 +       if (err < 0)
22524 +               goto unlink;
22525 +
22526 +       if (session->input) {
22527 +               hidp_send_message(session, 0x70);
22528 +               session->flags |= (1 << HIDP_BOOT_PROTOCOL_MODE);
22529 +
22530 +               session->leds = 0xff;
22531 +               hidp_input_event(session->input, EV_LED, 0, 0);
22532 +       }
22533 +
22534 +       up_write(&hidp_session_sem);
22535 +       return 0;
22536 +
22537 +unlink:
22538 +       hidp_del_timer(session);
22539 +
22540 +       __hidp_unlink_session(session);
22541 +
22542 +       if (session->input)
22543 +               input_unregister_device(session->input);
22544 +
22545 +failed:
22546 +       up_write(&hidp_session_sem);
22547 +
22548 +       if (session->input)
22549 +               kfree(session->input);
22550 +
22551 +       kfree(session);
22552 +       return err;
22553 +}
22554 +
22555 +int hidp_del_connection(struct hidp_conndel_req *req)
22556 +{
22557 +       struct hidp_session *session;
22558 +       int err = 0;
22559 +
22560 +       BT_DBG("");
22561 +
22562 +       down_read(&hidp_session_sem);
22563 +
22564 +       session = __hidp_get_session(&req->bdaddr);
22565 +       if (session) {
22566 +               if (req->flags & (1 << HIDP_VIRTUAL_CABLE_UNPLUG)) {
22567 +                       hidp_send_message(session, 0x15);
22568 +               } else {
22569 +                       /* Flush the transmit queues */
22570 +                       skb_queue_purge(&session->ctrl_transmit);
22571 +                       skb_queue_purge(&session->intr_transmit);
22572 +
22573 +                       /* Kill session thread */
22574 +                       atomic_inc(&session->terminate);
22575 +                       hidp_schedule(session);
22576 +               }
22577 +       } else
22578 +               err = -ENOENT;
22579 +
22580 +       up_read(&hidp_session_sem);
22581 +       return err;
22582 +}
22583 +
22584 +int hidp_get_connlist(struct hidp_connlist_req *req)
22585 +{
22586 +       struct list_head *p;
22587 +       int err = 0, n = 0;
22588 +
22589 +       BT_DBG("");
22590 +
22591 +       down_read(&hidp_session_sem);
22592 +
22593 +       list_for_each(p, &hidp_session_list) {
22594 +               struct hidp_session *session;
22595 +               struct hidp_conninfo ci;
22596 +
22597 +               session = list_entry(p, struct hidp_session, list);
22598 +
22599 +               __hidp_copy_session(session, &ci);
22600 +
22601 +               if (copy_to_user(req->ci, &ci, sizeof(ci))) {
22602 +                       err = -EFAULT;
22603 +                       break;
22604 +               }
22605 +
22606 +               if (++n >= req->cnum)
22607 +                       break;
22608 +
22609 +               req->ci++;
22610 +       }
22611 +       req->cnum = n;
22612 +
22613 +       up_read(&hidp_session_sem);
22614 +       return err;
22615 +}
22616 +
22617 +int hidp_get_conninfo(struct hidp_conninfo *ci)
22618 +{
22619 +       struct hidp_session *session;
22620 +       int err = 0;
22621 +
22622 +       down_read(&hidp_session_sem);
22623 +
22624 +       session = __hidp_get_session(&ci->bdaddr);
22625 +       if (session)
22626 +               __hidp_copy_session(session, ci);
22627 +       else
22628 +               err = -ENOENT;
22629 +
22630 +       up_read(&hidp_session_sem);
22631 +       return err;
22632 +}
22633 +
22634 +static int __init hidp_init(void)
22635 +{
22636 +       l2cap_load();
22637 +
22638 +       hidp_init_sockets();
22639 +
22640 +       BT_INFO("BlueZ HIDP ver %s", VERSION);
22641 +       BT_INFO("Copyright (C) 2003-2004 Marcel Holtmann <marcel@holtmann.org>");
22642 +
22643 +       return 0;
22644 +}
22645 +
22646 +static void __exit hidp_exit(void)
22647 +{
22648 +       hidp_cleanup_sockets();
22649 +}
22650 +
22651 +module_init(hidp_init);
22652 +module_exit(hidp_exit);
22653 +
22654 +MODULE_AUTHOR("Marcel Holtmann <marcel@holtmann.org>");
22655 +MODULE_DESCRIPTION("Bluetooth HIDP ver " VERSION);
22656 +MODULE_LICENSE("GPL");
22657 diff -urN linux-2.4.18/net/bluetooth/hidp/hidp.h linux-2.4.18-mh15/net/bluetooth/hidp/hidp.h
22658 --- linux-2.4.18/net/bluetooth/hidp/hidp.h      1970-01-01 01:00:00.000000000 +0100
22659 +++ linux-2.4.18-mh15/net/bluetooth/hidp/hidp.h 2004-08-01 16:26:23.000000000 +0200
22660 @@ -0,0 +1,122 @@
22661 +/* 
22662 +   HIDP implementation for Linux Bluetooth stack (BlueZ).
22663 +   Copyright (C) 2003-2004 Marcel Holtmann <marcel@holtmann.org>
22664 +
22665 +   This program is free software; you can redistribute it and/or modify
22666 +   it under the terms of the GNU General Public License version 2 as
22667 +   published by the Free Software Foundation;
22668 +
22669 +   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
22670 +   OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22671 +   FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS.
22672 +   IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) AND AUTHOR(S) BE LIABLE FOR ANY
22673 +   CLAIM, OR ANY SPECIAL INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES 
22674 +   WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 
22675 +   ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 
22676 +   OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
22677 +
22678 +   ALL LIABILITY, INCLUDING LIABILITY FOR INFRINGEMENT OF ANY PATENTS, 
22679 +   COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS, RELATING TO USE OF THIS 
22680 +   SOFTWARE IS DISCLAIMED.
22681 +*/
22682 +
22683 +#ifndef __HIDP_H
22684 +#define __HIDP_H
22685 +
22686 +#include <linux/types.h>
22687 +#include <net/bluetooth/bluetooth.h>
22688 +
22689 +/* HIDP ioctl defines */
22690 +#define HIDPCONNADD    _IOW('H', 200, int)
22691 +#define HIDPCONNDEL    _IOW('H', 201, int)
22692 +#define HIDPGETCONNLIST        _IOR('H', 210, int)
22693 +#define HIDPGETCONNINFO        _IOR('H', 211, int)
22694 +
22695 +#define HIDP_VIRTUAL_CABLE_UNPLUG      0
22696 +#define HIDP_BOOT_PROTOCOL_MODE                1
22697 +#define HIDP_BLUETOOTH_VENDOR_ID       9
22698 +
22699 +struct hidp_connadd_req {
22700 +       int   ctrl_sock;        // Connected control socket
22701 +       int   intr_sock;        // Connteted interrupt socket
22702 +       __u16 parser;
22703 +       __u16 rd_size;
22704 +       __u8 *rd_data;
22705 +       __u8  country;
22706 +       __u8  subclass;
22707 +       __u16 vendor;
22708 +       __u16 product;
22709 +       __u16 version;
22710 +       __u32 flags;
22711 +       __u32 idle_to;
22712 +       char  name[128];
22713 +};
22714 +
22715 +struct hidp_conndel_req {
22716 +       bdaddr_t bdaddr;
22717 +       __u32    flags;
22718 +};
22719 +
22720 +struct hidp_conninfo {
22721 +       bdaddr_t bdaddr;
22722 +       __u32    flags;
22723 +       __u16    state;
22724 +       __u16    vendor;
22725 +       __u16    product;
22726 +       __u16    version;
22727 +       char     name[128];
22728 +};
22729 +
22730 +struct hidp_connlist_req {
22731 +       __u32  cnum;
22732 +       struct hidp_conninfo *ci;
22733 +};
22734 +
22735 +int hidp_add_connection(struct hidp_connadd_req *req, struct socket *ctrl_sock, struct socket *intr_sock);
22736 +int hidp_del_connection(struct hidp_conndel_req *req);
22737 +int hidp_get_connlist(struct hidp_connlist_req *req);
22738 +int hidp_get_conninfo(struct hidp_conninfo *ci);
22739 +
22740 +/* HIDP session defines */
22741 +struct hidp_session {
22742 +       struct list_head list;
22743 +
22744 +       struct socket *ctrl_sock;
22745 +       struct socket *intr_sock;
22746 +
22747 +       bdaddr_t bdaddr;
22748 +
22749 +       unsigned long state;
22750 +       unsigned long flags;
22751 +       unsigned long idle_to;
22752 +
22753 +       uint ctrl_mtu;
22754 +       uint intr_mtu;
22755 +
22756 +       atomic_t terminate;
22757 +
22758 +       unsigned char keys[8];
22759 +       unsigned char leds;
22760 +
22761 +       struct input_dev *input;
22762 +
22763 +       struct timer_list timer;
22764 +
22765 +       struct sk_buff_head ctrl_transmit;
22766 +       struct sk_buff_head intr_transmit;
22767 +};
22768 +
22769 +static inline void hidp_schedule(struct hidp_session *session)
22770 +{
22771 +       struct sock *ctrl_sk = session->ctrl_sock->sk;
22772 +       struct sock *intr_sk = session->intr_sock->sk;
22773 +
22774 +       wake_up_interruptible(ctrl_sk->sleep);
22775 +       wake_up_interruptible(intr_sk->sleep);
22776 +}
22777 +
22778 +/* HIDP init defines */
22779 +extern int __init hidp_init_sockets(void);
22780 +extern void __exit hidp_cleanup_sockets(void);
22781 +
22782 +#endif /* __HIDP_H */
22783 diff -urN linux-2.4.18/net/bluetooth/hidp/Makefile linux-2.4.18-mh15/net/bluetooth/hidp/Makefile
22784 --- linux-2.4.18/net/bluetooth/hidp/Makefile    1970-01-01 01:00:00.000000000 +0100
22785 +++ linux-2.4.18-mh15/net/bluetooth/hidp/Makefile       2004-08-01 16:26:23.000000000 +0200
22786 @@ -0,0 +1,10 @@
22787 +#
22788 +# Makefile for the Linux Bluetooth HIDP layer
22789 +#
22790 +
22791 +O_TARGET := hidp.o
22792 +
22793 +obj-y  := core.o sock.o
22794 +obj-m  += $(O_TARGET)
22795 +
22796 +include $(TOPDIR)/Rules.make
22797 diff -urN linux-2.4.18/net/bluetooth/hidp/sock.c linux-2.4.18-mh15/net/bluetooth/hidp/sock.c
22798 --- linux-2.4.18/net/bluetooth/hidp/sock.c      1970-01-01 01:00:00.000000000 +0100
22799 +++ linux-2.4.18-mh15/net/bluetooth/hidp/sock.c 2004-08-01 16:26:23.000000000 +0200
22800 @@ -0,0 +1,212 @@
22801 +/* 
22802 +   HIDP implementation for Linux Bluetooth stack (BlueZ).
22803 +   Copyright (C) 2003-2004 Marcel Holtmann <marcel@holtmann.org>
22804 +
22805 +   This program is free software; you can redistribute it and/or modify
22806 +   it under the terms of the GNU General Public License version 2 as
22807 +   published by the Free Software Foundation;
22808 +
22809 +   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
22810 +   OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22811 +   FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS.
22812 +   IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) AND AUTHOR(S) BE LIABLE FOR ANY
22813 +   CLAIM, OR ANY SPECIAL INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES 
22814 +   WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 
22815 +   ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 
22816 +   OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
22817 +
22818 +   ALL LIABILITY, INCLUDING LIABILITY FOR INFRINGEMENT OF ANY PATENTS, 
22819 +   COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS, RELATING TO USE OF THIS 
22820 +   SOFTWARE IS DISCLAIMED.
22821 +*/
22822 +
22823 +#include <linux/config.h>
22824 +#include <linux/module.h>
22825 +
22826 +#include <linux/types.h>
22827 +#include <linux/errno.h>
22828 +#include <linux/kernel.h>
22829 +#include <linux/major.h>
22830 +#include <linux/sched.h>
22831 +#include <linux/slab.h>
22832 +#include <linux/poll.h>
22833 +#include <linux/fcntl.h>
22834 +#include <linux/skbuff.h>
22835 +#include <linux/socket.h>
22836 +#include <linux/ioctl.h>
22837 +#include <linux/file.h>
22838 +#include <linux/init.h>
22839 +#include <net/sock.h>
22840 +
22841 +#include "hidp.h"
22842 +
22843 +#ifndef CONFIG_BT_HIDP_DEBUG
22844 +#undef  BT_DBG
22845 +#define BT_DBG(D...)
22846 +#endif
22847 +
22848 +static int hidp_sock_release(struct socket *sock)
22849 +{
22850 +       struct sock *sk = sock->sk;
22851 +
22852 +       BT_DBG("sock %p sk %p", sock, sk);
22853 +
22854 +       if (!sk)
22855 +               return 0;
22856 +
22857 +       sock_orphan(sk);
22858 +       sock_put(sk);
22859 +
22860 +       MOD_DEC_USE_COUNT;
22861 +       return 0;
22862 +}
22863 +
22864 +static int hidp_sock_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
22865 +{
22866 +       struct hidp_connadd_req ca;
22867 +       struct hidp_conndel_req cd;
22868 +       struct hidp_connlist_req cl;
22869 +       struct hidp_conninfo ci;
22870 +       struct socket *csock;
22871 +       struct socket *isock;
22872 +       int err;
22873 +
22874 +       BT_DBG("cmd %x arg %lx", cmd, arg);
22875 +
22876 +       switch (cmd) {
22877 +       case HIDPCONNADD:
22878 +               if (!capable(CAP_NET_ADMIN))
22879 +                       return -EACCES;
22880 +
22881 +               if (copy_from_user(&ca, (void *) arg, sizeof(ca)))
22882 +                       return -EFAULT;
22883 +
22884 +               csock = sockfd_lookup(ca.ctrl_sock, &err);
22885 +               if (!csock)
22886 +                       return err;
22887 +
22888 +               isock = sockfd_lookup(ca.intr_sock, &err);
22889 +               if (!isock) {
22890 +                       fput(csock->file);
22891 +                       return err;
22892 +               }
22893 +
22894 +               if (csock->sk->state != BT_CONNECTED || isock->sk->state != BT_CONNECTED) {
22895 +                       fput(csock->file);
22896 +                       fput(isock->file);
22897 +                       return -EBADFD;
22898 +               }
22899 +
22900 +               err = hidp_add_connection(&ca, csock, isock);
22901 +               if (!err) {
22902 +                       if (copy_to_user((void *) arg, &ca, sizeof(ca)))
22903 +                               err = -EFAULT;
22904 +               } else {
22905 +                       fput(csock->file);
22906 +                       fput(isock->file);
22907 +               }
22908 +
22909 +               return err;
22910 +
22911 +       case HIDPCONNDEL:
22912 +               if (!capable(CAP_NET_ADMIN))
22913 +                       return -EACCES;
22914 +
22915 +               if (copy_from_user(&cd, (void *) arg, sizeof(cd)))
22916 +                       return -EFAULT;
22917 +
22918 +               return hidp_del_connection(&cd);
22919 +
22920 +       case HIDPGETCONNLIST:
22921 +               if (copy_from_user(&cl, (void *) arg, sizeof(cl)))
22922 +                       return -EFAULT;
22923 +
22924 +               if (cl.cnum <= 0)
22925 +                       return -EINVAL;
22926 +
22927 +               err = hidp_get_connlist(&cl);
22928 +               if (!err && copy_to_user((void *) arg, &cl, sizeof(cl)))
22929 +                       return -EFAULT;
22930 +
22931 +               return err;
22932 +
22933 +       case HIDPGETCONNINFO:
22934 +               if (copy_from_user(&ci, (void *) arg, sizeof(ci)))
22935 +                       return -EFAULT;
22936 +
22937 +               err = hidp_get_conninfo(&ci);
22938 +               if (!err && copy_to_user((void *) arg, &ci, sizeof(ci)))
22939 +                       return -EFAULT;
22940 +
22941 +               return err;
22942 +       }
22943 +
22944 +       return -EINVAL;
22945 +}
22946 +
22947 +static struct proto_ops hidp_sock_ops = {
22948 +       family:         PF_BLUETOOTH,
22949 +       release:        hidp_sock_release,
22950 +       ioctl:          hidp_sock_ioctl,
22951 +       bind:           sock_no_bind,
22952 +       getname:        sock_no_getname,
22953 +       sendmsg:        sock_no_sendmsg,
22954 +       recvmsg:        sock_no_recvmsg,
22955 +       poll:           sock_no_poll,
22956 +       listen:         sock_no_listen,
22957 +       shutdown:       sock_no_shutdown,
22958 +       setsockopt:     sock_no_setsockopt,
22959 +       getsockopt:     sock_no_getsockopt,
22960 +       connect:        sock_no_connect,
22961 +       socketpair:     sock_no_socketpair,
22962 +       accept:         sock_no_accept,
22963 +       mmap:           sock_no_mmap
22964 +};
22965 +
22966 +static int hidp_sock_create(struct socket *sock, int protocol)
22967 +{
22968 +       struct sock *sk;
22969 +
22970 +       BT_DBG("sock %p", sock);
22971 +
22972 +       if (sock->type != SOCK_RAW)
22973 +               return -ESOCKTNOSUPPORT;
22974 +
22975 +       sock->ops = &hidp_sock_ops;
22976 +
22977 +       if (!(sk = sk_alloc(PF_BLUETOOTH, GFP_KERNEL, 1)))
22978 +               return -ENOMEM;
22979 +
22980 +       MOD_INC_USE_COUNT;
22981 +
22982 +       sock->state = SS_UNCONNECTED;
22983 +       sock_init_data(sock, sk);
22984 +
22985 +       sk->destruct = NULL;
22986 +       sk->protocol = protocol;
22987 +
22988 +       return 0;
22989 +}
22990 +
22991 +static struct net_proto_family hidp_sock_family_ops = {
22992 +       family:         PF_BLUETOOTH,
22993 +       create:         hidp_sock_create
22994 +};
22995 +
22996 +int __init hidp_init_sockets(void)
22997 +{
22998 +       int err;
22999 +
23000 +       if ((err = bluez_sock_register(BTPROTO_HIDP, &hidp_sock_family_ops)))
23001 +               BT_ERR("Can't register HIDP socket layer (%d)", err);
23002 +
23003 +       return err;
23004 +}
23005 +
23006 +void __exit hidp_cleanup_sockets(void)
23007 +{
23008 +       int err;
23009 +
23010 +       if ((err = bluez_sock_unregister(BTPROTO_HIDP)))
23011 +               BT_ERR("Can't unregister HIDP socket layer (%d)", err);
23012 +}
23013 diff -urN linux-2.4.18/net/bluetooth/l2cap.c linux-2.4.18-mh15/net/bluetooth/l2cap.c
23014 --- linux-2.4.18/net/bluetooth/l2cap.c  1970-01-01 01:00:00.000000000 +0100
23015 +++ linux-2.4.18-mh15/net/bluetooth/l2cap.c     2004-08-01 16:26:23.000000000 +0200
23016 @@ -0,0 +1,2222 @@
23017 +/* 
23018 +   BlueZ - Bluetooth protocol stack for Linux
23019 +   Copyright (C) 2000-2001 Qualcomm Incorporated
23020 +
23021 +   Written 2000,2001 by Maxim Krasnyansky <maxk@qualcomm.com>
23022 +
23023 +   This program is free software; you can redistribute it and/or modify
23024 +   it under the terms of the GNU General Public License version 2 as
23025 +   published by the Free Software Foundation;
23026 +
23027 +   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
23028 +   OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23029 +   FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS.
23030 +   IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) AND AUTHOR(S) BE LIABLE FOR ANY
23031 +   CLAIM, OR ANY SPECIAL INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES 
23032 +   WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 
23033 +   ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 
23034 +   OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
23035 +
23036 +   ALL LIABILITY, INCLUDING LIABILITY FOR INFRINGEMENT OF ANY PATENTS, 
23037 +   COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS, RELATING TO USE OF THIS 
23038 +   SOFTWARE IS DISCLAIMED.
23039 +*/
23040 +
23041 +/*
23042 + * BlueZ L2CAP core and sockets.
23043 + *
23044 + * $Id: l2cap.c,v 1.15 2002/09/09 01:14:52 maxk Exp $
23045 + */
23046 +#define VERSION "2.3"
23047 +
23048 +#include <linux/config.h>
23049 +#include <linux/module.h>
23050 +
23051 +#include <linux/types.h>
23052 +#include <linux/errno.h>
23053 +#include <linux/kernel.h>
23054 +#include <linux/major.h>
23055 +#include <linux/sched.h>
23056 +#include <linux/slab.h>
23057 +#include <linux/poll.h>
23058 +#include <linux/fcntl.h>
23059 +#include <linux/init.h>
23060 +#include <linux/skbuff.h>
23061 +#include <linux/interrupt.h>
23062 +#include <linux/socket.h>
23063 +#include <linux/skbuff.h>
23064 +#include <linux/proc_fs.h>
23065 +#include <linux/list.h>
23066 +#include <net/sock.h>
23067 +
23068 +#include <asm/system.h>
23069 +#include <asm/uaccess.h>
23070 +#include <asm/unaligned.h>
23071 +
23072 +#include <net/bluetooth/bluetooth.h>
23073 +#include <net/bluetooth/hci_core.h>
23074 +#include <net/bluetooth/l2cap.h>
23075 +
23076 +#ifndef L2CAP_DEBUG
23077 +#undef  BT_DBG
23078 +#define BT_DBG( A... )
23079 +#endif
23080 +
23081 +static struct proto_ops l2cap_sock_ops;
23082 +
23083 +struct bluez_sock_list l2cap_sk_list = {
23084 +       lock: RW_LOCK_UNLOCKED
23085 +};
23086 +
23087 +static int l2cap_conn_del(struct hci_conn *conn, int err);
23088 +
23089 +static inline void l2cap_chan_add(struct l2cap_conn *conn, struct sock *sk, struct sock *parent);
23090 +static void l2cap_chan_del(struct sock *sk, int err);
23091 +static int  l2cap_chan_send(struct sock *sk, struct msghdr *msg, int len);
23092 +
23093 +static void __l2cap_sock_close(struct sock *sk, int reason);
23094 +static void l2cap_sock_close(struct sock *sk);
23095 +static void l2cap_sock_kill(struct sock *sk);
23096 +
23097 +static int l2cap_send_req(struct l2cap_conn *conn, __u8 code, __u16 len, void *data);
23098 +static int l2cap_send_rsp(struct l2cap_conn *conn, __u8 ident, __u8 code, __u16 len, void *data);
23099 +
23100 +/* ----- L2CAP timers ------ */
23101 +static void l2cap_sock_timeout(unsigned long arg)
23102 +{
23103 +       struct sock *sk = (struct sock *) arg;
23104 +
23105 +       BT_DBG("sock %p state %d", sk, sk->state);
23106 +
23107 +       bh_lock_sock(sk);
23108 +       __l2cap_sock_close(sk, ETIMEDOUT);
23109 +       bh_unlock_sock(sk);
23110 +
23111 +       l2cap_sock_kill(sk);
23112 +       sock_put(sk);
23113 +}
23114 +
23115 +static void l2cap_sock_set_timer(struct sock *sk, long timeout)
23116 +{
23117 +       BT_DBG("sk %p state %d timeout %ld", sk, sk->state, timeout);
23118 +
23119 +       if (!mod_timer(&sk->timer, jiffies + timeout))
23120 +               sock_hold(sk);
23121 +}
23122 +
23123 +static void l2cap_sock_clear_timer(struct sock *sk)
23124 +{
23125 +       BT_DBG("sock %p state %d", sk, sk->state);
23126 +
23127 +       if (timer_pending(&sk->timer) && del_timer(&sk->timer))
23128 +               __sock_put(sk);
23129 +}
23130 +
23131 +static void l2cap_sock_init_timer(struct sock *sk)
23132 +{
23133 +       init_timer(&sk->timer);
23134 +       sk->timer.function = l2cap_sock_timeout;
23135 +       sk->timer.data = (unsigned long)sk;
23136 +}
23137 +
23138 +/* -------- L2CAP connections --------- */
23139 +static struct l2cap_conn *l2cap_conn_add(struct hci_conn *hcon, __u8 status)
23140 +{
23141 +       struct l2cap_conn *conn;
23142 +
23143 +       if ((conn = hcon->l2cap_data))
23144 +               return conn;
23145 +
23146 +       if (status)
23147 +               return conn;
23148 +
23149 +       if (!(conn = kmalloc(sizeof(struct l2cap_conn), GFP_ATOMIC)))
23150 +               return NULL;
23151 +       memset(conn, 0, sizeof(struct l2cap_conn));
23152 +
23153 +       hcon->l2cap_data = conn;
23154 +       conn->hcon = hcon;
23155 +       
23156 +       conn->mtu = hcon->hdev->acl_mtu;
23157 +       conn->src = &hcon->hdev->bdaddr;
23158 +       conn->dst = &hcon->dst;
23159 +       
23160 +       spin_lock_init(&conn->lock);
23161 +       conn->chan_list.lock = RW_LOCK_UNLOCKED;
23162 +
23163 +       BT_DBG("hcon %p conn %p", hcon, conn);
23164 +
23165 +       MOD_INC_USE_COUNT;
23166 +       return conn;
23167 +}
23168 +
23169 +static int l2cap_conn_del(struct hci_conn *hcon, int err)
23170 +{
23171 +       struct l2cap_conn *conn;
23172 +       struct sock *sk;
23173 +
23174 +       if (!(conn = hcon->l2cap_data)) 
23175 +               return 0;
23176 +
23177 +       BT_DBG("hcon %p conn %p, err %d", hcon, conn, err);
23178 +
23179 +       if (conn->rx_skb)
23180 +               kfree_skb(conn->rx_skb);
23181 +
23182 +       /* Kill channels */
23183 +       while ((sk = conn->chan_list.head)) {
23184 +               bh_lock_sock(sk);
23185 +               l2cap_chan_del(sk, err);
23186 +               bh_unlock_sock(sk);
23187 +               l2cap_sock_kill(sk);
23188 +       }
23189 +
23190 +       hcon->l2cap_data = NULL;
23191 +       kfree(conn);
23192 +
23193 +       MOD_DEC_USE_COUNT;
23194 +       return 0;
23195 +}
23196 +
23197 +/* -------- Socket interface ---------- */
23198 +static struct sock *__l2cap_get_sock_by_addr(__u16 psm, bdaddr_t *src)
23199 +{
23200 +       struct sock *sk;
23201 +       for (sk = l2cap_sk_list.head; sk; sk = sk->next) {
23202 +               if (sk->sport == psm && !bacmp(&bluez_pi(sk)->src, src))
23203 +                       break;
23204 +       }
23205 +       return sk;
23206 +}
23207 +
23208 +/* Find socket with psm and source bdaddr.
23209 + * Returns closest match.
23210 + */
23211 +static struct sock *__l2cap_get_sock_by_psm(int state, __u16 psm, bdaddr_t *src)
23212 +{
23213 +       struct sock *sk, *sk1 = NULL;
23214 +
23215 +       for (sk = l2cap_sk_list.head; sk; sk = sk->next) {
23216 +               if (state && sk->state != state)
23217 +                       continue;
23218 +
23219 +               if (l2cap_pi(sk)->psm == psm) {
23220 +                       /* Exact match. */
23221 +                       if (!bacmp(&bluez_pi(sk)->src, src))
23222 +                               break;
23223 +
23224 +                       /* Closest match */
23225 +                       if (!bacmp(&bluez_pi(sk)->src, BDADDR_ANY))
23226 +                               sk1 = sk;
23227 +               }
23228 +       }
23229 +       return sk ? sk : sk1;
23230 +}
23231 +
23232 +/* Find socket with given address (psm, src).
23233 + * Returns locked socket */
23234 +static inline struct sock *l2cap_get_sock_by_psm(int state, __u16 psm, bdaddr_t *src)
23235 +{
23236 +       struct sock *s;
23237 +       read_lock(&l2cap_sk_list.lock);
23238 +       s = __l2cap_get_sock_by_psm(state, psm, src);
23239 +       if (s) bh_lock_sock(s);
23240 +       read_unlock(&l2cap_sk_list.lock);
23241 +       return s;
23242 +}
23243 +
23244 +static void l2cap_sock_destruct(struct sock *sk)
23245 +{
23246 +       BT_DBG("sk %p", sk);
23247 +
23248 +       skb_queue_purge(&sk->receive_queue);
23249 +       skb_queue_purge(&sk->write_queue);
23250 +
23251 +       MOD_DEC_USE_COUNT;
23252 +}
23253 +
23254 +static void l2cap_sock_cleanup_listen(struct sock *parent)
23255 +{
23256 +       struct sock *sk;
23257 +
23258 +       BT_DBG("parent %p", parent);
23259 +
23260 +       /* Close not yet accepted channels */
23261 +       while ((sk = bluez_accept_dequeue(parent, NULL)))
23262 +               l2cap_sock_close(sk);
23263 +
23264 +       parent->state  = BT_CLOSED;
23265 +       parent->zapped = 1;
23266 +}
23267 +
23268 +/* Kill socket (only if zapped and orphan)
23269 + * Must be called on unlocked socket.
23270 + */
23271 +static void l2cap_sock_kill(struct sock *sk)
23272 +{
23273 +       if (!sk->zapped || sk->socket)
23274 +               return;
23275 +
23276 +       BT_DBG("sk %p state %d", sk, sk->state);
23277 +
23278 +       /* Kill poor orphan */
23279 +       bluez_sock_unlink(&l2cap_sk_list, sk);
23280 +       sk->dead = 1;
23281 +       sock_put(sk);
23282 +}
23283 +
23284 +/* Close socket.
23285 + */
23286 +static void __l2cap_sock_close(struct sock *sk, int reason)
23287 +{
23288 +       BT_DBG("sk %p state %d socket %p", sk, sk->state, sk->socket);
23289 +
23290 +       switch (sk->state) {
23291 +       case BT_LISTEN:
23292 +               l2cap_sock_cleanup_listen(sk);
23293 +               break;
23294 +
23295 +       case BT_CONNECTED:
23296 +       case BT_CONFIG:
23297 +       case BT_CONNECT2:
23298 +               if (sk->type == SOCK_SEQPACKET) {
23299 +                       struct l2cap_conn *conn = l2cap_pi(sk)->conn;
23300 +                       l2cap_disconn_req req;
23301 +
23302 +                       sk->state = BT_DISCONN;
23303 +                       l2cap_sock_set_timer(sk, sk->sndtimeo);
23304 +
23305 +                       req.dcid = __cpu_to_le16(l2cap_pi(sk)->dcid);
23306 +                       req.scid = __cpu_to_le16(l2cap_pi(sk)->scid);
23307 +                       l2cap_send_req(conn, L2CAP_DISCONN_REQ, L2CAP_DISCONN_REQ_SIZE, &req);
23308 +               } else {
23309 +                       l2cap_chan_del(sk, reason);
23310 +               }
23311 +               break;
23312 +
23313 +       case BT_CONNECT:
23314 +       case BT_DISCONN:
23315 +               l2cap_chan_del(sk, reason);
23316 +               break;
23317 +
23318 +       default:
23319 +               sk->zapped = 1;
23320 +               break;
23321 +       };
23322 +}
23323 +
23324 +/* Must be called on unlocked socket. */
23325 +static void l2cap_sock_close(struct sock *sk)
23326 +{
23327 +       l2cap_sock_clear_timer(sk);
23328 +       lock_sock(sk);
23329 +       __l2cap_sock_close(sk, ECONNRESET);
23330 +       release_sock(sk);
23331 +       l2cap_sock_kill(sk);
23332 +}
23333 +
23334 +static void l2cap_sock_init(struct sock *sk, struct sock *parent)
23335 +{
23336 +       struct l2cap_pinfo *pi = l2cap_pi(sk);
23337 +
23338 +       BT_DBG("sk %p", sk);
23339 +
23340 +       if (parent) {
23341 +               sk->type = parent->type;
23342 +               pi->imtu = l2cap_pi(parent)->imtu;
23343 +               pi->omtu = l2cap_pi(parent)->omtu;
23344 +               pi->link_mode = l2cap_pi(parent)->link_mode;
23345 +       } else {
23346 +               pi->imtu = L2CAP_DEFAULT_MTU;
23347 +               pi->omtu = 0;
23348 +               pi->link_mode = 0;
23349 +       }
23350 +
23351 +       /* Default config options */
23352 +       pi->conf_mtu = L2CAP_DEFAULT_MTU;
23353 +       pi->flush_to = L2CAP_DEFAULT_FLUSH_TO;
23354 +}
23355 +
23356 +static struct sock *l2cap_sock_alloc(struct socket *sock, int proto, int prio)
23357 +{
23358 +       struct sock *sk;
23359 +
23360 +       if (!(sk = sk_alloc(PF_BLUETOOTH, prio, 1)))
23361 +               return NULL;
23362 +
23363 +       bluez_sock_init(sock, sk);
23364 +       
23365 +       sk->zapped   = 0;
23366 +
23367 +       sk->destruct = l2cap_sock_destruct;
23368 +       sk->sndtimeo = L2CAP_CONN_TIMEOUT;
23369 +
23370 +       sk->protocol = proto;
23371 +       sk->state    = BT_OPEN;
23372 +
23373 +       l2cap_sock_init_timer(sk);
23374 +
23375 +       bluez_sock_link(&l2cap_sk_list, sk);
23376 +
23377 +       MOD_INC_USE_COUNT;
23378 +       return sk;
23379 +}
23380 +
23381 +static int l2cap_sock_create(struct socket *sock, int protocol)
23382 +{
23383 +       struct sock *sk;
23384 +
23385 +       BT_DBG("sock %p", sock);
23386 +
23387 +       sock->state = SS_UNCONNECTED;
23388 +
23389 +       if (sock->type != SOCK_SEQPACKET && sock->type != SOCK_DGRAM && sock->type != SOCK_RAW)
23390 +               return -ESOCKTNOSUPPORT;
23391 +
23392 +       if (sock->type == SOCK_RAW && !capable(CAP_NET_RAW))
23393 +               return -EPERM;
23394 +       
23395 +       sock->ops = &l2cap_sock_ops;
23396 +
23397 +       if (!(sk = l2cap_sock_alloc(sock, protocol, GFP_KERNEL)))
23398 +               return -ENOMEM;
23399 +
23400 +       l2cap_sock_init(sk, NULL);
23401 +       return 0;
23402 +}
23403 +
23404 +static int l2cap_sock_bind(struct socket *sock, struct sockaddr *addr, int addr_len)
23405 +{
23406 +       struct sockaddr_l2 *la = (struct sockaddr_l2 *) addr;
23407 +       struct sock *sk = sock->sk;
23408 +       int err = 0;
23409 +
23410 +       BT_DBG("sk %p, %s %d", sk, batostr(&la->l2_bdaddr), la->l2_psm);
23411 +
23412 +       if (!addr || addr->sa_family != AF_BLUETOOTH)
23413 +               return -EINVAL;
23414 +
23415 +       lock_sock(sk);
23416 +
23417 +       if (sk->state != BT_OPEN) {
23418 +               err = -EBADFD;
23419 +               goto done;
23420 +       }
23421 +
23422 +       write_lock_bh(&l2cap_sk_list.lock);
23423 +       if (la->l2_psm && __l2cap_get_sock_by_addr(la->l2_psm, &la->l2_bdaddr)) {
23424 +               err = -EADDRINUSE;
23425 +       } else {
23426 +               /* Save source address */
23427 +               bacpy(&bluez_pi(sk)->src, &la->l2_bdaddr);
23428 +               l2cap_pi(sk)->psm = la->l2_psm;
23429 +               sk->sport = la->l2_psm;
23430 +               sk->state = BT_BOUND;
23431 +       }
23432 +       write_unlock_bh(&l2cap_sk_list.lock);
23433 +
23434 +done:
23435 +       release_sock(sk);
23436 +       return err;
23437 +}
23438 +
23439 +static int l2cap_do_connect(struct sock *sk)
23440 +{
23441 +       bdaddr_t *src = &bluez_pi(sk)->src;
23442 +       bdaddr_t *dst = &bluez_pi(sk)->dst;
23443 +       struct l2cap_conn *conn;
23444 +       struct hci_conn   *hcon;
23445 +       struct hci_dev    *hdev;
23446 +       int err = 0;
23447 +
23448 +       BT_DBG("%s -> %s psm 0x%2.2x", batostr(src), batostr(dst), l2cap_pi(sk)->psm);
23449 +
23450 +       if (!(hdev = hci_get_route(dst, src)))
23451 +               return -EHOSTUNREACH;
23452 +
23453 +       hci_dev_lock_bh(hdev);
23454 +
23455 +       err = -ENOMEM;
23456 +
23457 +       hcon = hci_connect(hdev, ACL_LINK, dst);
23458 +       if (!hcon)
23459 +               goto done;
23460 +
23461 +       conn = l2cap_conn_add(hcon, 0);
23462 +       if (!conn) {
23463 +               hci_conn_put(hcon);
23464 +               goto done;
23465 +       }
23466 +
23467 +       err = 0;
23468 +
23469 +       /* Update source addr of the socket */
23470 +       bacpy(src, conn->src);
23471 +
23472 +       l2cap_chan_add(conn, sk, NULL);
23473 +
23474 +       sk->state = BT_CONNECT;
23475 +       l2cap_sock_set_timer(sk, sk->sndtimeo);
23476 +
23477 +       if (hcon->state == BT_CONNECTED) {
23478 +               if (sk->type == SOCK_SEQPACKET) {
23479 +                       l2cap_conn_req req;
23480 +                       req.scid = __cpu_to_le16(l2cap_pi(sk)->scid);
23481 +                       req.psm  = l2cap_pi(sk)->psm;
23482 +                       l2cap_send_req(conn, L2CAP_CONN_REQ, L2CAP_CONN_REQ_SIZE, &req);
23483 +               } else {
23484 +                       l2cap_sock_clear_timer(sk);
23485 +                       sk->state = BT_CONNECTED;
23486 +               }
23487 +       }
23488 +
23489 +done:
23490 +       hci_dev_unlock_bh(hdev);
23491 +       hci_dev_put(hdev);
23492 +       return err;
23493 +}
23494 +
23495 +static int l2cap_sock_connect(struct socket *sock, struct sockaddr *addr, int alen, int flags)
23496 +{
23497 +       struct sockaddr_l2 *la = (struct sockaddr_l2 *) addr;
23498 +       struct sock *sk = sock->sk;
23499 +       int err = 0;
23500 +
23501 +       lock_sock(sk);
23502 +
23503 +       BT_DBG("sk %p", sk);
23504 +
23505 +       if (addr->sa_family != AF_BLUETOOTH || alen < sizeof(struct sockaddr_l2)) {
23506 +               err = -EINVAL;
23507 +               goto done;
23508 +       }
23509 +
23510 +       if (sk->type == SOCK_SEQPACKET && !la->l2_psm) {
23511 +               err = -EINVAL;
23512 +               goto done;
23513 +       }
23514 +
23515 +       switch(sk->state) {
23516 +       case BT_CONNECT:
23517 +       case BT_CONNECT2:
23518 +       case BT_CONFIG:
23519 +               /* Already connecting */
23520 +               goto wait;
23521 +
23522 +       case BT_CONNECTED:
23523 +               /* Already connected */
23524 +               goto done;
23525 +
23526 +       case BT_OPEN:
23527 +       case BT_BOUND:
23528 +               /* Can connect */
23529 +               break;
23530 +
23531 +       default:
23532 +               err = -EBADFD;
23533 +               goto done;
23534 +       }
23535 +
23536 +       /* Set destination address and psm */
23537 +       bacpy(&bluez_pi(sk)->dst, &la->l2_bdaddr);
23538 +       l2cap_pi(sk)->psm = la->l2_psm;
23539 +
23540 +       if ((err = l2cap_do_connect(sk)))
23541 +               goto done;
23542 +
23543 +wait:
23544 +       err = bluez_sock_wait_state(sk, BT_CONNECTED,
23545 +                       sock_sndtimeo(sk, flags & O_NONBLOCK));
23546 +
23547 +done:
23548 +       release_sock(sk);
23549 +       return err;
23550 +}
23551 +
23552 +int l2cap_sock_listen(struct socket *sock, int backlog)
23553 +{
23554 +       struct sock *sk = sock->sk;
23555 +       int err = 0;
23556 +
23557 +       BT_DBG("sk %p backlog %d", sk, backlog);
23558 +
23559 +       lock_sock(sk);
23560 +
23561 +       if (sk->state != BT_BOUND || sock->type != SOCK_SEQPACKET) {
23562 +               err = -EBADFD;
23563 +               goto done;
23564 +       }
23565 +
23566 +       if (!l2cap_pi(sk)->psm) {
23567 +               err = -EINVAL;
23568 +               goto done;
23569 +       }
23570 +
23571 +       sk->max_ack_backlog = backlog;
23572 +       sk->ack_backlog = 0;
23573 +       sk->state = BT_LISTEN;
23574 +
23575 +done:
23576 +       release_sock(sk);
23577 +       return err;
23578 +}
23579 +
23580 +int l2cap_sock_accept(struct socket *sock, struct socket *newsock, int flags)
23581 +{
23582 +       DECLARE_WAITQUEUE(wait, current);
23583 +       struct sock *sk = sock->sk, *nsk;
23584 +       long timeo;
23585 +       int err = 0;
23586 +
23587 +       lock_sock(sk);
23588 +
23589 +       if (sk->state != BT_LISTEN) {
23590 +               err = -EBADFD;
23591 +               goto done;
23592 +       }
23593 +
23594 +       timeo = sock_rcvtimeo(sk, flags & O_NONBLOCK);
23595 +
23596 +       BT_DBG("sk %p timeo %ld", sk, timeo);
23597 +
23598 +       /* Wait for an incoming connection. (wake-one). */
23599 +       add_wait_queue_exclusive(sk->sleep, &wait);
23600 +       while (!(nsk = bluez_accept_dequeue(sk, newsock))) {
23601 +               set_current_state(TASK_INTERRUPTIBLE);
23602 +               if (!timeo) {
23603 +                       err = -EAGAIN;
23604 +                       break;
23605 +               }
23606 +
23607 +               release_sock(sk);
23608 +               timeo = schedule_timeout(timeo);
23609 +               lock_sock(sk);
23610 +
23611 +               if (sk->state != BT_LISTEN) {
23612 +                       err = -EBADFD;
23613 +                       break;
23614 +               }
23615 +
23616 +               if (signal_pending(current)) {
23617 +                       err = sock_intr_errno(timeo);
23618 +                       break;
23619 +               }
23620 +       }
23621 +       set_current_state(TASK_RUNNING);
23622 +       remove_wait_queue(sk->sleep, &wait);
23623 +
23624 +       if (err)
23625 +               goto done;
23626 +
23627 +       newsock->state = SS_CONNECTED;
23628 +
23629 +       BT_DBG("new socket %p", nsk);
23630 +
23631 +done:
23632 +       release_sock(sk);
23633 +       return err;
23634 +}
23635 +
23636 +static int l2cap_sock_getname(struct socket *sock, struct sockaddr *addr, int *len, int peer)
23637 +{
23638 +       struct sockaddr_l2 *la = (struct sockaddr_l2 *) addr;
23639 +       struct sock *sk = sock->sk;
23640 +
23641 +       BT_DBG("sock %p, sk %p", sock, sk);
23642 +
23643 +       addr->sa_family = AF_BLUETOOTH;
23644 +       *len = sizeof(struct sockaddr_l2);
23645 +
23646 +       if (peer)
23647 +               bacpy(&la->l2_bdaddr, &bluez_pi(sk)->dst);
23648 +       else
23649 +               bacpy(&la->l2_bdaddr, &bluez_pi(sk)->src);
23650 +
23651 +       la->l2_psm = l2cap_pi(sk)->psm;
23652 +       return 0;
23653 +}
23654 +
23655 +static int l2cap_sock_sendmsg(struct socket *sock, struct msghdr *msg, int len, struct scm_cookie *scm)
23656 +{
23657 +       struct sock *sk = sock->sk;
23658 +       int err = 0;
23659 +
23660 +       BT_DBG("sock %p, sk %p", sock, sk);
23661 +
23662 +       if (sk->err)
23663 +               return sock_error(sk);
23664 +
23665 +       if (msg->msg_flags & MSG_OOB)
23666 +               return -EOPNOTSUPP;
23667 +
23668 +       /* Check outgoing MTU */
23669 +       if (len > l2cap_pi(sk)->omtu)
23670 +               return -EINVAL;
23671 +
23672 +       lock_sock(sk);
23673 +
23674 +       if (sk->state == BT_CONNECTED)
23675 +               err = l2cap_chan_send(sk, msg, len);
23676 +       else
23677 +               err = -ENOTCONN;
23678 +
23679 +       release_sock(sk);
23680 +       return err;
23681 +}
23682 +
23683 +static int l2cap_sock_setsockopt(struct socket *sock, int level, int optname, char *optval, int optlen)
23684 +{
23685 +       struct sock *sk = sock->sk;
23686 +       struct l2cap_options opts;
23687 +       int err = 0, len;
23688 +       __u32 opt;
23689 +
23690 +       BT_DBG("sk %p", sk);
23691 +
23692 +       lock_sock(sk);
23693 +
23694 +       switch (optname) {
23695 +       case L2CAP_OPTIONS:
23696 +               len = MIN(sizeof(opts), optlen);
23697 +               if (copy_from_user((char *)&opts, optval, len)) {
23698 +                       err = -EFAULT;
23699 +                       break;
23700 +               }
23701 +               l2cap_pi(sk)->imtu  = opts.imtu;
23702 +               l2cap_pi(sk)->omtu  = opts.omtu;
23703 +               break;
23704 +
23705 +       case L2CAP_LM:
23706 +               if (get_user(opt, (__u32 *)optval)) {
23707 +                       err = -EFAULT;
23708 +                       break;
23709 +               }
23710 +
23711 +               l2cap_pi(sk)->link_mode = opt;
23712 +               break;
23713 +
23714 +       default:
23715 +               err = -ENOPROTOOPT;
23716 +               break;
23717 +       }
23718 +
23719 +       release_sock(sk);
23720 +       return err;
23721 +}
23722 +
23723 +static int l2cap_sock_getsockopt(struct socket *sock, int level, int optname, char *optval, int *optlen)
23724 +{
23725 +       struct sock *sk = sock->sk;
23726 +       struct l2cap_options opts;
23727 +       struct l2cap_conninfo cinfo;
23728 +       int len, err = 0; 
23729 +
23730 +       if (get_user(len, optlen))
23731 +               return -EFAULT;
23732 +
23733 +       lock_sock(sk);
23734 +
23735 +       switch (optname) {
23736 +       case L2CAP_OPTIONS:
23737 +               opts.imtu     = l2cap_pi(sk)->imtu;
23738 +               opts.omtu     = l2cap_pi(sk)->omtu;
23739 +               opts.flush_to = l2cap_pi(sk)->flush_to;
23740 +
23741 +               len = MIN(len, sizeof(opts));
23742 +               if (copy_to_user(optval, (char *)&opts, len))
23743 +                       err = -EFAULT;
23744 +
23745 +               break;
23746 +
23747 +       case L2CAP_LM:
23748 +               if (put_user(l2cap_pi(sk)->link_mode, (__u32 *)optval))
23749 +                       err = -EFAULT;
23750 +               break;
23751 +
23752 +       case L2CAP_CONNINFO:
23753 +               if (sk->state != BT_CONNECTED) {
23754 +                       err = -ENOTCONN;
23755 +                       break;
23756 +               }
23757 +
23758 +               cinfo.hci_handle = l2cap_pi(sk)->conn->hcon->handle;
23759 +
23760 +               len = MIN(len, sizeof(cinfo));
23761 +               if (copy_to_user(optval, (char *)&cinfo, len))
23762 +                       err = -EFAULT;
23763 +
23764 +               break;
23765 +
23766 +       default:
23767 +               err = -ENOPROTOOPT;
23768 +               break;
23769 +       }
23770 +
23771 +       release_sock(sk);
23772 +       return err;
23773 +}
23774 +
23775 +static int l2cap_sock_shutdown(struct socket *sock, int how)
23776 +{
23777 +       struct sock *sk = sock->sk;
23778 +       int err = 0;
23779 +
23780 +       BT_DBG("sock %p, sk %p", sock, sk);
23781 +
23782 +       if (!sk) return 0;
23783 +
23784 +       lock_sock(sk);
23785 +       if (!sk->shutdown) {
23786 +               sk->shutdown = SHUTDOWN_MASK;
23787 +               l2cap_sock_clear_timer(sk);
23788 +               __l2cap_sock_close(sk, 0);
23789 +
23790 +               if (sk->linger)
23791 +                       err = bluez_sock_wait_state(sk, BT_CLOSED, sk->lingertime);
23792 +       }
23793 +       release_sock(sk);
23794 +       return err;
23795 +}
23796 +
23797 +static int l2cap_sock_release(struct socket *sock)
23798 +{
23799 +       struct sock *sk = sock->sk;
23800 +       int err;
23801 +
23802 +       BT_DBG("sock %p, sk %p", sock, sk);
23803 +
23804 +       if (!sk) return 0;
23805 +
23806 +       err = l2cap_sock_shutdown(sock, 2);
23807 +
23808 +       sock_orphan(sk);
23809 +       l2cap_sock_kill(sk);
23810 +       return err;
23811 +}
23812 +
23813 +/* --------- L2CAP channels --------- */
23814 +static struct sock * __l2cap_get_chan_by_dcid(struct l2cap_chan_list *l, __u16 cid)
23815 +{
23816 +       struct sock *s;
23817 +       for (s = l->head; s; s = l2cap_pi(s)->next_c) {
23818 +               if (l2cap_pi(s)->dcid == cid)
23819 +                       break;
23820 +       }
23821 +       return s;
23822 +}
23823 +
23824 +static struct sock *__l2cap_get_chan_by_scid(struct l2cap_chan_list *l, __u16 cid)
23825 +{
23826 +       struct sock *s;
23827 +       for (s = l->head; s; s = l2cap_pi(s)->next_c) {
23828 +               if (l2cap_pi(s)->scid == cid)
23829 +                       break;
23830 +       }
23831 +       return s;
23832 +}
23833 +
23834 +/* Find channel with given SCID.
23835 + * Returns locked socket */
23836 +static inline struct sock *l2cap_get_chan_by_scid(struct l2cap_chan_list *l, __u16 cid)
23837 +{
23838 +       struct sock *s;
23839 +       read_lock(&l->lock);
23840 +       s = __l2cap_get_chan_by_scid(l, cid);
23841 +       if (s) bh_lock_sock(s);
23842 +       read_unlock(&l->lock);
23843 +       return s;
23844 +}
23845 +
23846 +static __u16 l2cap_alloc_cid(struct l2cap_chan_list *l)
23847 +{
23848 +       __u16 cid = 0x0040;
23849 +
23850 +       for (; cid < 0xffff; cid++) {
23851 +               if(!__l2cap_get_chan_by_scid(l, cid))
23852 +                       return cid;
23853 +       }
23854 +
23855 +       return 0;
23856 +}
23857 +
23858 +static inline void __l2cap_chan_link(struct l2cap_chan_list *l, struct sock *sk)
23859 +{
23860 +       sock_hold(sk);
23861 +
23862 +       if (l->head)
23863 +               l2cap_pi(l->head)->prev_c = sk;
23864 +
23865 +       l2cap_pi(sk)->next_c = l->head;
23866 +       l2cap_pi(sk)->prev_c = NULL;
23867 +       l->head = sk;
23868 +}
23869 +
23870 +static inline void l2cap_chan_unlink(struct l2cap_chan_list *l, struct sock *sk)
23871 +{
23872 +       struct sock *next = l2cap_pi(sk)->next_c, *prev = l2cap_pi(sk)->prev_c;
23873 +
23874 +       write_lock(&l->lock);
23875 +       if (sk == l->head)
23876 +               l->head = next;
23877 +
23878 +       if (next)
23879 +               l2cap_pi(next)->prev_c = prev;
23880 +       if (prev)
23881 +               l2cap_pi(prev)->next_c = next;
23882 +       write_unlock(&l->lock);
23883 +
23884 +       __sock_put(sk);
23885 +}
23886 +
23887 +static void __l2cap_chan_add(struct l2cap_conn *conn, struct sock *sk, struct sock *parent)
23888 +{
23889 +       struct l2cap_chan_list *l = &conn->chan_list;
23890 +
23891 +       BT_DBG("conn %p, psm 0x%2.2x, dcid 0x%4.4x", conn, l2cap_pi(sk)->psm, l2cap_pi(sk)->dcid);
23892 +
23893 +       l2cap_pi(sk)->conn = conn;
23894 +
23895 +       if (sk->type == SOCK_SEQPACKET) {
23896 +               /* Alloc CID for connection-oriented socket */
23897 +               l2cap_pi(sk)->scid = l2cap_alloc_cid(l);
23898 +       } else if (sk->type == SOCK_DGRAM) {
23899 +               /* Connectionless socket */
23900 +               l2cap_pi(sk)->scid = 0x0002;
23901 +               l2cap_pi(sk)->dcid = 0x0002;
23902 +               l2cap_pi(sk)->omtu = L2CAP_DEFAULT_MTU;
23903 +       } else {
23904 +               /* Raw socket can send/recv signalling messages only */
23905 +               l2cap_pi(sk)->scid = 0x0001;
23906 +               l2cap_pi(sk)->dcid = 0x0001;
23907 +               l2cap_pi(sk)->omtu = L2CAP_DEFAULT_MTU;
23908 +       }
23909 +
23910 +       __l2cap_chan_link(l, sk);
23911 +
23912 +       if (parent)
23913 +               bluez_accept_enqueue(parent, sk);
23914 +}
23915 +
23916 +static inline void l2cap_chan_add(struct l2cap_conn *conn, struct sock *sk, struct sock *parent)
23917 +{
23918 +       struct l2cap_chan_list *l = &conn->chan_list;
23919 +       write_lock(&l->lock);
23920 +       __l2cap_chan_add(conn, sk, parent);
23921 +       write_unlock(&l->lock);
23922 +}
23923 +
23924 +/* Delete channel. 
23925 + * Must be called on the locked socket. */
23926 +static void l2cap_chan_del(struct sock *sk, int err)
23927 +{
23928 +       struct l2cap_conn *conn = l2cap_pi(sk)->conn;
23929 +       struct sock *parent = bluez_pi(sk)->parent;
23930 +
23931 +       l2cap_sock_clear_timer(sk);
23932 +
23933 +       BT_DBG("sk %p, conn %p, err %d", sk, conn, err);
23934 +
23935 +       if (conn) { 
23936 +               /* Unlink from channel list */
23937 +               l2cap_chan_unlink(&conn->chan_list, sk);
23938 +               l2cap_pi(sk)->conn = NULL;
23939 +               hci_conn_put(conn->hcon);
23940 +       }
23941 +
23942 +       sk->state  = BT_CLOSED;
23943 +       sk->zapped = 1;
23944 +
23945 +       if (err)
23946 +               sk->err = err;
23947 +
23948 +       if (parent)
23949 +               parent->data_ready(parent, 0);
23950 +       else
23951 +               sk->state_change(sk);
23952 +}
23953 +
23954 +static void l2cap_conn_ready(struct l2cap_conn *conn)
23955 +{
23956 +       struct l2cap_chan_list *l = &conn->chan_list;
23957 +       struct sock *sk;
23958 +
23959 +       BT_DBG("conn %p", conn);
23960 +
23961 +       read_lock(&l->lock);
23962 +
23963 +       for (sk = l->head; sk; sk = l2cap_pi(sk)->next_c) {
23964 +               bh_lock_sock(sk);
23965 +
23966 +               if (sk->type != SOCK_SEQPACKET) {
23967 +                       l2cap_sock_clear_timer(sk);
23968 +                       sk->state = BT_CONNECTED;
23969 +                       sk->state_change(sk);
23970 +               } else if (sk->state == BT_CONNECT) {
23971 +                       l2cap_conn_req req;
23972 +                       req.scid = __cpu_to_le16(l2cap_pi(sk)->scid);
23973 +                       req.psm  = l2cap_pi(sk)->psm;
23974 +                       l2cap_send_req(conn, L2CAP_CONN_REQ, L2CAP_CONN_REQ_SIZE, &req);
23975 +               }
23976 +
23977 +               bh_unlock_sock(sk);
23978 +       }
23979 +
23980 +       read_unlock(&l->lock);
23981 +}
23982 +
23983 +/* Notify sockets that we cannot guaranty reliability anymore */
23984 +static void l2cap_conn_unreliable(struct l2cap_conn *conn, int err)
23985 +{
23986 +       struct l2cap_chan_list *l = &conn->chan_list;
23987 +       struct sock *sk;
23988 +
23989 +       BT_DBG("conn %p", conn);
23990 +
23991 +       read_lock(&l->lock);
23992 +       for (sk = l->head; sk; sk = l2cap_pi(sk)->next_c) {
23993 +               if (l2cap_pi(sk)->link_mode & L2CAP_LM_RELIABLE)
23994 +                       sk->err = err;
23995 +       }
23996 +       read_unlock(&l->lock);
23997 +}
23998 +
23999 +static void l2cap_chan_ready(struct sock *sk)
24000 +{
24001 +       struct sock *parent = bluez_pi(sk)->parent;
24002 +
24003 +       BT_DBG("sk %p, parent %p", sk, parent);
24004 +
24005 +       l2cap_pi(sk)->conf_state = 0;
24006 +       l2cap_sock_clear_timer(sk);
24007 +
24008 +       if (!parent) {
24009 +               /* Outgoing channel.
24010 +                * Wake up socket sleeping on connect.
24011 +                */
24012 +               sk->state = BT_CONNECTED;
24013 +               sk->state_change(sk);
24014 +       } else {
24015 +               /* Incomming channel.
24016 +                * Wake up socket sleeping on accept.
24017 +                */
24018 +               parent->data_ready(parent, 0);
24019 +       }
24020 +}
24021 +
24022 +/* Copy frame to all raw sockets on that connection */
24023 +void l2cap_raw_recv(struct l2cap_conn *conn, struct sk_buff *skb)
24024 +{
24025 +       struct l2cap_chan_list *l = &conn->chan_list;
24026 +       struct sk_buff *nskb;
24027 +       struct sock * sk;
24028 +
24029 +       BT_DBG("conn %p", conn);
24030 +
24031 +       read_lock(&l->lock);
24032 +       for (sk = l->head; sk; sk = l2cap_pi(sk)->next_c) {
24033 +               if (sk->type != SOCK_RAW)
24034 +                       continue;
24035 +
24036 +               /* Don't send frame to the socket it came from */
24037 +               if (skb->sk == sk)
24038 +                       continue;
24039 +
24040 +               if (!(nskb = skb_clone(skb, GFP_ATOMIC)))
24041 +                       continue;
24042 +
24043 +               if (sock_queue_rcv_skb(sk, nskb))
24044 +                       kfree_skb(nskb);
24045 +       }
24046 +       read_unlock(&l->lock);
24047 +}
24048 +
24049 +static int l2cap_chan_send(struct sock *sk, struct msghdr *msg, int len)
24050 +{
24051 +       struct l2cap_conn *conn = l2cap_pi(sk)->conn;
24052 +       struct sk_buff *skb, **frag;
24053 +       int err, hlen, count, sent=0;
24054 +       l2cap_hdr *lh;
24055 +
24056 +       BT_DBG("sk %p len %d", sk, len);
24057 +
24058 +       /* First fragment (with L2CAP header) */
24059 +       if (sk->type == SOCK_DGRAM)
24060 +               hlen = L2CAP_HDR_SIZE + 2;
24061 +       else
24062 +               hlen = L2CAP_HDR_SIZE;
24063 +
24064 +       count = MIN(conn->mtu - hlen, len);
24065 +
24066 +       skb = bluez_skb_send_alloc(sk, hlen + count,
24067 +                       msg->msg_flags & MSG_DONTWAIT, &err);
24068 +       if (!skb)
24069 +               return err;
24070 +
24071 +       /* Create L2CAP header */
24072 +       lh = (l2cap_hdr *) skb_put(skb, L2CAP_HDR_SIZE);
24073 +       lh->cid = __cpu_to_le16(l2cap_pi(sk)->dcid);
24074 +       lh->len = __cpu_to_le16(len + (hlen - L2CAP_HDR_SIZE));
24075 +
24076 +       if (sk->type == SOCK_DGRAM)
24077 +               put_unaligned(l2cap_pi(sk)->psm, (__u16 *) skb_put(skb, 2));
24078 +
24079 +       if (memcpy_fromiovec(skb_put(skb, count), msg->msg_iov, count)) {
24080 +               err = -EFAULT;
24081 +               goto fail;
24082 +       }
24083 +
24084 +       sent += count;
24085 +       len  -= count;
24086 +
24087 +       /* Continuation fragments (no L2CAP header) */
24088 +       frag = &skb_shinfo(skb)->frag_list;
24089 +       while (len) {
24090 +               count = MIN(conn->mtu, len);
24091 +
24092 +               *frag = bluez_skb_send_alloc(sk, count, msg->msg_flags & MSG_DONTWAIT, &err);
24093 +               if (!*frag)
24094 +                       goto fail;
24095 +               
24096 +               if (memcpy_fromiovec(skb_put(*frag, count), msg->msg_iov, count)) {
24097 +                       err = -EFAULT;
24098 +                       goto fail;
24099 +               }
24100 +
24101 +               sent += count;
24102 +               len  -= count;
24103 +
24104 +               frag = &(*frag)->next;
24105 +       }
24106 +
24107 +       if ((err = hci_send_acl(conn->hcon, skb, 0)) < 0)
24108 +               goto fail;
24109 +
24110 +       return sent;
24111 +
24112 +fail:
24113 +       kfree_skb(skb);
24114 +       return err;
24115 +}
24116 +
24117 +/* --------- L2CAP signalling commands --------- */
24118 +static inline __u8 l2cap_get_ident(struct l2cap_conn *conn)
24119 +{
24120 +       __u8 id;
24121 +
24122 +       /* Get next available identificator.
24123 +        *    1 - 199 are used by kernel.
24124 +        *  200 - 254 are used by utilities like l2ping, etc 
24125 +        */
24126 +
24127 +       spin_lock(&conn->lock);
24128 +
24129 +       if (++conn->tx_ident > 199)
24130 +               conn->tx_ident = 1;
24131 +
24132 +       id = conn->tx_ident;
24133 +
24134 +       spin_unlock(&conn->lock);
24135 +
24136 +       return id;
24137 +}
24138 +
24139 +static struct sk_buff *l2cap_build_cmd(struct l2cap_conn *conn,
24140 +                               __u8 code, __u8 ident, __u16 dlen, void *data)
24141 +{
24142 +       struct sk_buff *skb, **frag;
24143 +       l2cap_cmd_hdr *cmd;
24144 +       l2cap_hdr *lh;
24145 +       int len, count;
24146 +
24147 +       BT_DBG("conn %p, code 0x%2.2x, ident 0x%2.2x, len %d", conn, code, ident, dlen);
24148 +
24149 +       len = L2CAP_HDR_SIZE + L2CAP_CMD_HDR_SIZE + dlen;
24150 +       count = MIN(conn->mtu, len);
24151 +       
24152 +       skb = bluez_skb_alloc(count, GFP_ATOMIC);
24153 +       if (!skb)
24154 +               return NULL;
24155 +
24156 +       lh = (l2cap_hdr *) skb_put(skb, L2CAP_HDR_SIZE);
24157 +       lh->len = __cpu_to_le16(L2CAP_CMD_HDR_SIZE + dlen);
24158 +       lh->cid = __cpu_to_le16(0x0001);
24159 +
24160 +       cmd = (l2cap_cmd_hdr *) skb_put(skb, L2CAP_CMD_HDR_SIZE);
24161 +       cmd->code  = code;
24162 +       cmd->ident = ident;
24163 +       cmd->len   = __cpu_to_le16(dlen);
24164 +
24165 +       if (dlen) {
24166 +               count -= L2CAP_HDR_SIZE + L2CAP_CMD_HDR_SIZE;
24167 +               memcpy(skb_put(skb, count), data, count);
24168 +               data += count;
24169 +       }
24170 +
24171 +       len -= skb->len;
24172 +       
24173 +       /* Continuation fragments (no L2CAP header) */
24174 +       frag = &skb_shinfo(skb)->frag_list;
24175 +       while (len) {
24176 +               count = MIN(conn->mtu, len);
24177 +
24178 +               *frag = bluez_skb_alloc(count, GFP_ATOMIC);
24179 +               if (!*frag)
24180 +                       goto fail;
24181 +               
24182 +               memcpy(skb_put(*frag, count), data, count);
24183 +
24184 +               len  -= count;
24185 +               data += count;
24186 +               
24187 +               frag = &(*frag)->next;
24188 +       }
24189 +
24190 +       return skb;
24191 +
24192 +fail:
24193 +       kfree_skb(skb);
24194 +       return NULL;
24195 +}
24196 +
24197 +static int l2cap_send_req(struct l2cap_conn *conn, __u8 code, __u16 len, void *data)
24198 +{
24199 +       __u8 ident = l2cap_get_ident(conn);
24200 +       struct sk_buff *skb = l2cap_build_cmd(conn, code, ident, len, data);
24201 +
24202 +       BT_DBG("code 0x%2.2x", code);
24203 +
24204 +       if (!skb)
24205 +               return -ENOMEM;
24206 +       return hci_send_acl(conn->hcon, skb, 0);
24207 +}
24208 +
24209 +static int l2cap_send_rsp(struct l2cap_conn *conn, __u8 ident, __u8 code, __u16 len, void *data)
24210 +{
24211 +       struct sk_buff *skb = l2cap_build_cmd(conn, code, ident, len, data);
24212 +
24213 +       BT_DBG("code 0x%2.2x", code);
24214 +
24215 +       if (!skb)
24216 +               return -ENOMEM;
24217 +       return hci_send_acl(conn->hcon, skb, 0);
24218 +}
24219 +
24220 +static inline int l2cap_get_conf_opt(void **ptr, int *type, int *olen, unsigned long *val)
24221 +{
24222 +       l2cap_conf_opt *opt = *ptr;
24223 +       int len;
24224 +
24225 +       len = L2CAP_CONF_OPT_SIZE + opt->len;
24226 +       *ptr += len;
24227 +
24228 +       *type = opt->type;
24229 +       *olen = opt->len;
24230 +
24231 +       switch (opt->len) {
24232 +       case 1:
24233 +               *val = *((__u8 *) opt->val);
24234 +               break;
24235 +
24236 +       case 2:
24237 +               *val = __le16_to_cpu(*((__u16 *)opt->val));
24238 +               break;
24239 +
24240 +       case 4:
24241 +               *val = __le32_to_cpu(*((__u32 *)opt->val));
24242 +               break;
24243 +
24244 +       default:
24245 +               *val = (unsigned long) opt->val;
24246 +               break;
24247 +       };
24248 +
24249 +       BT_DBG("type 0x%2.2x len %d val 0x%lx", *type, opt->len, *val);
24250 +       return len;
24251 +}
24252 +
24253 +static inline void l2cap_parse_conf_req(struct sock *sk, void *data, int len)
24254 +{
24255 +       int type, hint, olen; 
24256 +       unsigned long val;
24257 +       void *ptr = data;
24258 +
24259 +       BT_DBG("sk %p len %d", sk, len);
24260 +
24261 +       while (len >= L2CAP_CONF_OPT_SIZE) {
24262 +               len -= l2cap_get_conf_opt(&ptr, &type, &olen, &val);
24263 +
24264 +               hint  = type & 0x80;
24265 +               type &= 0x7f;
24266 +
24267 +               switch (type) {
24268 +               case L2CAP_CONF_MTU:
24269 +                       l2cap_pi(sk)->conf_mtu = val;
24270 +                       break;
24271 +
24272 +               case L2CAP_CONF_FLUSH_TO:
24273 +                       l2cap_pi(sk)->flush_to = val;
24274 +                       break;
24275 +
24276 +               case L2CAP_CONF_QOS:
24277 +                       break;
24278 +               
24279 +               default:
24280 +                       if (hint)
24281 +                               break;
24282 +
24283 +                       /* FIXME: Reject unknown option */
24284 +                       break;
24285 +               };
24286 +       }
24287 +}
24288 +
24289 +static void l2cap_add_conf_opt(void **ptr, __u8 type, __u8 len, unsigned long val)
24290 +{
24291 +       register l2cap_conf_opt *opt = *ptr;
24292 +
24293 +       BT_DBG("type 0x%2.2x len %d val 0x%lx", type, len, val);
24294 +
24295 +       opt->type = type;
24296 +       opt->len  = len;
24297 +
24298 +       switch (len) {
24299 +       case 1:
24300 +               *((__u8 *) opt->val)  = val;
24301 +               break;
24302 +
24303 +       case 2:
24304 +               *((__u16 *) opt->val) = __cpu_to_le16(val);
24305 +               break;
24306 +
24307 +       case 4:
24308 +               *((__u32 *) opt->val) = __cpu_to_le32(val);
24309 +               break;
24310 +
24311 +       default:
24312 +               memcpy(opt->val, (void *) val, len);
24313 +               break;
24314 +       };
24315 +
24316 +       *ptr += L2CAP_CONF_OPT_SIZE + len;
24317 +}
24318 +
24319 +static int l2cap_build_conf_req(struct sock *sk, void *data)
24320 +{
24321 +       struct l2cap_pinfo *pi = l2cap_pi(sk);
24322 +       l2cap_conf_req *req = (l2cap_conf_req *) data;
24323 +       void *ptr = req->data;
24324 +
24325 +       BT_DBG("sk %p", sk);
24326 +
24327 +       if (pi->imtu != L2CAP_DEFAULT_MTU)
24328 +               l2cap_add_conf_opt(&ptr, L2CAP_CONF_MTU, 2, pi->imtu);
24329 +
24330 +       /* FIXME. Need actual value of the flush timeout */
24331 +       //if (flush_to != L2CAP_DEFAULT_FLUSH_TO)
24332 +       //   l2cap_add_conf_opt(&ptr, L2CAP_CONF_FLUSH_TO, 2, pi->flush_to);
24333 +
24334 +       req->dcid  = __cpu_to_le16(pi->dcid);
24335 +       req->flags = __cpu_to_le16(0);
24336 +
24337 +       return ptr - data;
24338 +}
24339 +
24340 +static inline int l2cap_conf_output(struct sock *sk, void **ptr)
24341 +{
24342 +       struct l2cap_pinfo *pi = l2cap_pi(sk);
24343 +       int result = 0;
24344 +
24345 +       /* Configure output options and let the other side know
24346 +        * which ones we don't like.
24347 +        */
24348 +       if (pi->conf_mtu < pi->omtu) {
24349 +               l2cap_add_conf_opt(ptr, L2CAP_CONF_MTU, 2, pi->omtu);
24350 +               result = L2CAP_CONF_UNACCEPT;
24351 +       } else {
24352 +               pi->omtu = pi->conf_mtu;
24353 +       }
24354 +
24355 +       BT_DBG("sk %p result %d", sk, result);
24356 +       return result;
24357 +}
24358 +
24359 +static int l2cap_build_conf_rsp(struct sock *sk, void *data, int *result)
24360 +{
24361 +       l2cap_conf_rsp *rsp = (l2cap_conf_rsp *) data;
24362 +       void *ptr = rsp->data;
24363 +       u16 flags = 0;
24364 +
24365 +       BT_DBG("sk %p complete %d", sk, result ? 1 : 0);
24366 +
24367 +       if (result)
24368 +               *result = l2cap_conf_output(sk, &ptr);
24369 +       else    
24370 +               flags |= 0x0001;
24371 +
24372 +       rsp->scid   = __cpu_to_le16(l2cap_pi(sk)->dcid);
24373 +       rsp->result = __cpu_to_le16(result ? *result : 0);
24374 +       rsp->flags  = __cpu_to_le16(flags);
24375 +
24376 +       return ptr - data;
24377 +}
24378 +
24379 +static inline int l2cap_connect_req(struct l2cap_conn *conn, l2cap_cmd_hdr *cmd, __u8 *data)
24380 +{
24381 +       struct l2cap_chan_list *list = &conn->chan_list;
24382 +       l2cap_conn_req *req = (l2cap_conn_req *) data;
24383 +       l2cap_conn_rsp rsp;
24384 +       struct sock *sk, *parent;
24385 +       int result = 0, status = 0;
24386 +
24387 +       __u16 dcid = 0, scid = __le16_to_cpu(req->scid);
24388 +       __u16 psm  = req->psm;
24389 +
24390 +       BT_DBG("psm 0x%2.2x scid 0x%4.4x", psm, scid);
24391 +
24392 +       /* Check if we have socket listening on psm */
24393 +       parent = l2cap_get_sock_by_psm(BT_LISTEN, psm, conn->src);
24394 +       if (!parent) {
24395 +               result = L2CAP_CR_BAD_PSM;
24396 +               goto sendresp;
24397 +       }
24398 +
24399 +       result = L2CAP_CR_NO_MEM;
24400 +
24401 +       /* Check for backlog size */
24402 +       if (parent->ack_backlog > parent->max_ack_backlog) {
24403 +               BT_DBG("backlog full %d", parent->ack_backlog); 
24404 +               goto response;
24405 +       }
24406 +
24407 +       sk = l2cap_sock_alloc(NULL, BTPROTO_L2CAP, GFP_ATOMIC);
24408 +       if (!sk)
24409 +               goto response;
24410 +
24411 +       write_lock(&list->lock);
24412 +
24413 +       /* Check if we already have channel with that dcid */
24414 +       if (__l2cap_get_chan_by_dcid(list, scid)) {
24415 +               write_unlock(&list->lock);
24416 +               sk->zapped = 1;
24417 +               l2cap_sock_kill(sk);
24418 +               goto response;
24419 +       }
24420 +
24421 +       hci_conn_hold(conn->hcon);
24422 +
24423 +       l2cap_sock_init(sk, parent);
24424 +       bacpy(&bluez_pi(sk)->src, conn->src);
24425 +       bacpy(&bluez_pi(sk)->dst, conn->dst);
24426 +       l2cap_pi(sk)->psm  = psm;
24427 +       l2cap_pi(sk)->dcid = scid;
24428 +
24429 +       __l2cap_chan_add(conn, sk, parent);
24430 +       dcid = l2cap_pi(sk)->scid;
24431 +
24432 +       l2cap_sock_set_timer(sk, sk->sndtimeo);
24433 +
24434 +       /* Service level security */
24435 +       result = L2CAP_CR_PEND;
24436 +       status = L2CAP_CS_AUTHEN_PEND;
24437 +       sk->state = BT_CONNECT2;
24438 +       l2cap_pi(sk)->ident = cmd->ident;
24439 +       
24440 +       if (l2cap_pi(sk)->link_mode & L2CAP_LM_ENCRYPT) {
24441 +               if (!hci_conn_encrypt(conn->hcon))
24442 +                       goto done;
24443 +       } else if (l2cap_pi(sk)->link_mode & L2CAP_LM_AUTH) {
24444 +               if (!hci_conn_auth(conn->hcon))
24445 +                       goto done;
24446 +       }
24447 +
24448 +       sk->state = BT_CONFIG;
24449 +       result = status = 0;
24450 +
24451 +done:
24452 +       write_unlock(&list->lock);
24453 +
24454 +response:
24455 +       bh_unlock_sock(parent);
24456 +
24457 +sendresp:
24458 +       rsp.scid   = __cpu_to_le16(scid);
24459 +       rsp.dcid   = __cpu_to_le16(dcid);
24460 +       rsp.result = __cpu_to_le16(result);
24461 +       rsp.status = __cpu_to_le16(status);
24462 +       l2cap_send_rsp(conn, cmd->ident, L2CAP_CONN_RSP, L2CAP_CONN_RSP_SIZE, &rsp);
24463 +       return 0;
24464 +}
24465 +
24466 +static inline int l2cap_connect_rsp(struct l2cap_conn *conn, l2cap_cmd_hdr *cmd, __u8 *data)
24467 +{
24468 +       l2cap_conn_rsp *rsp = (l2cap_conn_rsp *) data;
24469 +       __u16 scid, dcid, result, status;
24470 +       struct sock *sk;
24471 +       char req[128];
24472 +
24473 +       scid   = __le16_to_cpu(rsp->scid);
24474 +       dcid   = __le16_to_cpu(rsp->dcid);
24475 +       result = __le16_to_cpu(rsp->result);
24476 +       status = __le16_to_cpu(rsp->status);
24477 +
24478 +       BT_DBG("dcid 0x%4.4x scid 0x%4.4x result 0x%2.2x status 0x%2.2x", dcid, scid, result, status);
24479 +
24480 +       if (!(sk = l2cap_get_chan_by_scid(&conn->chan_list, scid)))
24481 +               return -ENOENT;
24482 +
24483 +       switch (result) {
24484 +       case L2CAP_CR_SUCCESS:
24485 +               sk->state = BT_CONFIG;
24486 +               l2cap_pi(sk)->dcid = dcid;
24487 +               l2cap_pi(sk)->conf_state |= L2CAP_CONF_REQ_SENT;
24488 +
24489 +               l2cap_send_req(conn, L2CAP_CONF_REQ, l2cap_build_conf_req(sk, req), req);
24490 +               break;
24491 +
24492 +       case L2CAP_CR_PEND:
24493 +               break;
24494 +
24495 +       default:
24496 +               l2cap_chan_del(sk, ECONNREFUSED);
24497 +               break;
24498 +       }
24499 +
24500 +       bh_unlock_sock(sk);
24501 +       return 0;
24502 +}
24503 +
24504 +static inline int l2cap_config_req(struct l2cap_conn *conn, l2cap_cmd_hdr *cmd, __u8 *data)
24505 +{
24506 +       l2cap_conf_req * req = (l2cap_conf_req *) data;
24507 +       __u16 dcid, flags;
24508 +       __u8 rsp[64];
24509 +       struct sock *sk;
24510 +       int result;
24511 +
24512 +       dcid  = __le16_to_cpu(req->dcid);
24513 +       flags = __le16_to_cpu(req->flags);
24514 +
24515 +       BT_DBG("dcid 0x%4.4x flags 0x%2.2x", dcid, flags);
24516 +
24517 +       if (!(sk = l2cap_get_chan_by_scid(&conn->chan_list, dcid)))
24518 +               return -ENOENT;
24519 +
24520 +       l2cap_parse_conf_req(sk, req->data, cmd->len - L2CAP_CONF_REQ_SIZE);
24521 +
24522 +       if (flags & 0x0001) {
24523 +               /* Incomplete config. Send empty response. */
24524 +               l2cap_send_rsp(conn, cmd->ident, L2CAP_CONF_RSP, l2cap_build_conf_rsp(sk, rsp, NULL), rsp);
24525 +               goto unlock;
24526 +       }
24527 +
24528 +       /* Complete config. */
24529 +       l2cap_send_rsp(conn, cmd->ident, L2CAP_CONF_RSP, l2cap_build_conf_rsp(sk, rsp, &result), rsp);
24530 +
24531 +       if (result)
24532 +               goto unlock;
24533 +
24534 +       /* Output config done */
24535 +       l2cap_pi(sk)->conf_state |= L2CAP_CONF_OUTPUT_DONE;
24536 +
24537 +       if (l2cap_pi(sk)->conf_state & L2CAP_CONF_INPUT_DONE) {
24538 +               sk->state = BT_CONNECTED;
24539 +               l2cap_chan_ready(sk);
24540 +       } else if (!(l2cap_pi(sk)->conf_state & L2CAP_CONF_REQ_SENT)) {
24541 +               char req[64];
24542 +               l2cap_send_req(conn, L2CAP_CONF_REQ, l2cap_build_conf_req(sk, req), req);
24543 +       }
24544 +
24545 +unlock:
24546 +       bh_unlock_sock(sk);
24547 +       return 0;
24548 +}
24549 +
24550 +static inline int l2cap_config_rsp(struct l2cap_conn *conn, l2cap_cmd_hdr *cmd, __u8 *data)
24551 +{
24552 +       l2cap_conf_rsp *rsp = (l2cap_conf_rsp *)data;
24553 +       __u16 scid, flags, result;
24554 +       struct sock *sk;
24555 +       int err = 0;
24556 +
24557 +       scid   = __le16_to_cpu(rsp->scid);
24558 +       flags  = __le16_to_cpu(rsp->flags);
24559 +       result = __le16_to_cpu(rsp->result);
24560 +
24561 +       BT_DBG("scid 0x%4.4x flags 0x%2.2x result 0x%2.2x", scid, flags, result);
24562 +
24563 +       if (!(sk = l2cap_get_chan_by_scid(&conn->chan_list, scid)))
24564 +               return -ENOENT;
24565 +
24566 +       switch (result) {
24567 +       case L2CAP_CONF_SUCCESS:
24568 +               break;
24569 +
24570 +       case L2CAP_CONF_UNACCEPT:
24571 +               if (++l2cap_pi(sk)->conf_retry < L2CAP_CONF_MAX_RETRIES) {
24572 +                       char req[128];
24573 +                       /* 
24574 +                          It does not make sense to adjust L2CAP parameters 
24575 +                          that are currently defined in the spec. We simply 
24576 +                          resend config request that we sent earlier. It is
24577 +                          stupid :) but it helps qualification testing
24578 +                          which expects at least some response from us.
24579 +                       */
24580 +                       l2cap_send_req(conn, L2CAP_CONF_REQ,
24581 +                               l2cap_build_conf_req(sk, req), req);
24582 +                       goto done;
24583 +               }
24584 +       default: 
24585 +               sk->state = BT_DISCONN;
24586 +               sk->err   = ECONNRESET;
24587 +               l2cap_sock_set_timer(sk, HZ * 5);
24588 +               {
24589 +                       l2cap_disconn_req req;
24590 +                       req.dcid = __cpu_to_le16(l2cap_pi(sk)->dcid);
24591 +                       req.scid = __cpu_to_le16(l2cap_pi(sk)->scid);
24592 +                       l2cap_send_req(conn, L2CAP_DISCONN_REQ, L2CAP_DISCONN_REQ_SIZE, &req);
24593 +               }
24594 +               goto done;
24595 +       }
24596 +
24597 +       if (flags & 0x01)
24598 +               goto done;
24599 +
24600 +       /* Input config done */
24601 +       l2cap_pi(sk)->conf_state |= L2CAP_CONF_INPUT_DONE;
24602 +
24603 +       if (l2cap_pi(sk)->conf_state & L2CAP_CONF_OUTPUT_DONE) {
24604 +               sk->state = BT_CONNECTED;
24605 +               l2cap_chan_ready(sk);
24606 +       }
24607 +
24608 +done:
24609 +       bh_unlock_sock(sk);
24610 +       return err;
24611 +}
24612 +
24613 +static inline int l2cap_disconnect_req(struct l2cap_conn *conn, l2cap_cmd_hdr *cmd, __u8 *data)
24614 +{
24615 +       l2cap_disconn_req *req = (l2cap_disconn_req *) data;
24616 +       l2cap_disconn_rsp rsp;
24617 +       __u16 dcid, scid;
24618 +       struct sock *sk;
24619 +
24620 +       scid = __le16_to_cpu(req->scid);
24621 +       dcid = __le16_to_cpu(req->dcid);
24622 +
24623 +       BT_DBG("scid 0x%4.4x dcid 0x%4.4x", scid, dcid);
24624 +
24625 +       if (!(sk = l2cap_get_chan_by_scid(&conn->chan_list, dcid)))
24626 +               return 0;
24627 +
24628 +       rsp.dcid = __cpu_to_le16(l2cap_pi(sk)->scid);
24629 +       rsp.scid = __cpu_to_le16(l2cap_pi(sk)->dcid);
24630 +       l2cap_send_rsp(conn, cmd->ident, L2CAP_DISCONN_RSP, L2CAP_DISCONN_RSP_SIZE, &rsp);
24631 +
24632 +       sk->shutdown = SHUTDOWN_MASK;
24633 +       
24634 +       l2cap_chan_del(sk, ECONNRESET);
24635 +       bh_unlock_sock(sk);
24636 +
24637 +       l2cap_sock_kill(sk);
24638 +       return 0;
24639 +}
24640 +
24641 +static inline int l2cap_disconnect_rsp(struct l2cap_conn *conn, l2cap_cmd_hdr *cmd, __u8 *data)
24642 +{
24643 +       l2cap_disconn_rsp *rsp = (l2cap_disconn_rsp *) data;
24644 +       __u16 dcid, scid;
24645 +       struct sock *sk;
24646 +
24647 +       scid = __le16_to_cpu(rsp->scid);
24648 +       dcid = __le16_to_cpu(rsp->dcid);
24649 +
24650 +       BT_DBG("dcid 0x%4.4x scid 0x%4.4x", dcid, scid);
24651 +
24652 +       if (!(sk = l2cap_get_chan_by_scid(&conn->chan_list, scid)))
24653 +               return 0;
24654 +       l2cap_chan_del(sk, 0);
24655 +       bh_unlock_sock(sk);
24656 +
24657 +       l2cap_sock_kill(sk);
24658 +       return 0;
24659 +}
24660 +
24661 +static inline int l2cap_information_req(struct l2cap_conn *conn, l2cap_cmd_hdr *cmd, u8 *data)
24662 +{
24663 +       l2cap_info_req *req = (l2cap_info_req *) data;
24664 +       l2cap_info_rsp rsp;
24665 +       u16 type;
24666 +
24667 +       type = __le16_to_cpu(req->type);
24668 +
24669 +       BT_DBG("type 0x%4.4x", type);
24670 +
24671 +       rsp.type   = __cpu_to_le16(type);
24672 +       rsp.result = __cpu_to_le16(L2CAP_IR_NOTSUPP);
24673 +       l2cap_send_rsp(conn, cmd->ident, L2CAP_INFO_RSP, sizeof(rsp), &rsp);
24674 +       return 0;
24675 +}
24676 +
24677 +static inline int l2cap_information_rsp(struct l2cap_conn *conn, l2cap_cmd_hdr *cmd, u8 *data)
24678 +{
24679 +       l2cap_info_rsp *rsp = (l2cap_info_rsp *) data;
24680 +       u16 type, result;
24681 +
24682 +       type   = __le16_to_cpu(rsp->type);
24683 +       result = __le16_to_cpu(rsp->result);
24684 +
24685 +       BT_DBG("type 0x%4.4x result 0x%2.2x", type, result);
24686 +
24687 +       return 0;
24688 +}
24689 +
24690 +static inline void l2cap_sig_channel(struct l2cap_conn *conn, struct sk_buff *skb)
24691 +{
24692 +       __u8 *data = skb->data;
24693 +       int len = skb->len;
24694 +       l2cap_cmd_hdr cmd;
24695 +       int err = 0;
24696 +
24697 +       l2cap_raw_recv(conn, skb);
24698 +
24699 +       while (len >= L2CAP_CMD_HDR_SIZE) {
24700 +               memcpy(&cmd, data, L2CAP_CMD_HDR_SIZE);
24701 +               data += L2CAP_CMD_HDR_SIZE;
24702 +               len  -= L2CAP_CMD_HDR_SIZE;
24703 +
24704 +               cmd.len = __le16_to_cpu(cmd.len);
24705 +
24706 +               BT_DBG("code 0x%2.2x len %d id 0x%2.2x", cmd.code, cmd.len, cmd.ident);
24707 +
24708 +               if (cmd.len > len || !cmd.ident) {
24709 +                       BT_DBG("corrupted command");
24710 +                       break;
24711 +               }
24712 +
24713 +               switch (cmd.code) {
24714 +               case L2CAP_COMMAND_REJ:
24715 +                       /* FIXME: We should process this */
24716 +                       break;
24717 +
24718 +               case L2CAP_CONN_REQ:
24719 +                       err = l2cap_connect_req(conn, &cmd, data);
24720 +                       break;
24721 +
24722 +               case L2CAP_CONN_RSP:
24723 +                       err = l2cap_connect_rsp(conn, &cmd, data);
24724 +                       break;
24725 +
24726 +               case L2CAP_CONF_REQ:
24727 +                       err = l2cap_config_req(conn, &cmd, data);
24728 +                       break;
24729 +
24730 +               case L2CAP_CONF_RSP:
24731 +                       err = l2cap_config_rsp(conn, &cmd, data);
24732 +                       break;
24733 +
24734 +               case L2CAP_DISCONN_REQ:
24735 +                       err = l2cap_disconnect_req(conn, &cmd, data);
24736 +                       break;
24737 +
24738 +               case L2CAP_DISCONN_RSP:
24739 +                       err = l2cap_disconnect_rsp(conn, &cmd, data);
24740 +                       break;
24741 +
24742 +               case L2CAP_ECHO_REQ:
24743 +                       l2cap_send_rsp(conn, cmd.ident, L2CAP_ECHO_RSP, cmd.len, data);
24744 +                       break;
24745 +
24746 +               case L2CAP_ECHO_RSP:
24747 +                       break;
24748 +
24749 +               case L2CAP_INFO_REQ:
24750 +                       err = l2cap_information_req(conn, &cmd, data);
24751 +                       break;
24752 +
24753 +               case L2CAP_INFO_RSP:
24754 +                       err = l2cap_information_rsp(conn, &cmd, data);
24755 +                       break;
24756 +
24757 +               default:
24758 +                       BT_ERR("Unknown signaling command 0x%2.2x", cmd.code);
24759 +                       err = -EINVAL;
24760 +                       break;
24761 +               };
24762 +
24763 +               if (err) {
24764 +                       l2cap_cmd_rej rej;
24765 +                       BT_DBG("error %d", err);
24766 +
24767 +                       /* FIXME: Map err to a valid reason */
24768 +                       rej.reason = __cpu_to_le16(0);
24769 +                       l2cap_send_rsp(conn, cmd.ident, L2CAP_COMMAND_REJ, L2CAP_CMD_REJ_SIZE, &rej);
24770 +               }
24771 +
24772 +               data += cmd.len;
24773 +               len  -= cmd.len;
24774 +       }
24775 +
24776 +       kfree_skb(skb);
24777 +}
24778 +
24779 +static inline int l2cap_data_channel(struct l2cap_conn *conn, __u16 cid, struct sk_buff *skb)
24780 +{
24781 +       struct sock *sk;
24782 +
24783 +       sk = l2cap_get_chan_by_scid(&conn->chan_list, cid);
24784 +       if (!sk) {
24785 +               BT_DBG("unknown cid 0x%4.4x", cid);
24786 +               goto drop;
24787 +       }
24788 +
24789 +       BT_DBG("sk %p, len %d", sk, skb->len);
24790 +
24791 +       if (sk->state != BT_CONNECTED)
24792 +               goto drop;
24793 +
24794 +       if (l2cap_pi(sk)->imtu < skb->len)
24795 +               goto drop;
24796 +
24797 +       /* If socket recv buffers overflows we drop data here 
24798 +        * which is *bad* because L2CAP has to be reliable. 
24799 +        * But we don't have any other choice. L2CAP doesn't 
24800 +        * provide flow control mechanism */ 
24801 +       
24802 +       if (!sock_queue_rcv_skb(sk, skb))
24803 +               goto done;
24804 +
24805 +drop:
24806 +       kfree_skb(skb);
24807 +
24808 +done:
24809 +       if (sk) bh_unlock_sock(sk);
24810 +       return 0;
24811 +}
24812 +
24813 +static inline int l2cap_conless_channel(struct l2cap_conn *conn, __u16 psm, struct sk_buff *skb)
24814 +{
24815 +       struct sock *sk;
24816 +
24817 +       sk = l2cap_get_sock_by_psm(0, psm, conn->src);
24818 +       if (!sk)
24819 +               goto drop;
24820 +
24821 +       BT_DBG("sk %p, len %d", sk, skb->len);
24822 +
24823 +       if (sk->state != BT_BOUND && sk->state != BT_CONNECTED)
24824 +               goto drop;
24825 +
24826 +       if (l2cap_pi(sk)->imtu < skb->len)
24827 +               goto drop;
24828 +
24829 +       if (!sock_queue_rcv_skb(sk, skb))
24830 +               goto done;
24831 +
24832 +drop:
24833 +       kfree_skb(skb);
24834 +
24835 +done:
24836 +       if (sk) bh_unlock_sock(sk);
24837 +       return 0;
24838 +}
24839 +
24840 +static void l2cap_recv_frame(struct l2cap_conn *conn, struct sk_buff *skb)
24841 +{
24842 +       l2cap_hdr *lh = (l2cap_hdr *) skb->data;
24843 +       __u16 cid, psm, len;
24844 +
24845 +       skb_pull(skb, L2CAP_HDR_SIZE);
24846 +       cid = __le16_to_cpu(lh->cid);
24847 +       len = __le16_to_cpu(lh->len);
24848 +
24849 +       BT_DBG("len %d, cid 0x%4.4x", len, cid);
24850 +
24851 +       switch (cid) {
24852 +       case 0x0001:
24853 +               l2cap_sig_channel(conn, skb);
24854 +               break;
24855 +
24856 +       case 0x0002:
24857 +               psm = get_unaligned((__u16 *) skb->data);
24858 +               skb_pull(skb, 2);
24859 +               l2cap_conless_channel(conn, psm, skb);
24860 +               break;
24861 +               
24862 +       default:
24863 +               l2cap_data_channel(conn, cid, skb);
24864 +               break;
24865 +       }
24866 +}
24867 +
24868 +/* ------------ L2CAP interface with lower layer (HCI) ------------- */
24869 +
24870 +static int l2cap_connect_ind(struct hci_dev *hdev, bdaddr_t *bdaddr, __u8 type)
24871 +{
24872 +       int exact = 0, lm1 = 0, lm2 = 0;
24873 +       register struct sock *sk;
24874 +
24875 +       if (type != ACL_LINK)
24876 +               return 0;
24877 +
24878 +       BT_DBG("hdev %s, bdaddr %s", hdev->name, batostr(bdaddr));
24879 +
24880 +       /* Find listening sockets and check their link_mode */
24881 +       read_lock(&l2cap_sk_list.lock);
24882 +       for (sk = l2cap_sk_list.head; sk; sk = sk->next) {
24883 +               if (sk->state != BT_LISTEN)
24884 +                       continue;
24885 +
24886 +               if (!bacmp(&bluez_pi(sk)->src, &hdev->bdaddr)) {
24887 +                       lm1 |= (HCI_LM_ACCEPT | l2cap_pi(sk)->link_mode);
24888 +                       exact++;
24889 +               } else if (!bacmp(&bluez_pi(sk)->src, BDADDR_ANY))
24890 +                       lm2 |= (HCI_LM_ACCEPT | l2cap_pi(sk)->link_mode);
24891 +       }
24892 +       read_unlock(&l2cap_sk_list.lock);
24893 +
24894 +       return exact ? lm1 : lm2;
24895 +}
24896 +
24897 +static int l2cap_connect_cfm(struct hci_conn *hcon, __u8 status)
24898 +{
24899 +       BT_DBG("hcon %p bdaddr %s status %d", hcon, batostr(&hcon->dst), status);
24900 +
24901 +       if (hcon->type != ACL_LINK)
24902 +               return 0;
24903 +
24904 +       if (!status) {
24905 +               struct l2cap_conn *conn;
24906 +
24907 +               conn = l2cap_conn_add(hcon, status);
24908 +               if (conn)
24909 +                       l2cap_conn_ready(conn);
24910 +       } else 
24911 +               l2cap_conn_del(hcon, bterr(status));
24912 +       
24913 +       return 0;
24914 +}
24915 +
24916 +static int l2cap_disconn_ind(struct hci_conn *hcon, __u8 reason)
24917 +{
24918 +       BT_DBG("hcon %p reason %d", hcon, reason);
24919 +
24920 +       if (hcon->type != ACL_LINK)
24921 +               return 0;
24922 +
24923 +       l2cap_conn_del(hcon, bterr(reason));
24924 +       return 0;
24925 +}
24926 +
24927 +static int l2cap_auth_cfm(struct hci_conn *hcon, __u8 status)
24928 +{
24929 +       struct l2cap_chan_list *l;
24930 +       struct l2cap_conn *conn;
24931 +       l2cap_conn_rsp rsp;
24932 +       struct sock *sk;
24933 +       int result;
24934 +       
24935 +       if (!(conn = hcon->l2cap_data))
24936 +               return 0;
24937 +       l = &conn->chan_list;
24938 +
24939 +       BT_DBG("conn %p", conn);
24940 +
24941 +       read_lock(&l->lock);
24942 +
24943 +       for (sk = l->head; sk; sk = l2cap_pi(sk)->next_c) {
24944 +               bh_lock_sock(sk);
24945 +
24946 +               if (sk->state != BT_CONNECT2 ||
24947 +                               (l2cap_pi(sk)->link_mode & L2CAP_LM_ENCRYPT)) {
24948 +                       bh_unlock_sock(sk);
24949 +                       continue;
24950 +               }
24951 +
24952 +               if (!status) {
24953 +                       sk->state = BT_CONFIG;
24954 +                       result = 0;
24955 +               } else {
24956 +                       sk->state = BT_DISCONN;
24957 +                       l2cap_sock_set_timer(sk, HZ/10);
24958 +                       result = L2CAP_CR_SEC_BLOCK;
24959 +               }
24960 +
24961 +               rsp.scid   = __cpu_to_le16(l2cap_pi(sk)->dcid);
24962 +               rsp.dcid   = __cpu_to_le16(l2cap_pi(sk)->scid);
24963 +               rsp.result = __cpu_to_le16(result);
24964 +               rsp.status = __cpu_to_le16(0);
24965 +               l2cap_send_rsp(conn, l2cap_pi(sk)->ident, L2CAP_CONN_RSP,
24966 +                       L2CAP_CONN_RSP_SIZE, &rsp);
24967 +
24968 +               bh_unlock_sock(sk);
24969 +       }
24970 +
24971 +       read_unlock(&l->lock);
24972 +       return 0;
24973 +}
24974 +
24975 +static int l2cap_encrypt_cfm(struct hci_conn *hcon, __u8 status)
24976 +{
24977 +       struct l2cap_chan_list *l;
24978 +       struct l2cap_conn *conn;
24979 +       l2cap_conn_rsp rsp;
24980 +       struct sock *sk;
24981 +       int result;
24982 +       
24983 +       if (!(conn = hcon->l2cap_data))
24984 +               return 0;
24985 +       l = &conn->chan_list;
24986 +
24987 +       BT_DBG("conn %p", conn);
24988 +
24989 +       read_lock(&l->lock);
24990 +
24991 +       for (sk = l->head; sk; sk = l2cap_pi(sk)->next_c) {
24992 +               bh_lock_sock(sk);
24993 +
24994 +               if (sk->state != BT_CONNECT2) {
24995 +                       bh_unlock_sock(sk);
24996 +                       continue;
24997 +               }
24998 +
24999 +               if (!status) {
25000 +                       sk->state = BT_CONFIG;
25001 +                       result = 0;
25002 +               } else {
25003 +                       sk->state = BT_DISCONN;
25004 +                       l2cap_sock_set_timer(sk, HZ/10);
25005 +                       result = L2CAP_CR_SEC_BLOCK;
25006 +               }
25007 +
25008 +               rsp.scid   = __cpu_to_le16(l2cap_pi(sk)->dcid);
25009 +               rsp.dcid   = __cpu_to_le16(l2cap_pi(sk)->scid);
25010 +               rsp.result = __cpu_to_le16(result);
25011 +               rsp.status = __cpu_to_le16(0);
25012 +               l2cap_send_rsp(conn, l2cap_pi(sk)->ident, L2CAP_CONN_RSP, 
25013 +                       L2CAP_CONN_RSP_SIZE, &rsp);
25014 +
25015 +               bh_unlock_sock(sk);
25016 +       }
25017 +
25018 +       read_unlock(&l->lock);
25019 +       return 0;
25020 +}
25021 +
25022 +static int l2cap_recv_acldata(struct hci_conn *hcon, struct sk_buff *skb, __u16 flags)
25023 +{
25024 +       struct l2cap_conn *conn = hcon->l2cap_data;
25025 +
25026 +       if (!conn && !(conn = l2cap_conn_add(hcon, 0)))
25027 +               goto drop;
25028 +
25029 +       BT_DBG("conn %p len %d flags 0x%x", conn, skb->len, flags);
25030 +
25031 +       if (flags & ACL_START) {
25032 +               l2cap_hdr *hdr;
25033 +               int len;
25034 +
25035 +               if (conn->rx_len) {
25036 +                       BT_ERR("Unexpected start frame (len %d)", skb->len);
25037 +                       kfree_skb(conn->rx_skb);
25038 +                       conn->rx_skb = NULL;
25039 +                       conn->rx_len = 0;
25040 +                       l2cap_conn_unreliable(conn, ECOMM);
25041 +               }
25042 +
25043 +               if (skb->len < 2) {
25044 +                       BT_ERR("Frame is too short (len %d)", skb->len);
25045 +                       l2cap_conn_unreliable(conn, ECOMM);
25046 +                       goto drop;
25047 +               }
25048 +
25049 +               hdr = (l2cap_hdr *) skb->data;
25050 +               len = __le16_to_cpu(hdr->len) + L2CAP_HDR_SIZE;
25051 +
25052 +               if (len == skb->len) {
25053 +                       /* Complete frame received */
25054 +                       l2cap_recv_frame(conn, skb);
25055 +                       return 0;
25056 +               }
25057 +
25058 +               BT_DBG("Start: total len %d, frag len %d", len, skb->len);
25059 +
25060 +               if (skb->len > len) {
25061 +                       BT_ERR("Frame is too long (len %d, expected len %d)",
25062 +                               skb->len, len);
25063 +                       l2cap_conn_unreliable(conn, ECOMM);
25064 +                       goto drop;
25065 +               }
25066 +
25067 +               /* Allocate skb for the complete frame including header */
25068 +               conn->rx_skb = bluez_skb_alloc(len, GFP_ATOMIC);
25069 +               if (!conn->rx_skb)
25070 +                       goto drop;
25071 +
25072 +               memcpy(skb_put(conn->rx_skb, skb->len), skb->data, skb->len);
25073 +               conn->rx_len = len - skb->len;
25074 +       } else {
25075 +               BT_DBG("Cont: frag len %d (expecting %d)", skb->len, conn->rx_len);
25076 +
25077 +               if (!conn->rx_len) {
25078 +                       BT_ERR("Unexpected continuation frame (len %d)", skb->len);
25079 +                       l2cap_conn_unreliable(conn, ECOMM);
25080 +                       goto drop;
25081 +               }
25082 +
25083 +               if (skb->len > conn->rx_len) {
25084 +                       BT_ERR("Fragment is too long (len %d, expected %d)",
25085 +                                       skb->len, conn->rx_len);
25086 +                       kfree_skb(conn->rx_skb);
25087 +                       conn->rx_skb = NULL;
25088 +                       conn->rx_len = 0;
25089 +                       l2cap_conn_unreliable(conn, ECOMM);
25090 +                       goto drop;
25091 +               }
25092 +
25093 +               memcpy(skb_put(conn->rx_skb, skb->len), skb->data, skb->len);
25094 +               conn->rx_len -= skb->len;
25095 +
25096 +               if (!conn->rx_len) {
25097 +                       /* Complete frame received */
25098 +                       l2cap_recv_frame(conn, conn->rx_skb);
25099 +                       conn->rx_skb = NULL;
25100 +               }
25101 +       }
25102 +
25103 +drop:
25104 +       kfree_skb(skb);
25105 +       return 0;
25106 +}
25107 +
25108 +/* ----- Proc fs support ------ */
25109 +static int l2cap_sock_dump(char *buf, struct bluez_sock_list *list)
25110 +{
25111 +       struct l2cap_pinfo *pi;
25112 +       struct sock *sk;
25113 +       char *ptr = buf;
25114 +
25115 +       read_lock_bh(&list->lock);
25116 +
25117 +       for (sk = list->head; sk; sk = sk->next) {
25118 +               pi = l2cap_pi(sk);
25119 +               ptr += sprintf(ptr, "%s %s %d %d 0x%4.4x 0x%4.4x %d %d 0x%x\n",
25120 +                               batostr(&bluez_pi(sk)->src), batostr(&bluez_pi(sk)->dst), 
25121 +                               sk->state, pi->psm, pi->scid, pi->dcid, pi->imtu, pi->omtu,
25122 +                               pi->link_mode);
25123 +       }
25124 +
25125 +       read_unlock_bh(&list->lock);
25126 +
25127 +       ptr += sprintf(ptr, "\n");
25128 +       return ptr - buf;
25129 +}
25130 +
25131 +static int l2cap_read_proc(char *buf, char **start, off_t offset, int count, int *eof, void *priv)
25132 +{
25133 +       char *ptr = buf;
25134 +       int len;
25135 +
25136 +       BT_DBG("count %d, offset %ld", count, offset);
25137 +
25138 +       ptr += l2cap_sock_dump(ptr, &l2cap_sk_list);
25139 +       len  = ptr - buf;
25140 +
25141 +       if (len <= count + offset)
25142 +               *eof = 1;
25143 +
25144 +       *start = buf + offset;
25145 +       len -= offset;
25146 +
25147 +       if (len > count)
25148 +               len = count;
25149 +       if (len < 0)
25150 +               len = 0;
25151 +
25152 +       return len;
25153 +}
25154 +
25155 +static struct proto_ops l2cap_sock_ops = {
25156 +       family:         PF_BLUETOOTH,
25157 +       release:        l2cap_sock_release,
25158 +       bind:           l2cap_sock_bind,
25159 +       connect:        l2cap_sock_connect,
25160 +       listen:         l2cap_sock_listen,
25161 +       accept:         l2cap_sock_accept,
25162 +       getname:        l2cap_sock_getname,
25163 +       sendmsg:        l2cap_sock_sendmsg,
25164 +       recvmsg:        bluez_sock_recvmsg,
25165 +       poll:           bluez_sock_poll,
25166 +       socketpair:     sock_no_socketpair,
25167 +       ioctl:          sock_no_ioctl,
25168 +       shutdown:       l2cap_sock_shutdown,
25169 +       setsockopt:     l2cap_sock_setsockopt,
25170 +       getsockopt:     l2cap_sock_getsockopt,
25171 +       mmap:           sock_no_mmap
25172 +};
25173 +
25174 +static struct net_proto_family l2cap_sock_family_ops = {
25175 +       family:         PF_BLUETOOTH,
25176 +       create:         l2cap_sock_create
25177 +};
25178 +
25179 +static struct hci_proto l2cap_hci_proto = {
25180 +       name:           "L2CAP",
25181 +       id:             HCI_PROTO_L2CAP,
25182 +       connect_ind:    l2cap_connect_ind,
25183 +       connect_cfm:    l2cap_connect_cfm,
25184 +       disconn_ind:    l2cap_disconn_ind,
25185 +       recv_acldata:   l2cap_recv_acldata,
25186 +       auth_cfm:       l2cap_auth_cfm,
25187 +       encrypt_cfm:    l2cap_encrypt_cfm
25188 +};
25189 +
25190 +int __init l2cap_init(void)
25191 +{
25192 +       int err;
25193 +
25194 +       if ((err = bluez_sock_register(BTPROTO_L2CAP, &l2cap_sock_family_ops))) {
25195 +               BT_ERR("Can't register L2CAP socket");
25196 +               return err;
25197 +       }
25198 +
25199 +       if ((err = hci_register_proto(&l2cap_hci_proto))) {
25200 +               BT_ERR("Can't register L2CAP protocol");
25201 +               return err;
25202 +       }
25203 +
25204 +       create_proc_read_entry("bluetooth/l2cap", 0, 0, l2cap_read_proc, NULL);
25205 +
25206 +       BT_INFO("BlueZ L2CAP ver %s Copyright (C) 2000,2001 Qualcomm Inc", VERSION);
25207 +       BT_INFO("Written 2000,2001 by Maxim Krasnyansky <maxk@qualcomm.com>");
25208 +       return 0;
25209 +}
25210 +
25211 +void l2cap_cleanup(void)
25212 +{
25213 +       remove_proc_entry("bluetooth/l2cap", NULL);
25214 +
25215 +       /* Unregister socket and protocol */
25216 +       if (bluez_sock_unregister(BTPROTO_L2CAP))
25217 +               BT_ERR("Can't unregister L2CAP socket");
25218 +
25219 +       if (hci_unregister_proto(&l2cap_hci_proto))
25220 +               BT_ERR("Can't unregister L2CAP protocol");
25221 +}
25222 +
25223 +void l2cap_load(void)
25224 +{
25225 +       /* Dummy function to trigger automatic L2CAP module loading by 
25226 +          other modules that use L2CAP sockets but do not use any other
25227 +          symbols from it. */
25228 +       return;
25229 +}
25230 +
25231 +EXPORT_SYMBOL(l2cap_load);
25232 +
25233 +module_init(l2cap_init);
25234 +module_exit(l2cap_cleanup);
25235 +
25236 +MODULE_AUTHOR("Maxim Krasnyansky <maxk@qualcomm.com>");
25237 +MODULE_DESCRIPTION("BlueZ L2CAP ver " VERSION);
25238 +MODULE_LICENSE("GPL");
25239 diff -urN linux-2.4.18/net/bluetooth/l2cap_core.c linux-2.4.18-mh15/net/bluetooth/l2cap_core.c
25240 --- linux-2.4.18/net/bluetooth/l2cap_core.c     2001-09-30 21:26:08.000000000 +0200
25241 +++ linux-2.4.18-mh15/net/bluetooth/l2cap_core.c        1970-01-01 01:00:00.000000000 +0100
25242 @@ -1,2316 +0,0 @@
25243 -/* 
25244 -   BlueZ - Bluetooth protocol stack for Linux
25245 -   Copyright (C) 2000-2001 Qualcomm Incorporated
25246 -
25247 -   Written 2000,2001 by Maxim Krasnyansky <maxk@qualcomm.com>
25248 -
25249 -   This program is free software; you can redistribute it and/or modify
25250 -   it under the terms of the GNU General Public License version 2 as
25251 -   published by the Free Software Foundation;
25252 -
25253 -   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
25254 -   OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25255 -   FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS.
25256 -   IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) AND AUTHOR(S) BE LIABLE FOR ANY
25257 -   CLAIM, OR ANY SPECIAL INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES 
25258 -   WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 
25259 -   ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 
25260 -   OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
25261 -
25262 -   ALL LIABILITY, INCLUDING LIABILITY FOR INFRINGEMENT OF ANY PATENTS, 
25263 -   COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS, RELATING TO USE OF THIS 
25264 -   SOFTWARE IS DISCLAIMED.
25265 -*/
25266 -
25267 -/*
25268 - * BlueZ L2CAP core and sockets.
25269 - *
25270 - * $Id: l2cap_core.c,v 1.19 2001/08/03 04:19:50 maxk Exp $
25271 - */
25272 -#define VERSION "1.1"
25273 -
25274 -#include <linux/config.h>
25275 -#include <linux/module.h>
25276 -
25277 -#include <linux/types.h>
25278 -#include <linux/errno.h>
25279 -#include <linux/kernel.h>
25280 -#include <linux/major.h>
25281 -#include <linux/sched.h>
25282 -#include <linux/slab.h>
25283 -#include <linux/poll.h>
25284 -#include <linux/fcntl.h>
25285 -#include <linux/init.h>
25286 -#include <linux/skbuff.h>
25287 -#include <linux/interrupt.h>
25288 -#include <linux/socket.h>
25289 -#include <linux/skbuff.h>
25290 -#include <linux/proc_fs.h>
25291 -#include <linux/list.h>
25292 -#include <net/sock.h>
25293 -
25294 -#include <asm/system.h>
25295 -#include <asm/uaccess.h>
25296 -
25297 -#include <net/bluetooth/bluetooth.h>
25298 -#include <net/bluetooth/bluez.h>
25299 -#include <net/bluetooth/hci_core.h>
25300 -#include <net/bluetooth/l2cap.h>
25301 -#include <net/bluetooth/l2cap_core.h>
25302 -
25303 -#ifndef L2CAP_DEBUG
25304 -#undef  DBG
25305 -#define DBG( A... )
25306 -#endif
25307 -
25308 -struct proto_ops l2cap_sock_ops;
25309 -
25310 -struct bluez_sock_list l2cap_sk_list = {
25311 -       lock: RW_LOCK_UNLOCKED
25312 -};
25313 -
25314 -struct list_head l2cap_iff_list = LIST_HEAD_INIT(l2cap_iff_list);
25315 -rwlock_t l2cap_rt_lock = RW_LOCK_UNLOCKED;
25316 -
25317 -static int  l2cap_conn_del(struct l2cap_conn *conn, int err);
25318 -
25319 -static inline void l2cap_chan_add(struct l2cap_conn *conn, struct sock *sk, struct sock *parent);
25320 -static void l2cap_chan_del(struct sock *sk, int err);
25321 -static int  l2cap_chan_send(struct sock *sk, struct msghdr *msg, int len);
25322 -
25323 -static void l2cap_sock_close(struct sock *sk);
25324 -static void l2cap_sock_kill(struct sock *sk);
25325 -
25326 -static int l2cap_send_req(struct l2cap_conn *conn, __u8 code, __u16 len, void *data);
25327 -static int l2cap_send_rsp(struct l2cap_conn *conn, __u8 ident, __u8 code, __u16 len, void *data);
25328 -
25329 -/* -------- L2CAP interfaces & routing --------- */
25330 -/* Add/delete L2CAP interface.
25331 - * Must be called with locked rt_lock
25332 - */ 
25333 -
25334 -static void l2cap_iff_add(struct hci_dev *hdev)
25335 -{
25336 -       struct l2cap_iff *iff;
25337 -
25338 -       DBG("%s", hdev->name);
25339 -
25340 -       DBG("iff_list %p next %p prev %p", &l2cap_iff_list, l2cap_iff_list.next, l2cap_iff_list.prev);
25341 -
25342 -       /* Allocate new interface and lock HCI device */
25343 -       if (!(iff = kmalloc(sizeof(struct l2cap_iff), GFP_KERNEL))) {
25344 -               ERR("Can't allocate new interface %s", hdev->name);
25345 -               return;
25346 -       }
25347 -       memset(iff, 0, sizeof(struct l2cap_iff));
25348 -
25349 -       hci_dev_hold(hdev);
25350 -       hdev->l2cap_data = iff;
25351 -       iff->hdev   = hdev;
25352 -       iff->mtu    = hdev->acl_mtu - HCI_ACL_HDR_SIZE;
25353 -       iff->bdaddr = &hdev->bdaddr;
25354 -
25355 -       spin_lock_init(&iff->lock);
25356 -       INIT_LIST_HEAD(&iff->conn_list);
25357 -
25358 -       list_add(&iff->list, &l2cap_iff_list);
25359 -}
25360 -
25361 -static void l2cap_iff_del(struct hci_dev *hdev)
25362 -{
25363 -       struct l2cap_iff *iff;
25364 -
25365 -       if (!(iff = hdev->l2cap_data))
25366 -               return;
25367 -
25368 -       DBG("%s iff %p", hdev->name, iff);
25369 -
25370 -       list_del(&iff->list);
25371 -
25372 -       l2cap_iff_lock(iff);
25373 -
25374 -       /* Drop connections */
25375 -       while (!list_empty(&iff->conn_list)) {
25376 -               struct l2cap_conn *c;
25377 -
25378 -               c = list_entry(iff->conn_list.next, struct l2cap_conn, list);
25379 -               l2cap_conn_del(c, ENODEV);
25380 -       }
25381 -
25382 -       l2cap_iff_unlock(iff);
25383 -
25384 -       /* Unlock HCI device */
25385 -       hdev->l2cap_data = NULL;
25386 -       hci_dev_put(hdev);
25387 -
25388 -       kfree(iff);
25389 -}
25390 -
25391 -/* Get route. Returns L2CAP interface.
25392 - * Must be called with locked rt_lock
25393 - */
25394 -static struct l2cap_iff *l2cap_get_route(bdaddr_t *src, bdaddr_t *dst)
25395 -{
25396 -       struct list_head *p;
25397 -       int use_src;
25398 -
25399 -       DBG("%s -> %s", batostr(src), batostr(dst));
25400 -
25401 -       use_src = bacmp(src, BDADDR_ANY) ? 0 : 1;
25402 -       
25403 -       /* Simple routing: 
25404 -        *      No source address - find interface with bdaddr != dst 
25405 -        *      Source address    - find interface with bdaddr == src 
25406 -        */
25407 -
25408 -       list_for_each(p, &l2cap_iff_list) {
25409 -               struct l2cap_iff *iff;
25410 -
25411 -               iff = list_entry(p, struct l2cap_iff, list);
25412 -
25413 -               if (use_src && !bacmp(iff->bdaddr, src))
25414 -                       return iff;
25415 -               else if (bacmp(iff->bdaddr, dst))
25416 -                       return iff;
25417 -       }
25418 -       return NULL;
25419 -}
25420 -
25421 -/* ----- L2CAP timers ------ */
25422 -static void l2cap_sock_timeout(unsigned long arg)
25423 -{
25424 -       struct sock *sk = (struct sock *) arg;
25425 -
25426 -       DBG("sock %p state %d", sk, sk->state);
25427 -
25428 -       bh_lock_sock(sk);
25429 -       switch (sk->state) {
25430 -       case BT_DISCONN:
25431 -               l2cap_chan_del(sk, ETIMEDOUT);
25432 -               break;
25433 -
25434 -       default:
25435 -               sk->err = ETIMEDOUT;
25436 -               sk->state_change(sk);
25437 -               break;
25438 -       };
25439 -       bh_unlock_sock(sk);
25440 -
25441 -       l2cap_sock_kill(sk);
25442 -       sock_put(sk);
25443 -}
25444 -
25445 -static void l2cap_sock_set_timer(struct sock *sk, long timeout)
25446 -{
25447 -       DBG("sock %p state %d timeout %ld", sk, sk->state, timeout);
25448 -
25449 -       if (!mod_timer(&sk->timer, jiffies + timeout))
25450 -               sock_hold(sk);
25451 -}
25452 -
25453 -static void l2cap_sock_clear_timer(struct sock *sk)
25454 -{
25455 -       DBG("sock %p state %d", sk, sk->state);
25456 -
25457 -       if (timer_pending(&sk->timer) && del_timer(&sk->timer))
25458 -               __sock_put(sk);
25459 -}
25460 -
25461 -static void l2cap_sock_init_timer(struct sock *sk)
25462 -{
25463 -       init_timer(&sk->timer);
25464 -       sk->timer.function = l2cap_sock_timeout;
25465 -       sk->timer.data = (unsigned long)sk;
25466 -}
25467 -
25468 -static void l2cap_conn_timeout(unsigned long arg)
25469 -{
25470 -       struct l2cap_conn *conn = (void *)arg;
25471 -       
25472 -       DBG("conn %p state %d", conn, conn->state);
25473 -
25474 -       if (conn->state == BT_CONNECTED) {
25475 -               hci_disconnect(conn->hconn, 0x13);
25476 -       }
25477 -               
25478 -       return;
25479 -}
25480 -
25481 -static void l2cap_conn_set_timer(struct l2cap_conn *conn, long timeout)
25482 -{
25483 -       DBG("conn %p state %d timeout %ld", conn, conn->state, timeout);
25484 -
25485 -       mod_timer(&conn->timer, jiffies + timeout);
25486 -}
25487 -
25488 -static void l2cap_conn_clear_timer(struct l2cap_conn *conn)
25489 -{
25490 -       DBG("conn %p state %d", conn, conn->state);
25491 -
25492 -       del_timer(&conn->timer);
25493 -}
25494 -
25495 -static void l2cap_conn_init_timer(struct l2cap_conn *conn)
25496 -{
25497 -       init_timer(&conn->timer);
25498 -       conn->timer.function = l2cap_conn_timeout;
25499 -       conn->timer.data = (unsigned long)conn;
25500 -}
25501 -
25502 -/* -------- L2CAP connections --------- */
25503 -/* Add new connection to the interface.
25504 - * Interface must be locked
25505 - */
25506 -static struct l2cap_conn *l2cap_conn_add(struct l2cap_iff *iff, bdaddr_t *dst)
25507 -{
25508 -       struct l2cap_conn *conn;
25509 -       bdaddr_t *src = iff->bdaddr;
25510 -
25511 -       if (!(conn = kmalloc(sizeof(struct l2cap_conn), GFP_KERNEL)))
25512 -               return NULL;
25513 -
25514 -       memset(conn, 0, sizeof(struct l2cap_conn));
25515 -
25516 -       conn->state = BT_OPEN;
25517 -       conn->iff   = iff;
25518 -       bacpy(&conn->src, src);
25519 -       bacpy(&conn->dst, dst);
25520 -
25521 -       spin_lock_init(&conn->lock);
25522 -       conn->chan_list.lock = RW_LOCK_UNLOCKED;
25523 -
25524 -       l2cap_conn_init_timer(conn);
25525 -       
25526 -       __l2cap_conn_link(iff, conn);
25527 -
25528 -       DBG("%s -> %s, %p", batostr(src), batostr(dst), conn);
25529 -
25530 -       MOD_INC_USE_COUNT;
25531 -
25532 -       return conn;
25533 -}
25534 -
25535 -/* Delete connection on the interface.
25536 - * Interface must be locked
25537 - */
25538 -static int l2cap_conn_del(struct l2cap_conn *conn, int err)
25539 -{
25540 -       struct sock *sk;
25541 -
25542 -       DBG("conn %p, state %d, err %d", conn, conn->state, err);
25543 -
25544 -       l2cap_conn_clear_timer(conn);
25545 -       __l2cap_conn_unlink(conn->iff, conn);
25546 -
25547 -       conn->state = BT_CLOSED;
25548 -
25549 -       if (conn->rx_skb)
25550 -               kfree_skb(conn->rx_skb);
25551 -
25552 -       /* Kill channels */
25553 -       while ((sk = conn->chan_list.head)) {
25554 -               bh_lock_sock(sk);
25555 -               l2cap_sock_clear_timer(sk);
25556 -               l2cap_chan_del(sk, err);
25557 -               bh_unlock_sock(sk);
25558 -
25559 -               l2cap_sock_kill(sk);
25560 -       }
25561 -
25562 -       kfree(conn);
25563 -
25564 -       MOD_DEC_USE_COUNT;
25565 -       return 0;
25566 -}
25567 -
25568 -static inline struct l2cap_conn *l2cap_get_conn_by_addr(struct l2cap_iff *iff, bdaddr_t *dst)
25569 -{
25570 -       struct list_head *p;
25571 -
25572 -       list_for_each(p, &iff->conn_list) {
25573 -               struct l2cap_conn *c;
25574 -
25575 -               c = list_entry(p, struct l2cap_conn, list);
25576 -               if (!bacmp(&c->dst, dst))
25577 -                       return c;
25578 -       }
25579 -       return NULL;
25580 -}
25581 -
25582 -int l2cap_connect(struct sock *sk)
25583 -{
25584 -       bdaddr_t *src = &l2cap_pi(sk)->src;
25585 -       bdaddr_t *dst = &l2cap_pi(sk)->dst;
25586 -       struct l2cap_conn *conn;
25587 -       struct l2cap_iff *iff;
25588 -       int err = 0;
25589 -
25590 -       DBG("%s -> %s psm 0x%2.2x", batostr(src), batostr(dst), l2cap_pi(sk)->psm);
25591 -
25592 -       read_lock_bh(&l2cap_rt_lock);
25593 -
25594 -       /* Get route to remote BD address */
25595 -       if (!(iff = l2cap_get_route(src, dst))) {
25596 -               err = -EHOSTUNREACH;
25597 -               goto done;
25598 -       }
25599 -
25600 -       /* Update source addr of the socket */
25601 -       bacpy(src, iff->bdaddr);
25602 -
25603 -       l2cap_iff_lock(iff);
25604 -
25605 -       if (!(conn = l2cap_get_conn_by_addr(iff, dst))) {
25606 -               /* Connection doesn't exist */
25607 -               if (!(conn = l2cap_conn_add(iff, dst))) {
25608 -                       l2cap_iff_unlock(iff);
25609 -                       err = -ENOMEM;
25610 -                       goto done;
25611 -               }
25612 -               conn->out = 1;
25613 -       }
25614 -
25615 -       l2cap_iff_unlock(iff);
25616 -
25617 -       l2cap_chan_add(conn, sk, NULL);
25618 -
25619 -       sk->state = BT_CONNECT;
25620 -       l2cap_sock_set_timer(sk, sk->sndtimeo);
25621 -
25622 -       switch (conn->state) {
25623 -       case BT_CONNECTED:
25624 -               if (sk->type == SOCK_SEQPACKET) {
25625 -                       l2cap_conn_req req;
25626 -                       req.scid = __cpu_to_le16(l2cap_pi(sk)->scid);
25627 -                       req.psm  = l2cap_pi(sk)->psm;
25628 -                       l2cap_send_req(conn, L2CAP_CONN_REQ, L2CAP_CONN_REQ_SIZE, &req);
25629 -               } else {
25630 -                       l2cap_sock_clear_timer(sk);
25631 -                       sk->state = BT_CONNECTED;
25632 -               }
25633 -               break;
25634 -
25635 -       case BT_CONNECT:
25636 -               break;
25637 -
25638 -       default:
25639 -               /* Create ACL connection */
25640 -               conn->state = BT_CONNECT;
25641 -               hci_connect(iff->hdev, dst);
25642 -               break;
25643 -       };
25644 -
25645 -done:
25646 -       read_unlock_bh(&l2cap_rt_lock);
25647 -       return err;
25648 -}
25649 -
25650 -/* ------ Channel queues for listening sockets ------ */
25651 -void l2cap_accept_queue(struct sock *parent, struct sock *sk)
25652 -{
25653 -       struct l2cap_accept_q *q = &l2cap_pi(parent)->accept_q;
25654 -
25655 -       DBG("parent %p, sk %p", parent, sk);
25656 -
25657 -       sock_hold(sk);
25658 -       l2cap_pi(sk)->parent = parent;
25659 -       l2cap_pi(sk)->next_q = NULL;
25660 -
25661 -       if (!q->head) {
25662 -               q->head = q->tail = sk;
25663 -       } else {
25664 -               struct sock *tail = q->tail;
25665 -
25666 -               l2cap_pi(sk)->prev_q = tail;
25667 -               l2cap_pi(tail)->next_q = sk;
25668 -               q->tail = sk;
25669 -       }
25670 -
25671 -       parent->ack_backlog++;
25672 -}
25673 -
25674 -void l2cap_accept_unlink(struct sock *sk)
25675 -{
25676 -       struct sock *parent = l2cap_pi(sk)->parent;
25677 -       struct l2cap_accept_q *q = &l2cap_pi(parent)->accept_q;
25678 -       struct sock *next, *prev;
25679 -
25680 -       DBG("sk %p", sk);
25681 -
25682 -       next = l2cap_pi(sk)->next_q;
25683 -       prev = l2cap_pi(sk)->prev_q;
25684 -
25685 -       if (sk == q->head)
25686 -               q->head = next;
25687 -       if (sk == q->tail)
25688 -               q->tail = prev;
25689 -
25690 -       if (next)
25691 -               l2cap_pi(next)->prev_q = prev;
25692 -       if (prev)
25693 -               l2cap_pi(prev)->next_q = next;
25694 -
25695 -       l2cap_pi(sk)->parent = NULL;
25696 -
25697 -       parent->ack_backlog--;
25698 -       __sock_put(sk);
25699 -}
25700 -
25701 -/* Get next connected channel in queue. */
25702 -struct sock *l2cap_accept_dequeue(struct sock *parent, int state)
25703 -{
25704 -       struct l2cap_accept_q *q = &l2cap_pi(parent)->accept_q;
25705 -       struct sock *sk;
25706 -
25707 -       for (sk = q->head; sk; sk = l2cap_pi(sk)->next_q) {
25708 -               if (!state || sk->state == state) {
25709 -                       l2cap_accept_unlink(sk);
25710 -                       break;
25711 -               }
25712 -       }
25713 -
25714 -       DBG("parent %p, sk %p", parent, sk);
25715 -
25716 -       return sk;
25717 -}
25718 -
25719 -/* -------- Socket interface ---------- */
25720 -static struct sock *__l2cap_get_sock_by_addr(struct sockaddr_l2 *addr)
25721 -{
25722 -       bdaddr_t *src = &addr->l2_bdaddr;
25723 -       __u16 psm = addr->l2_psm;
25724 -       struct sock *sk;
25725 -
25726 -       for (sk = l2cap_sk_list.head; sk; sk = sk->next) {
25727 -               if (l2cap_pi(sk)->psm == psm &&
25728 -                   !bacmp(&l2cap_pi(sk)->src, src))
25729 -                       break;
25730 -       }
25731 -
25732 -       return sk;
25733 -}
25734 -
25735 -/* Find socket listening on psm and source bdaddr.
25736 - * Returns closest match.
25737 - */
25738 -static struct sock *l2cap_get_sock_listen(bdaddr_t *src, __u16 psm)
25739 -{
25740 -       struct sock *sk, *sk1 = NULL;
25741 -
25742 -       read_lock(&l2cap_sk_list.lock);
25743 -
25744 -       for (sk = l2cap_sk_list.head; sk; sk = sk->next) {
25745 -               struct l2cap_pinfo *pi;
25746 -
25747 -               if (sk->state != BT_LISTEN)
25748 -                       continue;
25749 -
25750 -               pi = l2cap_pi(sk);
25751 -
25752 -               if (pi->psm == psm) {
25753 -                       /* Exact match. */
25754 -                       if (!bacmp(&pi->src, src))
25755 -                               break;
25756 -
25757 -                       /* Closest match */
25758 -                       if (!bacmp(&pi->src, BDADDR_ANY))
25759 -                               sk1 = sk;
25760 -               }
25761 -       }
25762 -
25763 -       read_unlock(&l2cap_sk_list.lock);
25764 -
25765 -       return sk ? sk : sk1;
25766 -}
25767 -
25768 -static void l2cap_sock_destruct(struct sock *sk)
25769 -{
25770 -       DBG("sk %p", sk);
25771 -
25772 -       skb_queue_purge(&sk->receive_queue);
25773 -       skb_queue_purge(&sk->write_queue);
25774 -
25775 -       MOD_DEC_USE_COUNT;
25776 -}
25777 -
25778 -static void l2cap_sock_cleanup_listen(struct sock *parent)
25779 -{
25780 -       struct sock *sk;
25781 -
25782 -       DBG("parent %p", parent);
25783 -
25784 -       /* Close not yet accepted channels */
25785 -       while ((sk = l2cap_accept_dequeue(parent, 0)))
25786 -               l2cap_sock_close(sk);
25787 -
25788 -       parent->state  = BT_CLOSED;
25789 -       parent->zapped = 1;
25790 -}
25791 -
25792 -/* Kill socket (only if zapped and orphan)
25793 - * Must be called on unlocked socket.
25794 - */
25795 -static void l2cap_sock_kill(struct sock *sk)
25796 -{
25797 -       if (!sk->zapped || sk->socket)
25798 -               return;
25799 -
25800 -       DBG("sk %p state %d", sk, sk->state);
25801 -
25802 -       /* Kill poor orphan */
25803 -       bluez_sock_unlink(&l2cap_sk_list, sk);
25804 -       sk->dead = 1;
25805 -       sock_put(sk);
25806 -}
25807 -
25808 -/* Close socket.
25809 - * Must be called on unlocked socket.
25810 - */
25811 -static void l2cap_sock_close(struct sock *sk)
25812 -{
25813 -       struct l2cap_conn *conn;
25814 -
25815 -       l2cap_sock_clear_timer(sk);
25816 -
25817 -       lock_sock(sk);
25818 -
25819 -       conn = l2cap_pi(sk)->conn;
25820 -
25821 -       DBG("sk %p state %d conn %p socket %p", sk, sk->state, conn, sk->socket);
25822 -
25823 -       switch (sk->state) {
25824 -       case BT_LISTEN:
25825 -               l2cap_sock_cleanup_listen(sk);
25826 -               break;
25827 -
25828 -       case BT_CONNECTED:
25829 -       case BT_CONFIG:
25830 -               if (sk->type == SOCK_SEQPACKET) {
25831 -                       l2cap_disconn_req req;
25832 -
25833 -                       sk->state = BT_DISCONN;
25834 -
25835 -                       req.dcid = __cpu_to_le16(l2cap_pi(sk)->dcid);
25836 -                       req.scid = __cpu_to_le16(l2cap_pi(sk)->scid);
25837 -                       l2cap_send_req(conn, L2CAP_DISCONN_REQ, L2CAP_DISCONN_REQ_SIZE, &req);
25838 -
25839 -                       l2cap_sock_set_timer(sk, sk->sndtimeo);
25840 -               } else {
25841 -                       l2cap_chan_del(sk, ECONNRESET);
25842 -               }
25843 -               break;
25844 -
25845 -       case BT_CONNECT:
25846 -       case BT_DISCONN:
25847 -               l2cap_chan_del(sk, ECONNRESET);
25848 -               break;
25849 -
25850 -       default:
25851 -               sk->zapped = 1;
25852 -               break;
25853 -       };
25854 -
25855 -       release_sock(sk);
25856 -
25857 -       l2cap_sock_kill(sk);
25858 -}
25859 -
25860 -static void l2cap_sock_init(struct sock *sk, struct sock *parent)
25861 -{
25862 -       struct l2cap_pinfo *pi = l2cap_pi(sk);
25863 -
25864 -       DBG("sk %p", sk);
25865 -
25866 -       if (parent) {
25867 -               sk->type = parent->type;
25868 -
25869 -               pi->imtu = l2cap_pi(parent)->imtu;
25870 -               pi->omtu = l2cap_pi(parent)->omtu;
25871 -       } else {
25872 -               pi->imtu = L2CAP_DEFAULT_MTU;
25873 -               pi->omtu = 0;
25874 -       }
25875 -
25876 -       /* Default config options */
25877 -       pi->conf_mtu = L2CAP_DEFAULT_MTU;
25878 -       pi->flush_to = L2CAP_DEFAULT_FLUSH_TO;
25879 -}
25880 -
25881 -static struct sock *l2cap_sock_alloc(struct socket *sock, int proto, int prio)
25882 -{
25883 -       struct sock *sk;
25884 -
25885 -       if (!(sk = sk_alloc(PF_BLUETOOTH, prio, 1)))
25886 -               return NULL;
25887 -
25888 -       sock_init_data(sock, sk);
25889 -
25890 -       sk->zapped   = 0;
25891 -
25892 -       sk->destruct = l2cap_sock_destruct;
25893 -       sk->sndtimeo = L2CAP_CONN_TIMEOUT;
25894 -
25895 -       sk->protocol = proto;
25896 -       sk->state    = BT_OPEN;
25897 -
25898 -       l2cap_sock_init_timer(sk);
25899 -
25900 -       bluez_sock_link(&l2cap_sk_list, sk);
25901 -
25902 -       MOD_INC_USE_COUNT;
25903 -
25904 -       return sk;
25905 -}
25906 -
25907 -static int l2cap_sock_create(struct socket *sock, int protocol)
25908 -{
25909 -       struct sock *sk;
25910 -
25911 -       DBG("sock %p", sock);
25912 -
25913 -       sock->state = SS_UNCONNECTED;
25914 -
25915 -       if (sock->type != SOCK_SEQPACKET && sock->type != SOCK_RAW)
25916 -               return -ESOCKTNOSUPPORT;
25917 -
25918 -       sock->ops = &l2cap_sock_ops;
25919 -
25920 -       if (!(sk = l2cap_sock_alloc(sock, protocol, GFP_KERNEL)))
25921 -               return -ENOMEM;
25922 -
25923 -       l2cap_sock_init(sk, NULL);
25924 -
25925 -       return 0;
25926 -}
25927 -
25928 -static int l2cap_sock_bind(struct socket *sock, struct sockaddr *addr, int addr_len)
25929 -{
25930 -       struct sockaddr_l2 *la = (struct sockaddr_l2 *) addr;
25931 -       struct sock *sk = sock->sk;
25932 -       int err = 0;
25933 -
25934 -       DBG("sk %p, %s %d", sk, batostr(&la->l2_bdaddr), la->l2_psm);
25935 -
25936 -       if (!addr || addr->sa_family != AF_BLUETOOTH)
25937 -               return -EINVAL;
25938 -
25939 -       lock_sock(sk);
25940 -
25941 -       if (sk->state != BT_OPEN) {
25942 -               err = -EBADFD;
25943 -               goto done;
25944 -       }
25945 -
25946 -       write_lock(&l2cap_sk_list.lock);
25947 -
25948 -       if (la->l2_psm && __l2cap_get_sock_by_addr(la)) {
25949 -               err = -EADDRINUSE;
25950 -               goto unlock;
25951 -       }
25952 -
25953 -       /* Save source address */
25954 -       bacpy(&l2cap_pi(sk)->src, &la->l2_bdaddr);
25955 -       l2cap_pi(sk)->psm = la->l2_psm;
25956 -       sk->state = BT_BOUND;
25957 -
25958 -unlock:
25959 -       write_unlock(&l2cap_sk_list.lock);
25960 -
25961 -done:
25962 -       release_sock(sk);
25963 -
25964 -       return err;
25965 -}
25966 -
25967 -static int l2cap_sock_w4_connect(struct sock *sk, int flags)
25968 -{
25969 -       DECLARE_WAITQUEUE(wait, current);
25970 -       long timeo = sock_sndtimeo(sk, flags & O_NONBLOCK);
25971 -       int err = 0;
25972 -
25973 -       DBG("sk %p", sk);
25974 -
25975 -       add_wait_queue(sk->sleep, &wait);
25976 -       current->state = TASK_INTERRUPTIBLE;
25977 -
25978 -       while (sk->state != BT_CONNECTED) {
25979 -               if (!timeo) {
25980 -                       err = -EAGAIN;
25981 -                       break;
25982 -               }
25983 -
25984 -               release_sock(sk);
25985 -               timeo = schedule_timeout(timeo);
25986 -               lock_sock(sk);
25987 -
25988 -               err = 0;
25989 -               if (sk->state == BT_CONNECTED)
25990 -                       break;
25991 -
25992 -               if (sk->err) {
25993 -                       err = sock_error(sk);
25994 -                       break;
25995 -               }
25996 -
25997 -               if (signal_pending(current)) {
25998 -                       err = sock_intr_errno(timeo);
25999 -                       break;
26000 -               }
26001 -       }
26002 -       current->state = TASK_RUNNING;
26003 -       remove_wait_queue(sk->sleep, &wait);
26004 -
26005 -       return err;
26006 -}
26007 -
26008 -static int l2cap_sock_connect(struct socket *sock, struct sockaddr *addr, int alen, int flags)
26009 -{
26010 -       struct sockaddr_l2 *la = (struct sockaddr_l2 *) addr;
26011 -       struct sock *sk = sock->sk;
26012 -       int err = 0;
26013 -
26014 -       lock_sock(sk);
26015 -
26016 -       DBG("sk %p", sk);
26017 -
26018 -       if (addr->sa_family != AF_BLUETOOTH || alen < sizeof(struct sockaddr_l2)) {
26019 -               err = -EINVAL;
26020 -               goto done;
26021 -       }
26022 -
26023 -       if (sk->state != BT_OPEN && sk->state != BT_BOUND) {
26024 -               err = -EBADFD;
26025 -               goto done;
26026 -       }
26027 -
26028 -       if (sk->type == SOCK_SEQPACKET && !la->l2_psm) {
26029 -               err = -EINVAL;
26030 -               goto done;
26031 -       }
26032 -
26033 -       /* Set destination address and psm */
26034 -       bacpy(&l2cap_pi(sk)->dst, &la->l2_bdaddr);
26035 -       l2cap_pi(sk)->psm = la->l2_psm;
26036 -
26037 -       if ((err = l2cap_connect(sk)))
26038 -               goto done;
26039 -
26040 -       err = l2cap_sock_w4_connect(sk, flags);
26041 -
26042 -done:
26043 -       release_sock(sk);
26044 -       return err;
26045 -}
26046 -
26047 -int l2cap_sock_listen(struct socket *sock, int backlog)
26048 -{
26049 -       struct sock *sk = sock->sk;
26050 -       int err = 0;
26051 -
26052 -       DBG("sk %p backlog %d", sk, backlog);
26053 -
26054 -       lock_sock(sk);
26055 -
26056 -       if (sk->state != BT_BOUND || sock->type != SOCK_SEQPACKET) {
26057 -               err = -EBADFD;
26058 -               goto done;
26059 -       }
26060 -
26061 -       if (!l2cap_pi(sk)->psm) {
26062 -               err = -EINVAL;
26063 -               goto done;
26064 -       }
26065 -
26066 -       sk->max_ack_backlog = backlog;
26067 -       sk->ack_backlog = 0;
26068 -       sk->state = BT_LISTEN;
26069 -
26070 -done:
26071 -       release_sock(sk);
26072 -       return err;
26073 -}
26074 -
26075 -int l2cap_sock_accept(struct socket *sock, struct socket *newsock, int flags)
26076 -{
26077 -       DECLARE_WAITQUEUE(wait, current);
26078 -       struct sock *sk = sock->sk, *ch;
26079 -       long timeo;
26080 -       int err = 0;
26081 -
26082 -       lock_sock(sk);
26083 -
26084 -       if (sk->state != BT_LISTEN) {
26085 -               err = -EBADFD;
26086 -               goto done;
26087 -       }
26088 -
26089 -       timeo = sock_rcvtimeo(sk, flags & O_NONBLOCK);
26090 -
26091 -       DBG("sk %p timeo %ld", sk, timeo);
26092 -
26093 -       /* Wait for an incoming connection. (wake-one). */
26094 -       add_wait_queue_exclusive(sk->sleep, &wait);
26095 -       current->state = TASK_INTERRUPTIBLE;
26096 -       while (!(ch = l2cap_accept_dequeue(sk, BT_CONNECTED))) {
26097 -               if (!timeo) {
26098 -                       err = -EAGAIN;
26099 -                       break;
26100 -               }
26101 -
26102 -               release_sock(sk);
26103 -               timeo = schedule_timeout(timeo);
26104 -               lock_sock(sk);
26105 -
26106 -               if (sk->state != BT_LISTEN) {
26107 -                       err = -EBADFD;
26108 -                       break;
26109 -               }
26110 -
26111 -               if (signal_pending(current)) {
26112 -                       err = sock_intr_errno(timeo);
26113 -                       break;
26114 -               }
26115 -       }
26116 -       current->state = TASK_RUNNING;
26117 -       remove_wait_queue(sk->sleep, &wait);
26118 -
26119 -       if (err)
26120 -               goto done;
26121 -
26122 -       sock_graft(ch, newsock);
26123 -       newsock->state = SS_CONNECTED;
26124 -
26125 -       DBG("new socket %p", ch);
26126 -
26127 -done:
26128 -       release_sock(sk);
26129 -
26130 -       return err;
26131 -}
26132 -
26133 -static int l2cap_sock_getname(struct socket *sock, struct sockaddr *addr, int *len, int peer)
26134 -{
26135 -       struct sockaddr_l2 *la = (struct sockaddr_l2 *) addr;
26136 -       struct sock *sk = sock->sk;
26137 -
26138 -       DBG("sock %p, sk %p", sock, sk);
26139 -
26140 -       addr->sa_family = AF_BLUETOOTH;
26141 -       *len = sizeof(struct sockaddr_l2);
26142 -
26143 -       if (peer)
26144 -               bacpy(&la->l2_bdaddr, &l2cap_pi(sk)->dst);
26145 -       else
26146 -               bacpy(&la->l2_bdaddr, &l2cap_pi(sk)->src);
26147 -
26148 -       la->l2_psm = l2cap_pi(sk)->psm;
26149 -
26150 -       return 0;
26151 -}
26152 -
26153 -static int l2cap_sock_sendmsg(struct socket *sock, struct msghdr *msg, int len, struct scm_cookie *scm)
26154 -{
26155 -       struct sock *sk = sock->sk;
26156 -       int err = 0;
26157 -
26158 -       DBG("sock %p, sk %p", sock, sk);
26159 -
26160 -       if (sk->err)
26161 -               return sock_error(sk);
26162 -
26163 -       if (msg->msg_flags & MSG_OOB)
26164 -               return -EOPNOTSUPP;
26165 -
26166 -       lock_sock(sk);
26167 -
26168 -       if (sk->state == BT_CONNECTED)
26169 -               err = l2cap_chan_send(sk, msg, len);
26170 -       else
26171 -               err = -ENOTCONN;
26172 -
26173 -       release_sock(sk);
26174 -       return err;
26175 -}
26176 -
26177 -static int l2cap_sock_recvmsg(struct socket *sock, struct msghdr *msg, int len, int flags, struct scm_cookie *scm)
26178 -{
26179 -       struct sock *sk = sock->sk;
26180 -       int noblock = flags & MSG_DONTWAIT;
26181 -       int copied, err;
26182 -       struct sk_buff *skb;
26183 -
26184 -       DBG("sock %p, sk %p", sock, sk);
26185 -
26186 -       if (flags & (MSG_OOB))
26187 -               return -EOPNOTSUPP;
26188 -
26189 -       if (sk->state == BT_CLOSED)
26190 -               return 0;
26191 -
26192 -       if (!(skb = skb_recv_datagram(sk, flags, noblock, &err)))
26193 -               return err;
26194 -
26195 -       msg->msg_namelen = 0;
26196 -
26197 -       copied = skb->len;
26198 -       if (len < copied) {
26199 -               msg->msg_flags |= MSG_TRUNC;
26200 -               copied = len;
26201 -       }
26202 -
26203 -       skb->h.raw = skb->data;
26204 -       err = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied);
26205 -
26206 -       skb_free_datagram(sk, skb);
26207 -
26208 -       return err ? : copied;
26209 -}
26210 -
26211 -int l2cap_sock_setsockopt(struct socket *sock, int level, int optname, char *optval, int optlen)
26212 -{
26213 -       struct sock *sk = sock->sk;
26214 -       struct l2cap_options opts;
26215 -       int err = 0;
26216 -
26217 -       DBG("sk %p", sk);
26218 -
26219 -       lock_sock(sk);
26220 -
26221 -       switch (optname) {
26222 -       case L2CAP_OPTIONS:
26223 -               if (copy_from_user((char *)&opts, optval, optlen)) {
26224 -                       err = -EFAULT;
26225 -                       break;
26226 -               }
26227 -               l2cap_pi(sk)->imtu = opts.imtu;
26228 -               l2cap_pi(sk)->omtu = opts.omtu;
26229 -               break;
26230 -
26231 -       default:
26232 -               err = -ENOPROTOOPT;
26233 -               break;
26234 -       };
26235 -
26236 -       release_sock(sk);
26237 -       return err;
26238 -}
26239 -
26240 -int l2cap_sock_getsockopt(struct socket *sock, int level, int optname, char *optval, int *optlen)
26241 -{
26242 -       struct sock *sk = sock->sk;
26243 -       struct l2cap_options opts;
26244 -       struct l2cap_conninfo cinfo;
26245 -       int len, err = 0; 
26246 -
26247 -       if (get_user(len, optlen))
26248 -               return -EFAULT;
26249 -
26250 -       lock_sock(sk);
26251 -
26252 -       switch (optname) {
26253 -       case L2CAP_OPTIONS:
26254 -               opts.imtu     = l2cap_pi(sk)->imtu;
26255 -               opts.omtu     = l2cap_pi(sk)->omtu;
26256 -               opts.flush_to = l2cap_pi(sk)->flush_to;
26257 -
26258 -               len = MIN(len, sizeof(opts));
26259 -               if (copy_to_user(optval, (char *)&opts, len))
26260 -                       err = -EFAULT;
26261 -
26262 -               break;
26263 -
26264 -       case L2CAP_CONNINFO:
26265 -               if (sk->state != BT_CONNECTED) {
26266 -                       err = -ENOTCONN;
26267 -                       break;
26268 -               }
26269 -
26270 -               cinfo.hci_handle = l2cap_pi(sk)->conn->hconn->handle;
26271 -
26272 -               len = MIN(len, sizeof(cinfo));
26273 -               if (copy_to_user(optval, (char *)&cinfo, len))
26274 -                       err = -EFAULT;
26275 -
26276 -               break;
26277 -
26278 -       default:
26279 -               err = -ENOPROTOOPT;
26280 -               break;
26281 -       };
26282 -
26283 -       release_sock(sk);
26284 -       return err;
26285 -}
26286 -
26287 -static unsigned int l2cap_sock_poll(struct file * file, struct socket *sock, poll_table *wait)
26288 -{
26289 -       struct sock *sk = sock->sk;
26290 -       struct l2cap_accept_q *aq;
26291 -       unsigned int mask;
26292 -
26293 -       DBG("sock %p, sk %p", sock, sk);
26294 -
26295 -       poll_wait(file, sk->sleep, wait);
26296 -       mask = 0;
26297 -
26298 -       if (sk->err || !skb_queue_empty(&sk->error_queue))
26299 -               mask |= POLLERR;
26300 -
26301 -       if (sk->shutdown == SHUTDOWN_MASK)
26302 -               mask |= POLLHUP;
26303 -
26304 -       aq = &l2cap_pi(sk)->accept_q;
26305 -       if (!skb_queue_empty(&sk->receive_queue) || aq->head || (sk->shutdown & RCV_SHUTDOWN))
26306 -               mask |= POLLIN | POLLRDNORM;
26307 -
26308 -       if (sk->state == BT_CLOSED)
26309 -               mask |= POLLHUP;
26310 -
26311 -       if (sock_writeable(sk))
26312 -               mask |= POLLOUT | POLLWRNORM | POLLWRBAND;
26313 -       else
26314 -               set_bit(SOCK_ASYNC_NOSPACE, &sk->socket->flags);
26315 -
26316 -       return mask;
26317 -}
26318 -
26319 -static int l2cap_sock_release(struct socket *sock)
26320 -{
26321 -       struct sock *sk = sock->sk;
26322 -
26323 -       DBG("sock %p, sk %p", sock, sk);
26324 -
26325 -       if (!sk)
26326 -               return 0;
26327 -
26328 -       sock_orphan(sk);
26329 -
26330 -       l2cap_sock_close(sk);
26331 -
26332 -       return 0;
26333 -}
26334 -
26335 -/* --------- L2CAP channels --------- */
26336 -static struct sock * __l2cap_get_chan_by_dcid(struct l2cap_chan_list *l, __u16 cid)
26337 -{
26338 -       struct sock *s;
26339 -
26340 -       for (s = l->head; s; s = l2cap_pi(s)->next_c) {
26341 -               if (l2cap_pi(s)->dcid == cid)
26342 -                       break;
26343 -       }
26344 -
26345 -       return s;
26346 -}
26347 -
26348 -static inline struct sock *l2cap_get_chan_by_dcid(struct l2cap_chan_list *l, __u16 cid)
26349 -{
26350 -       struct sock *s;
26351 -
26352 -       read_lock(&l->lock);
26353 -       s = __l2cap_get_chan_by_dcid(l, cid);
26354 -       read_unlock(&l->lock);
26355 -
26356 -       return s;
26357 -}
26358 -
26359 -static struct sock *__l2cap_get_chan_by_scid(struct l2cap_chan_list *l, __u16 cid)
26360 -{
26361 -       struct sock *s;
26362 -
26363 -       for (s = l->head; s; s = l2cap_pi(s)->next_c) {
26364 -               if (l2cap_pi(s)->scid == cid)
26365 -                       break;
26366 -       }
26367 -
26368 -       return s;
26369 -}
26370 -static inline struct sock *l2cap_get_chan_by_scid(struct l2cap_chan_list *l, __u16 cid)
26371 -{
26372 -       struct sock *s;
26373 -
26374 -       read_lock(&l->lock);
26375 -       s = __l2cap_get_chan_by_scid(l, cid);
26376 -       read_unlock(&l->lock);
26377 -
26378 -       return s;
26379 -}
26380 -
26381 -static struct sock *__l2cap_get_chan_by_ident(struct l2cap_chan_list *l, __u8 ident)
26382 -{
26383 -       struct sock *s;
26384 -
26385 -       for (s = l->head; s; s = l2cap_pi(s)->next_c) {
26386 -               if (l2cap_pi(s)->ident == ident)
26387 -                       break;
26388 -       }
26389 -
26390 -       return s;
26391 -}
26392 -
26393 -static inline struct sock *l2cap_get_chan_by_ident(struct l2cap_chan_list *l, __u8 ident)
26394 -{
26395 -       struct sock *s;
26396 -
26397 -       read_lock(&l->lock);
26398 -       s = __l2cap_get_chan_by_ident(l, ident);
26399 -       read_unlock(&l->lock);
26400 -
26401 -       return s;
26402 -}
26403 -
26404 -static __u16 l2cap_alloc_cid(struct l2cap_chan_list *l)
26405 -{
26406 -       __u16 cid = 0x0040;
26407 -
26408 -       for (; cid < 0xffff; cid++) {
26409 -               if(!__l2cap_get_chan_by_scid(l, cid))
26410 -                       return cid;
26411 -       }
26412 -
26413 -       return 0;
26414 -}
26415 -
26416 -static inline void __l2cap_chan_link(struct l2cap_chan_list *l, struct sock *sk)
26417 -{
26418 -       sock_hold(sk);
26419 -
26420 -       if (l->head)
26421 -               l2cap_pi(l->head)->prev_c = sk;
26422 -
26423 -       l2cap_pi(sk)->next_c = l->head;
26424 -       l2cap_pi(sk)->prev_c = NULL;
26425 -       l->head = sk;
26426 -}
26427 -
26428 -static inline void l2cap_chan_unlink(struct l2cap_chan_list *l, struct sock *sk)
26429 -{
26430 -       struct sock *next = l2cap_pi(sk)->next_c, *prev = l2cap_pi(sk)->prev_c;
26431 -
26432 -       write_lock(&l->lock);
26433 -       if (sk == l->head)
26434 -               l->head = next;
26435 -
26436 -       if (next)
26437 -               l2cap_pi(next)->prev_c = prev;
26438 -       if (prev)
26439 -               l2cap_pi(prev)->next_c = next;
26440 -       write_unlock(&l->lock);
26441 -
26442 -       __sock_put(sk);
26443 -}
26444 -
26445 -static void __l2cap_chan_add(struct l2cap_conn *conn, struct sock *sk, struct sock *parent)
26446 -{
26447 -       struct l2cap_chan_list *l = &conn->chan_list;
26448 -
26449 -       DBG("conn %p, psm 0x%2.2x, dcid 0x%4.4x", conn, l2cap_pi(sk)->psm, l2cap_pi(sk)->dcid);
26450 -
26451 -       l2cap_conn_clear_timer(conn);
26452 -
26453 -       atomic_inc(&conn->refcnt);
26454 -       l2cap_pi(sk)->conn = conn;
26455 -
26456 -       if (sk->type == SOCK_SEQPACKET) {
26457 -               /* Alloc CID for normal socket */
26458 -               l2cap_pi(sk)->scid = l2cap_alloc_cid(l);
26459 -       } else {
26460 -               /* Raw socket can send only signalling messages */
26461 -               l2cap_pi(sk)->scid = 0x0001;
26462 -               l2cap_pi(sk)->dcid = 0x0001;
26463 -               l2cap_pi(sk)->omtu = L2CAP_DEFAULT_MTU;
26464 -       }
26465 -
26466 -       __l2cap_chan_link(l, sk);
26467 -
26468 -       if (parent)
26469 -               l2cap_accept_queue(parent, sk);
26470 -}
26471 -
26472 -static inline void l2cap_chan_add(struct l2cap_conn *conn, struct sock *sk, struct sock *parent)
26473 -{
26474 -       struct l2cap_chan_list *l = &conn->chan_list;
26475 -
26476 -       write_lock(&l->lock);
26477 -       __l2cap_chan_add(conn, sk, parent);
26478 -       write_unlock(&l->lock);
26479 -}
26480 -
26481 -/* Delete channel. 
26482 - * Must be called on the locked socket. */
26483 -static void l2cap_chan_del(struct sock *sk, int err)
26484 -{
26485 -       struct l2cap_conn *conn;
26486 -       struct sock *parent;
26487 -
26488 -       conn = l2cap_pi(sk)->conn;
26489 -       parent = l2cap_pi(sk)->parent;
26490 -
26491 -       DBG("sk %p, conn %p, err %d", sk, conn, err);
26492 -
26493 -       if (parent) {
26494 -               /* Unlink from parent accept queue */
26495 -               bh_lock_sock(parent);
26496 -               l2cap_accept_unlink(sk);
26497 -               bh_unlock_sock(parent);
26498 -       }
26499 -
26500 -       if (conn) { 
26501 -               long timeout;
26502 -
26503 -               /* Unlink from channel list */
26504 -               l2cap_chan_unlink(&conn->chan_list, sk);
26505 -               l2cap_pi(sk)->conn = NULL;
26506 -
26507 -               if (conn->out)
26508 -                       timeout = L2CAP_DISCONN_TIMEOUT;
26509 -               else
26510 -                       timeout = L2CAP_CONN_IDLE_TIMEOUT;
26511 -               
26512 -               if (atomic_dec_and_test(&conn->refcnt) && conn->state == BT_CONNECTED) {
26513 -                       /* Schedule Baseband disconnect */
26514 -                       l2cap_conn_set_timer(conn, timeout);
26515 -               }
26516 -       }
26517 -
26518 -       sk->state  = BT_CLOSED;
26519 -       sk->err    = err;
26520 -       sk->state_change(sk);
26521 -
26522 -       sk->zapped = 1;
26523 -}
26524 -
26525 -static void l2cap_conn_ready(struct l2cap_conn *conn)
26526 -{
26527 -       struct l2cap_chan_list *l = &conn->chan_list;
26528 -       struct sock *sk;
26529 -
26530 -       DBG("conn %p", conn);
26531 -
26532 -       read_lock(&l->lock);
26533 -
26534 -       for (sk = l->head; sk; sk = l2cap_pi(sk)->next_c) {
26535 -               bh_lock_sock(sk);
26536 -
26537 -               if (sk->type != SOCK_SEQPACKET) {
26538 -                       sk->state = BT_CONNECTED;
26539 -                       sk->state_change(sk);
26540 -                       l2cap_sock_clear_timer(sk);
26541 -               } else if (sk->state == BT_CONNECT) {
26542 -                       l2cap_conn_req req;
26543 -                       req.scid = __cpu_to_le16(l2cap_pi(sk)->scid);
26544 -                       req.psm  = l2cap_pi(sk)->psm;
26545 -                       l2cap_send_req(conn, L2CAP_CONN_REQ, L2CAP_CONN_REQ_SIZE, &req);
26546 -
26547 -                       l2cap_sock_set_timer(sk, sk->sndtimeo);
26548 -               }
26549 -
26550 -               bh_unlock_sock(sk);
26551 -       }
26552 -
26553 -       read_unlock(&l->lock);
26554 -}
26555 -
26556 -static void l2cap_chan_ready(struct sock *sk)
26557 -{
26558 -       struct sock *parent = l2cap_pi(sk)->parent;
26559 -
26560 -       DBG("sk %p, parent %p", sk, parent);
26561 -
26562 -       l2cap_pi(sk)->conf_state = 0;
26563 -       l2cap_sock_clear_timer(sk);
26564 -
26565 -       if (!parent) {
26566 -               /* Outgoing channel.
26567 -                * Wake up socket sleeping on connect.
26568 -                */
26569 -               sk->state = BT_CONNECTED;
26570 -               sk->state_change(sk);
26571 -       } else {
26572 -               /* Incomming channel.
26573 -                * Wake up socket sleeping on accept.
26574 -                */
26575 -               parent->data_ready(parent, 1);
26576 -       }
26577 -}
26578 -
26579 -/* Copy frame to all raw sockets on that connection */
26580 -void l2cap_raw_recv(struct l2cap_conn *conn, struct sk_buff *skb)
26581 -{
26582 -       struct l2cap_chan_list *l = &conn->chan_list;
26583 -       struct sk_buff *nskb;
26584 -       struct sock * sk;
26585 -
26586 -       DBG("conn %p", conn);
26587 -
26588 -       read_lock(&l->lock);
26589 -       for (sk = l->head; sk; sk = l2cap_pi(sk)->next_c) {
26590 -               if (sk->type != SOCK_RAW)
26591 -                       continue;
26592 -
26593 -               /* Don't send frame to the socket it came from */
26594 -               if (skb->sk == sk)
26595 -                       continue;
26596 -
26597 -               if (!(nskb = skb_clone(skb, GFP_ATOMIC)))
26598 -                       continue;
26599 -
26600 -               skb_queue_tail(&sk->receive_queue, nskb);
26601 -               sk->data_ready(sk, nskb->len);
26602 -       }
26603 -       read_unlock(&l->lock);
26604 -}
26605 -
26606 -static int l2cap_chan_send(struct sock *sk, struct msghdr *msg, int len)
26607 -{
26608 -       struct l2cap_conn *conn = l2cap_pi(sk)->conn;
26609 -       struct sk_buff *skb, **frag;
26610 -       int err, size, count, sent=0;
26611 -       l2cap_hdr *lh;
26612 -
26613 -       /* Check outgoing MTU */
26614 -       if (len > l2cap_pi(sk)->omtu)
26615 -               return -EINVAL;
26616 -
26617 -       DBG("sk %p len %d", sk, len);
26618 -
26619 -       /* First fragment (with L2CAP header) */
26620 -       count = MIN(conn->iff->mtu - L2CAP_HDR_SIZE, len);
26621 -       size  = L2CAP_HDR_SIZE + count;
26622 -       if (!(skb = bluez_skb_send_alloc(sk, size, msg->msg_flags & MSG_DONTWAIT, &err)))
26623 -               return err;
26624 -
26625 -       /* Create L2CAP header */
26626 -       lh = (l2cap_hdr *) skb_put(skb, L2CAP_HDR_SIZE);
26627 -       lh->len = __cpu_to_le16(len);
26628 -       lh->cid = __cpu_to_le16(l2cap_pi(sk)->dcid);
26629 -
26630 -       if (memcpy_fromiovec(skb_put(skb, count), msg->msg_iov, count)) {
26631 -               err = -EFAULT;
26632 -               goto fail;
26633 -       }
26634 -
26635 -       sent += count;
26636 -       len  -= count;
26637 -
26638 -       /* Continuation fragments (no L2CAP header) */
26639 -       frag = &skb_shinfo(skb)->frag_list;
26640 -       while (len) {
26641 -               count = MIN(conn->iff->mtu, len);
26642 -
26643 -               *frag = bluez_skb_send_alloc(sk, count, msg->msg_flags & MSG_DONTWAIT, &err);
26644 -               if (!*frag)
26645 -                       goto fail;
26646 -               
26647 -               if (memcpy_fromiovec(skb_put(*frag, count), msg->msg_iov, count)) {
26648 -                       err = -EFAULT;
26649 -                       goto fail;
26650 -               }
26651 -
26652 -               sent += count;
26653 -               len  -= count;
26654 -
26655 -               frag = &(*frag)->next;
26656 -       }
26657 -
26658 -       if ((err = hci_send_acl(conn->hconn, skb, 0)) < 0)
26659 -               goto fail;
26660 -
26661 -       return sent;
26662 -
26663 -fail:
26664 -       kfree_skb(skb);
26665 -       return err;
26666 -}
26667 -
26668 -/* --------- L2CAP signalling commands --------- */
26669 -static inline __u8 l2cap_get_ident(struct l2cap_conn *conn)
26670 -{
26671 -       __u8 id;
26672 -
26673 -       /* Get next available identificator.
26674 -        *    1 - 199 are used by kernel.
26675 -        *  200 - 254 are used by utilities like l2ping, etc 
26676 -        */
26677 -
26678 -       spin_lock(&conn->lock);
26679 -
26680 -       if (++conn->tx_ident > 199)
26681 -               conn->tx_ident = 1;
26682 -
26683 -       id = conn->tx_ident;
26684 -
26685 -       spin_unlock(&conn->lock);
26686 -
26687 -       return id;
26688 -}
26689 -
26690 -static inline struct sk_buff *l2cap_build_cmd(__u8 code, __u8 ident, __u16 len, void *data)
26691 -{
26692 -       struct sk_buff *skb;
26693 -       l2cap_cmd_hdr *cmd;
26694 -       l2cap_hdr *lh;
26695 -       int size;
26696 -
26697 -       DBG("code 0x%2.2x, ident 0x%2.2x, len %d", code, ident, len);
26698 -
26699 -       size = L2CAP_HDR_SIZE + L2CAP_CMD_HDR_SIZE + len;
26700 -       if (!(skb = bluez_skb_alloc(size, GFP_ATOMIC)))
26701 -               return NULL;
26702 -
26703 -       lh = (l2cap_hdr *) skb_put(skb, L2CAP_HDR_SIZE);
26704 -       lh->len = __cpu_to_le16(L2CAP_CMD_HDR_SIZE + len);
26705 -       lh->cid = __cpu_to_le16(0x0001);
26706 -
26707 -       cmd = (l2cap_cmd_hdr *) skb_put(skb, L2CAP_CMD_HDR_SIZE);
26708 -       cmd->code  = code;
26709 -       cmd->ident = ident;
26710 -       cmd->len   = __cpu_to_le16(len);
26711 -
26712 -       if (len)
26713 -               memcpy(skb_put(skb, len), data, len);
26714 -
26715 -       return skb;
26716 -}
26717 -
26718 -static int l2cap_send_req(struct l2cap_conn *conn, __u8 code, __u16 len, void *data)
26719 -{
26720 -       struct sk_buff *skb;
26721 -       __u8 ident;
26722 -
26723 -       DBG("code 0x%2.2x", code);
26724 -
26725 -       ident = l2cap_get_ident(conn);
26726 -       if (!(skb = l2cap_build_cmd(code, ident, len, data)))
26727 -               return -ENOMEM;
26728 -       return hci_send_acl(conn->hconn, skb, 0);
26729 -}
26730 -
26731 -static int l2cap_send_rsp(struct l2cap_conn *conn, __u8 ident, __u8 code, __u16 len, void *data)
26732 -{
26733 -       struct sk_buff *skb;
26734 -
26735 -       DBG("code 0x%2.2x", code);
26736 -
26737 -       if (!(skb = l2cap_build_cmd(code, ident, len, data)))
26738 -               return -ENOMEM;
26739 -       return hci_send_acl(conn->hconn, skb, 0);
26740 -}
26741 -
26742 -static inline int l2cap_get_conf_opt(__u8 **ptr, __u8 *type, __u32 *val)
26743 -{
26744 -       l2cap_conf_opt *opt = (l2cap_conf_opt *) (*ptr);
26745 -       int len;
26746 -
26747 -       *type = opt->type;
26748 -       switch (opt->len) {
26749 -       case 1:
26750 -               *val = *((__u8 *) opt->val);
26751 -               break;
26752 -
26753 -       case 2:
26754 -               *val = __le16_to_cpu(*((__u16 *)opt->val));
26755 -               break;
26756 -
26757 -       case 4:
26758 -               *val = __le32_to_cpu(*((__u32 *)opt->val));
26759 -               break;
26760 -
26761 -       default:
26762 -               *val = 0L;
26763 -               break;
26764 -       };
26765 -
26766 -       DBG("type 0x%2.2x len %d val 0x%8.8x", *type, opt->len, *val);
26767 -
26768 -       len = L2CAP_CONF_OPT_SIZE + opt->len;
26769 -
26770 -       *ptr += len;
26771 -
26772 -       return len;
26773 -}
26774 -
26775 -static inline void l2cap_parse_conf_req(struct sock *sk, char *data, int len)
26776 -{
26777 -       __u8 type, hint; __u32 val;
26778 -       __u8 *ptr = data;
26779 -
26780 -       DBG("sk %p len %d", sk, len);
26781 -
26782 -       while (len >= L2CAP_CONF_OPT_SIZE) {
26783 -               len -= l2cap_get_conf_opt(&ptr, &type, &val);
26784 -
26785 -               hint  = type & 0x80;
26786 -               type &= 0x7f;
26787 -
26788 -               switch (type) {
26789 -               case L2CAP_CONF_MTU:
26790 -                       l2cap_pi(sk)->conf_mtu = val;
26791 -                       break;
26792 -
26793 -               case L2CAP_CONF_FLUSH_TO:
26794 -                       l2cap_pi(sk)->flush_to = val;
26795 -                       break;
26796 -
26797 -               case L2CAP_CONF_QOS:
26798 -                       break;
26799 -               
26800 -               default:
26801 -                       if (hint)
26802 -                               break;
26803 -
26804 -                       /* FIXME: Reject unknon option */
26805 -                       break;
26806 -               };
26807 -       }
26808 -}
26809 -
26810 -static inline void l2cap_add_conf_opt(__u8 **ptr, __u8 type, __u8 len, __u32 val)
26811 -{
26812 -       register l2cap_conf_opt *opt = (l2cap_conf_opt *) (*ptr);
26813 -
26814 -       DBG("type 0x%2.2x len %d val 0x%8.8x", type, len, val);
26815 -
26816 -       opt->type = type;
26817 -       opt->len  = len;
26818 -       switch (len) {
26819 -       case 1:
26820 -               *((__u8 *) opt->val)  = val;
26821 -               break;
26822 -
26823 -       case 2:
26824 -               *((__u16 *) opt->val) = __cpu_to_le16(val);
26825 -               break;
26826 -
26827 -       case 4:
26828 -               *((__u32 *) opt->val) = __cpu_to_le32(val);
26829 -               break;
26830 -       };
26831 -
26832 -       *ptr += L2CAP_CONF_OPT_SIZE + len;
26833 -}
26834 -
26835 -static int l2cap_build_conf_req(struct sock *sk, __u8 *data)
26836 -{
26837 -       struct l2cap_pinfo *pi = l2cap_pi(sk);
26838 -       l2cap_conf_req *req = (l2cap_conf_req *) data;
26839 -       __u8 *ptr = req->data;
26840 -
26841 -       DBG("sk %p", sk);
26842 -
26843 -       if (pi->imtu != L2CAP_DEFAULT_MTU)
26844 -               l2cap_add_conf_opt(&ptr, L2CAP_CONF_MTU, 2, pi->imtu);
26845 -
26846 -       /* FIXME. Need actual value of the flush timeout */
26847 -       //if (flush_to != L2CAP_DEFAULT_FLUSH_TO)
26848 -       //   l2cap_add_conf_opt(&ptr, L2CAP_CONF_FLUSH_TO, 2, pi->flush_to);
26849 -
26850 -       req->dcid  = __cpu_to_le16(pi->dcid);
26851 -       req->flags = __cpu_to_le16(0);
26852 -
26853 -       return ptr - data;
26854 -}
26855 -
26856 -static int l2cap_conf_output(struct sock *sk, __u8 **ptr)
26857 -{
26858 -       struct l2cap_pinfo *pi = l2cap_pi(sk);
26859 -       int result = 0;
26860 -
26861 -       /* Configure output options and let other side know
26862 -        * which ones we don't like.
26863 -        */
26864 -       if (pi->conf_mtu < pi->omtu) {
26865 -               l2cap_add_conf_opt(ptr, L2CAP_CONF_MTU, 2, l2cap_pi(sk)->omtu);
26866 -               result = L2CAP_CONF_UNACCEPT;
26867 -       } else {
26868 -               pi->omtu = pi->conf_mtu;
26869 -       }
26870 -
26871 -       DBG("sk %p result %d", sk, result);
26872 -       return result;
26873 -}
26874 -
26875 -static int l2cap_build_conf_rsp(struct sock *sk, __u8 *data, int *result)
26876 -{
26877 -       l2cap_conf_rsp *rsp = (l2cap_conf_rsp *) data;
26878 -       __u8 *ptr = rsp->data;
26879 -
26880 -       DBG("sk %p complete %d", sk, result ? 1 : 0);
26881 -
26882 -       if (result)
26883 -               *result = l2cap_conf_output(sk, &ptr);
26884 -
26885 -       rsp->scid   = __cpu_to_le16(l2cap_pi(sk)->dcid);
26886 -       rsp->result = __cpu_to_le16(result ? *result : 0);
26887 -       rsp->flags  = __cpu_to_le16(0);
26888 -
26889 -       return ptr - data;
26890 -}
26891 -
26892 -static inline int l2cap_connect_req(struct l2cap_conn *conn, l2cap_cmd_hdr *cmd, __u8 *data)
26893 -{
26894 -       struct l2cap_chan_list *list = &conn->chan_list;
26895 -       l2cap_conn_req *req = (l2cap_conn_req *) data;
26896 -       l2cap_conn_rsp rsp;
26897 -       struct sock *sk, *parent;
26898 -
26899 -       __u16 scid = __le16_to_cpu(req->scid);
26900 -       __u16 psm  = req->psm;
26901 -
26902 -       DBG("psm 0x%2.2x scid 0x%4.4x", psm, scid);
26903 -
26904 -       /* Check if we have socket listening on psm */
26905 -       if (!(parent = l2cap_get_sock_listen(&conn->src, psm)))
26906 -               goto reject;
26907 -
26908 -       bh_lock_sock(parent);
26909 -       write_lock(&list->lock);
26910 -
26911 -       /* Check if we already have channel with that dcid */
26912 -       if (__l2cap_get_chan_by_dcid(list, scid))
26913 -               goto unlock;
26914 -
26915 -       /* Check for backlog size */
26916 -       if (parent->ack_backlog > parent->max_ack_backlog)
26917 -               goto unlock;
26918 -
26919 -       if (!(sk = l2cap_sock_alloc(NULL, BTPROTO_L2CAP, GFP_ATOMIC)))
26920 -               goto unlock;
26921 -
26922 -       l2cap_sock_init(sk, parent);
26923 -
26924 -       bacpy(&l2cap_pi(sk)->src, &conn->src);
26925 -       bacpy(&l2cap_pi(sk)->dst, &conn->dst);
26926 -       l2cap_pi(sk)->psm  = psm;
26927 -       l2cap_pi(sk)->dcid = scid;
26928 -
26929 -       __l2cap_chan_add(conn, sk, parent);
26930 -       sk->state = BT_CONFIG;
26931 -
26932 -       write_unlock(&list->lock);
26933 -       bh_unlock_sock(parent);
26934 -
26935 -       rsp.dcid   = __cpu_to_le16(l2cap_pi(sk)->scid);
26936 -       rsp.scid   = __cpu_to_le16(l2cap_pi(sk)->dcid);
26937 -       rsp.result = __cpu_to_le16(0);
26938 -       rsp.status = __cpu_to_le16(0);
26939 -       l2cap_send_rsp(conn, cmd->ident, L2CAP_CONN_RSP, L2CAP_CONN_RSP_SIZE, &rsp);
26940 -
26941 -       return 0;
26942 -
26943 -unlock:
26944 -       write_unlock(&list->lock);
26945 -       bh_unlock_sock(parent);
26946 -
26947 -reject:
26948 -       rsp.scid   = __cpu_to_le16(scid);
26949 -       rsp.dcid   = __cpu_to_le16(0);
26950 -       rsp.status = __cpu_to_le16(0);
26951 -       rsp.result = __cpu_to_le16(L2CAP_CONN_NO_MEM);
26952 -       l2cap_send_rsp(conn, cmd->ident, L2CAP_CONN_RSP, L2CAP_CONN_RSP_SIZE, &rsp);
26953 -
26954 -       return 0;
26955 -}
26956 -
26957 -static inline int l2cap_connect_rsp(struct l2cap_conn *conn, l2cap_cmd_hdr *cmd, __u8 *data)
26958 -{
26959 -       l2cap_conn_rsp *rsp = (l2cap_conn_rsp *) data;
26960 -       __u16 scid, dcid, result, status;
26961 -       struct sock *sk;
26962 -
26963 -       scid   = __le16_to_cpu(rsp->scid);
26964 -       dcid   = __le16_to_cpu(rsp->dcid);
26965 -       result = __le16_to_cpu(rsp->result);
26966 -       status = __le16_to_cpu(rsp->status);
26967 -
26968 -       DBG("dcid 0x%4.4x scid 0x%4.4x result 0x%2.2x status 0x%2.2x", dcid, scid, result, status);
26969 -
26970 -       if (!(sk = l2cap_get_chan_by_scid(&conn->chan_list, scid)))
26971 -               return -ENOENT;
26972 -
26973 -       bh_lock_sock(sk);
26974 -
26975 -       if (!result) {
26976 -               char req[64];
26977 -
26978 -               sk->state = BT_CONFIG;
26979 -               l2cap_pi(sk)->dcid = dcid;
26980 -               l2cap_pi(sk)->conf_state |= CONF_REQ_SENT;
26981 -
26982 -               l2cap_send_req(conn, L2CAP_CONF_REQ, l2cap_build_conf_req(sk, req), req);
26983 -       } else {
26984 -               l2cap_chan_del(sk, ECONNREFUSED);
26985 -       }
26986 -
26987 -       bh_unlock_sock(sk);
26988 -       return 0;
26989 -}
26990 -
26991 -static inline int l2cap_config_req(struct l2cap_conn *conn, l2cap_cmd_hdr *cmd, __u8 *data)
26992 -{
26993 -       l2cap_conf_req * req = (l2cap_conf_req *) data;
26994 -       __u16 dcid, flags;
26995 -       __u8 rsp[64];
26996 -       struct sock *sk;
26997 -       int result;
26998 -
26999 -       dcid  = __le16_to_cpu(req->dcid);
27000 -       flags = __le16_to_cpu(req->flags);
27001 -
27002 -       DBG("dcid 0x%4.4x flags 0x%2.2x", dcid, flags);
27003 -
27004 -       if (!(sk = l2cap_get_chan_by_scid(&conn->chan_list, dcid)))
27005 -               return -ENOENT;
27006 -
27007 -       bh_lock_sock(sk);
27008 -
27009 -       l2cap_parse_conf_req(sk, req->data, cmd->len - L2CAP_CONF_REQ_SIZE);
27010 -
27011 -       if (flags & 0x01) {
27012 -               /* Incomplete config. Send empty response. */
27013 -               l2cap_send_rsp(conn, cmd->ident, L2CAP_CONF_RSP, l2cap_build_conf_rsp(sk, rsp, NULL), rsp);
27014 -               goto unlock;
27015 -       }
27016 -
27017 -       /* Complete config. */
27018 -       l2cap_send_rsp(conn, cmd->ident, L2CAP_CONF_RSP, l2cap_build_conf_rsp(sk, rsp, &result), rsp);
27019 -
27020 -       if (result)
27021 -               goto unlock;
27022 -
27023 -       /* Output config done */
27024 -       l2cap_pi(sk)->conf_state |= CONF_OUTPUT_DONE;
27025 -
27026 -       if (l2cap_pi(sk)->conf_state & CONF_INPUT_DONE) {
27027 -               sk->state = BT_CONNECTED;
27028 -               l2cap_chan_ready(sk);
27029 -       } else if (!(l2cap_pi(sk)->conf_state & CONF_REQ_SENT)) {
27030 -               char req[64];
27031 -               l2cap_send_req(conn, L2CAP_CONF_REQ, l2cap_build_conf_req(sk, req), req);
27032 -       }
27033 -
27034 -unlock:
27035 -       bh_unlock_sock(sk);
27036 -
27037 -       return 0;
27038 -}
27039 -
27040 -static inline int l2cap_config_rsp(struct l2cap_conn *conn, l2cap_cmd_hdr *cmd, __u8 *data)
27041 -{
27042 -       l2cap_conf_rsp *rsp = (l2cap_conf_rsp *)data;
27043 -       __u16 scid, flags, result;
27044 -       struct sock *sk;
27045 -       int err = 0;
27046 -
27047 -       scid   = __le16_to_cpu(rsp->scid);
27048 -       flags  = __le16_to_cpu(rsp->flags);
27049 -       result = __le16_to_cpu(rsp->result);
27050 -
27051 -       DBG("scid 0x%4.4x flags 0x%2.2x result 0x%2.2x", scid, flags, result);
27052 -
27053 -       if (!(sk = l2cap_get_chan_by_scid(&conn->chan_list, scid)))
27054 -               return -ENOENT;
27055 -
27056 -       bh_lock_sock(sk);
27057 -
27058 -       if (result) {
27059 -               l2cap_disconn_req req;
27060 -
27061 -               /* They didn't like our options. Well... we do not negotiate.
27062 -                * Close channel.
27063 -                */
27064 -               sk->state = BT_DISCONN;
27065 -
27066 -               req.dcid = __cpu_to_le16(l2cap_pi(sk)->dcid);
27067 -               req.scid = __cpu_to_le16(l2cap_pi(sk)->scid);
27068 -               l2cap_send_req(conn, L2CAP_DISCONN_REQ, L2CAP_DISCONN_REQ_SIZE, &req);
27069 -
27070 -               l2cap_sock_set_timer(sk, sk->sndtimeo);
27071 -               goto done;
27072 -       }
27073 -
27074 -       if (flags & 0x01)
27075 -               goto done;
27076 -
27077 -       /* Input config done */
27078 -       l2cap_pi(sk)->conf_state |= CONF_INPUT_DONE;
27079 -
27080 -       if (l2cap_pi(sk)->conf_state & CONF_OUTPUT_DONE) {
27081 -               sk->state = BT_CONNECTED;
27082 -               l2cap_chan_ready(sk);
27083 -       }
27084 -
27085 -done:
27086 -       bh_unlock_sock(sk);
27087 -
27088 -       return err;
27089 -}
27090 -
27091 -static inline int l2cap_disconnect_req(struct l2cap_conn *conn, l2cap_cmd_hdr *cmd, __u8 *data)
27092 -{
27093 -       l2cap_disconn_req *req = (l2cap_disconn_req *) data;
27094 -       l2cap_disconn_rsp rsp;
27095 -       __u16 dcid, scid;
27096 -       struct sock *sk;
27097 -
27098 -       scid = __le16_to_cpu(req->scid);
27099 -       dcid = __le16_to_cpu(req->dcid);
27100 -
27101 -       DBG("scid 0x%4.4x dcid 0x%4.4x", scid, dcid);
27102 -
27103 -       if (!(sk = l2cap_get_chan_by_scid(&conn->chan_list, dcid)))
27104 -               return 0;
27105 -
27106 -       bh_lock_sock(sk);
27107 -
27108 -       rsp.dcid = __cpu_to_le16(l2cap_pi(sk)->scid);
27109 -       rsp.scid = __cpu_to_le16(l2cap_pi(sk)->dcid);
27110 -       l2cap_send_rsp(conn, cmd->ident, L2CAP_DISCONN_RSP, L2CAP_DISCONN_RSP_SIZE, &rsp);
27111 -
27112 -       l2cap_chan_del(sk, ECONNRESET);
27113 -
27114 -       bh_unlock_sock(sk);
27115 -
27116 -       l2cap_sock_kill(sk);
27117 -
27118 -       return 0;
27119 -}
27120 -
27121 -static inline int l2cap_disconnect_rsp(struct l2cap_conn *conn, l2cap_cmd_hdr *cmd, __u8 *data)
27122 -{
27123 -       l2cap_disconn_rsp *rsp = (l2cap_disconn_rsp *) data;
27124 -       __u16 dcid, scid;
27125 -       struct sock *sk;
27126 -
27127 -       scid = __le16_to_cpu(rsp->scid);
27128 -       dcid = __le16_to_cpu(rsp->dcid);
27129 -
27130 -       DBG("dcid 0x%4.4x scid 0x%4.4x", dcid, scid);
27131 -
27132 -       if (!(sk = l2cap_get_chan_by_scid(&conn->chan_list, scid)))
27133 -               return -ENOENT;
27134 -
27135 -       bh_lock_sock(sk);
27136 -       l2cap_sock_clear_timer(sk);
27137 -       l2cap_chan_del(sk, ECONNABORTED);
27138 -       bh_unlock_sock(sk);
27139 -
27140 -       l2cap_sock_kill(sk);
27141 -
27142 -       return 0;
27143 -}
27144 -
27145 -static inline void l2cap_sig_channel(struct l2cap_conn *conn, struct sk_buff *skb)
27146 -{
27147 -       __u8 *data = skb->data;
27148 -       int len = skb->len;
27149 -       l2cap_cmd_hdr cmd;
27150 -       int err = 0;
27151 -
27152 -       while (len >= L2CAP_CMD_HDR_SIZE) {
27153 -               memcpy(&cmd, data, L2CAP_CMD_HDR_SIZE);
27154 -               data += L2CAP_CMD_HDR_SIZE;
27155 -               len  -= L2CAP_CMD_HDR_SIZE;
27156 -
27157 -               cmd.len = __le16_to_cpu(cmd.len);
27158 -
27159 -               DBG("code 0x%2.2x len %d id 0x%2.2x", cmd.code, cmd.len, cmd.ident);
27160 -
27161 -               if (cmd.len > len || !cmd.ident) {
27162 -                       DBG("corrupted command");
27163 -                       break;
27164 -               }
27165 -
27166 -               switch (cmd.code) {
27167 -               case L2CAP_CONN_REQ:
27168 -                       err = l2cap_connect_req(conn, &cmd, data);
27169 -                       break;
27170 -
27171 -               case L2CAP_CONN_RSP:
27172 -                       err = l2cap_connect_rsp(conn, &cmd, data);
27173 -                       break;
27174 -
27175 -               case L2CAP_CONF_REQ:
27176 -                       err = l2cap_config_req(conn, &cmd, data);
27177 -                       break;
27178 -
27179 -               case L2CAP_CONF_RSP:
27180 -                       err = l2cap_config_rsp(conn, &cmd, data);
27181 -                       break;
27182 -
27183 -               case L2CAP_DISCONN_REQ:
27184 -                       err = l2cap_disconnect_req(conn, &cmd, data);
27185 -                       break;
27186 -
27187 -               case L2CAP_DISCONN_RSP:
27188 -                       err = l2cap_disconnect_rsp(conn, &cmd, data);
27189 -                       break;
27190 -
27191 -               case L2CAP_COMMAND_REJ:
27192 -                       /* FIXME: We should process this */
27193 -                       l2cap_raw_recv(conn, skb);
27194 -                       break;
27195 -
27196 -               case L2CAP_ECHO_REQ:
27197 -                       l2cap_send_rsp(conn, cmd.ident, L2CAP_ECHO_RSP, cmd.len, data);
27198 -                       break;
27199 -
27200 -               case L2CAP_ECHO_RSP:
27201 -               case L2CAP_INFO_REQ:
27202 -               case L2CAP_INFO_RSP:
27203 -                       l2cap_raw_recv(conn, skb);
27204 -                       break;
27205 -
27206 -               default:
27207 -                       ERR("Uknown signaling command 0x%2.2x", cmd.code);
27208 -                       err = -EINVAL;
27209 -                       break;
27210 -               };
27211 -
27212 -               if (err) {
27213 -                       l2cap_cmd_rej rej;
27214 -                       DBG("error %d", err);
27215 -
27216 -                       /* FIXME: Map err to a valid reason. */
27217 -                       rej.reason = __cpu_to_le16(0);
27218 -                       l2cap_send_rsp(conn, cmd.ident, L2CAP_COMMAND_REJ, L2CAP_CMD_REJ_SIZE, &rej);
27219 -               }
27220 -
27221 -               data += cmd.len;
27222 -               len  -= cmd.len;
27223 -       }
27224 -
27225 -       kfree_skb(skb);
27226 -}
27227 -
27228 -static inline int l2cap_data_channel(struct l2cap_conn *conn, __u16 cid, struct sk_buff *skb)
27229 -{
27230 -       struct sock *sk;
27231 -
27232 -       if (!(sk = l2cap_get_chan_by_scid(&conn->chan_list, cid))) {
27233 -               DBG("unknown cid 0x%4.4x", cid);
27234 -               goto drop;
27235 -       }
27236 -
27237 -       DBG("sk %p, len %d", sk, skb->len);
27238 -
27239 -       if (sk->state != BT_CONNECTED)
27240 -               goto drop;
27241 -
27242 -       if (l2cap_pi(sk)->imtu < skb->len)
27243 -               goto drop;
27244 -
27245 -       skb_queue_tail(&sk->receive_queue, skb);
27246 -       sk->data_ready(sk, skb->len);
27247 -
27248 -       return 0;
27249 -
27250 -drop:
27251 -       kfree_skb(skb);
27252 -
27253 -       return 0;
27254 -}
27255 -
27256 -static void l2cap_recv_frame(struct l2cap_conn *conn, struct sk_buff *skb)
27257 -{
27258 -       l2cap_hdr *lh = (l2cap_hdr *) skb->data;
27259 -       __u16 cid, len;
27260 -
27261 -       skb_pull(skb, L2CAP_HDR_SIZE);
27262 -       cid = __le16_to_cpu(lh->cid);
27263 -       len = __le16_to_cpu(lh->len);
27264 -
27265 -       DBG("len %d, cid 0x%4.4x", len, cid);
27266 -
27267 -       if (cid == 0x0001)
27268 -               l2cap_sig_channel(conn, skb);
27269 -       else    
27270 -               l2cap_data_channel(conn, cid, skb);
27271 -}
27272 -
27273 -/* ------------ L2CAP interface with lower layer (HCI) ------------- */
27274 -static int l2cap_dev_event(struct notifier_block *this, unsigned long event, void *ptr)
27275 -{
27276 -       struct hci_dev *hdev = (struct hci_dev *) ptr;
27277 -
27278 -       DBG("hdev %s, event %ld", hdev->name, event);
27279 -
27280 -       write_lock(&l2cap_rt_lock);
27281 -
27282 -       switch (event) {
27283 -       case HCI_DEV_UP:
27284 -               l2cap_iff_add(hdev);
27285 -               break;
27286 -
27287 -       case HCI_DEV_DOWN:
27288 -               l2cap_iff_del(hdev);
27289 -               break;
27290 -       };
27291 -
27292 -       write_unlock(&l2cap_rt_lock);
27293 -
27294 -       return NOTIFY_DONE;
27295 -}
27296 -
27297 -int l2cap_connect_ind(struct hci_dev *hdev, bdaddr_t *bdaddr)
27298 -{
27299 -       struct l2cap_iff *iff;
27300 -
27301 -       DBG("hdev %s, bdaddr %s", hdev->name, batostr(bdaddr));
27302 -
27303 -       if (!(iff = hdev->l2cap_data)) {
27304 -               ERR("unknown interface");
27305 -               return 0;
27306 -       }
27307 -
27308 -       /* Always accept connection */
27309 -       return 1;
27310 -}
27311 -
27312 -int l2cap_connect_cfm(struct hci_dev *hdev, bdaddr_t *bdaddr, __u8 status, struct hci_conn *hconn)
27313 -{
27314 -       struct l2cap_conn *conn;
27315 -       struct l2cap_iff *iff;
27316 -       int err = 0;
27317 -
27318 -       DBG("hdev %s bdaddr %s hconn %p", hdev->name, batostr(bdaddr), hconn);
27319 -
27320 -       if (!(iff = hdev->l2cap_data)) {
27321 -               ERR("unknown interface");
27322 -               return 0;
27323 -       }
27324 -
27325 -       l2cap_iff_lock(iff);
27326 -
27327 -       conn = l2cap_get_conn_by_addr(iff, bdaddr);
27328 -
27329 -       if (conn) {
27330 -               /* Outgoing connection */
27331 -               DBG("Outgoing connection: %s -> %s, %p, %2.2x", batostr(iff->bdaddr), batostr(bdaddr), conn, status);
27332 -
27333 -               if (!status && hconn) {
27334 -                       conn->state = BT_CONNECTED;
27335 -                       conn->hconn = hconn;
27336 -
27337 -                       hconn->l2cap_data = (void *)conn;
27338 -
27339 -                       /* Establish channels */
27340 -                       l2cap_conn_ready(conn);
27341 -               } else {
27342 -                       l2cap_conn_del(conn, bterr(status));
27343 -               }
27344 -       } else {
27345 -               /* Incomming connection */
27346 -               DBG("Incomming connection: %s -> %s, %2.2x", batostr(iff->bdaddr), batostr(bdaddr), status);
27347 -       
27348 -               if (status || !hconn)
27349 -                       goto done;
27350 -
27351 -               if (!(conn = l2cap_conn_add(iff, bdaddr))) {
27352 -                       err = -ENOMEM;
27353 -                       goto done;
27354 -               }
27355 -
27356 -               conn->hconn = hconn;
27357 -               hconn->l2cap_data = (void *)conn;
27358 -
27359 -               conn->state = BT_CONNECTED;
27360 -       }
27361 -
27362 -done:
27363 -       l2cap_iff_unlock(iff);
27364 -
27365 -       return err;
27366 -}
27367 -
27368 -int l2cap_disconn_ind(struct hci_conn *hconn, __u8 reason)
27369 -{
27370 -       struct l2cap_conn *conn = hconn->l2cap_data;
27371 -
27372 -       DBG("hconn %p reason %d", hconn, reason);
27373 -
27374 -       if (!conn) {
27375 -               ERR("unknown connection");
27376 -               return 0;
27377 -       }
27378 -       conn->hconn = NULL;
27379 -
27380 -       l2cap_iff_lock(conn->iff);
27381 -       l2cap_conn_del(conn, bterr(reason));
27382 -       l2cap_iff_unlock(conn->iff);
27383 -
27384 -       return 0;
27385 -}
27386 -
27387 -int l2cap_recv_acldata(struct hci_conn *hconn, struct sk_buff *skb, __u16 flags)
27388 -{
27389 -       struct l2cap_conn *conn = hconn->l2cap_data;
27390 -
27391 -       if (!conn) {
27392 -               ERR("unknown connection %p", hconn);
27393 -               goto drop;
27394 -       }
27395 -
27396 -       DBG("conn %p len %d flags 0x%x", conn, skb->len, flags);
27397 -
27398 -       if (flags & ACL_START) {
27399 -               int flen, tlen, size;
27400 -               l2cap_hdr *lh;
27401 -
27402 -               if (conn->rx_len) {
27403 -                       ERR("Unexpected start frame (len %d)", skb->len);
27404 -                       kfree_skb(conn->rx_skb); conn->rx_skb = NULL;
27405 -                       conn->rx_len = 0;
27406 -               }
27407 -
27408 -               if (skb->len < L2CAP_HDR_SIZE) {
27409 -                       ERR("Frame is too small (len %d)", skb->len);
27410 -                       goto drop;
27411 -               }
27412 -
27413 -               lh = (l2cap_hdr *)skb->data;
27414 -               tlen = __le16_to_cpu(lh->len);
27415 -               flen = skb->len - L2CAP_HDR_SIZE;
27416 -
27417 -               DBG("Start: total len %d, frag len %d", tlen, flen);
27418 -
27419 -               if (flen == tlen) {
27420 -                       /* Complete frame received */
27421 -                       l2cap_recv_frame(conn, skb);
27422 -                       return 0;
27423 -               }
27424 -
27425 -               /* Allocate skb for the complete frame (with header) */
27426 -               size = L2CAP_HDR_SIZE + tlen;
27427 -               if (!(conn->rx_skb = bluez_skb_alloc(size, GFP_ATOMIC)))
27428 -                       goto drop;
27429 -
27430 -               memcpy(skb_put(conn->rx_skb, skb->len), skb->data, skb->len);
27431 -
27432 -               conn->rx_len = tlen - flen;
27433 -       } else {
27434 -               DBG("Cont: frag len %d (expecting %d)", skb->len, conn->rx_len);
27435 -
27436 -               if (!conn->rx_len) {
27437 -                       ERR("Unexpected continuation frame (len %d)", skb->len);
27438 -                       goto drop;
27439 -               }
27440 -
27441 -               if (skb->len > conn->rx_len) {
27442 -                       ERR("Fragment is too large (len %d)", skb->len);
27443 -                       kfree_skb(conn->rx_skb); conn->rx_skb = NULL;
27444 -                       goto drop;
27445 -               }
27446 -
27447 -               memcpy(skb_put(conn->rx_skb, skb->len), skb->data, skb->len);
27448 -               conn->rx_len -= skb->len;
27449 -
27450 -               if (!conn->rx_len) {
27451 -                       /* Complete frame received */
27452 -                       l2cap_recv_frame(conn, conn->rx_skb);
27453 -                       conn->rx_skb = NULL;
27454 -               }
27455 -       }
27456 -
27457 -drop:
27458 -       kfree_skb(skb);
27459 -       return 0;
27460 -}
27461 -
27462 -struct proto_ops l2cap_sock_ops = {
27463 -       family:         PF_BLUETOOTH,
27464 -       release:        l2cap_sock_release,
27465 -       bind:           l2cap_sock_bind,
27466 -       connect:        l2cap_sock_connect,
27467 -       listen:         l2cap_sock_listen,
27468 -       accept:         l2cap_sock_accept,
27469 -       getname:        l2cap_sock_getname,
27470 -       sendmsg:        l2cap_sock_sendmsg,
27471 -       recvmsg:        l2cap_sock_recvmsg,
27472 -       poll:           l2cap_sock_poll,
27473 -       socketpair:     sock_no_socketpair,
27474 -       ioctl:          sock_no_ioctl,
27475 -       shutdown:       sock_no_shutdown,
27476 -       setsockopt:     l2cap_sock_setsockopt,
27477 -       getsockopt:     l2cap_sock_getsockopt,
27478 -       mmap:           sock_no_mmap
27479 -};
27480 -
27481 -struct net_proto_family l2cap_sock_family_ops = {
27482 -       family:         PF_BLUETOOTH,
27483 -       create:         l2cap_sock_create
27484 -};
27485 -
27486 -struct hci_proto l2cap_hci_proto = {
27487 -       name:           "L2CAP",
27488 -       id:             HCI_PROTO_L2CAP,
27489 -       connect_ind:    l2cap_connect_ind,
27490 -       connect_cfm:    l2cap_connect_cfm,
27491 -       disconn_ind:    l2cap_disconn_ind,
27492 -       recv_acldata:   l2cap_recv_acldata,
27493 -};
27494 -
27495 -struct notifier_block l2cap_nblock = {
27496 -       notifier_call: l2cap_dev_event
27497 -};
27498 -
27499 -int __init l2cap_init(void)
27500 -{
27501 -       INF("BlueZ L2CAP ver %s Copyright (C) 2000,2001 Qualcomm Inc",
27502 -               VERSION);
27503 -       INF("Written 2000,2001 by Maxim Krasnyansky <maxk@qualcomm.com>");
27504 -
27505 -       if (bluez_sock_register(BTPROTO_L2CAP, &l2cap_sock_family_ops)) {
27506 -               ERR("Can't register L2CAP socket");
27507 -               return -EPROTO;
27508 -       }
27509 -
27510 -       if (hci_register_proto(&l2cap_hci_proto) < 0) {
27511 -               ERR("Can't register L2CAP protocol");
27512 -               return -EPROTO;
27513 -       }
27514 -
27515 -       hci_register_notifier(&l2cap_nblock);
27516 -
27517 -       l2cap_register_proc();
27518 -
27519 -       return 0;
27520 -}
27521 -
27522 -void l2cap_cleanup(void)
27523 -{
27524 -       l2cap_unregister_proc();
27525 -
27526 -       /* Unregister socket, protocol and notifier */
27527 -       if (bluez_sock_unregister(BTPROTO_L2CAP))
27528 -               ERR("Can't unregister L2CAP socket");
27529 -
27530 -       if (hci_unregister_proto(&l2cap_hci_proto) < 0)
27531 -               ERR("Can't unregister L2CAP protocol");
27532 -
27533 -       hci_unregister_notifier(&l2cap_nblock);
27534 -
27535 -       /* We _must_ not have any sockets and/or connections
27536 -        * at this stage.
27537 -        */
27538 -
27539 -       /* Free interface list and unlock HCI devices */
27540 -       {
27541 -               struct list_head *list = &l2cap_iff_list;
27542 -
27543 -               while (!list_empty(list)) {
27544 -                       struct l2cap_iff *iff;
27545 -
27546 -                       iff = list_entry(list->next, struct l2cap_iff, list);
27547 -                       l2cap_iff_del(iff->hdev);
27548 -               }
27549 -       }
27550 -}
27551 -
27552 -module_init(l2cap_init);
27553 -module_exit(l2cap_cleanup);
27554 -
27555 -MODULE_AUTHOR("Maxim Krasnyansky <maxk@qualcomm.com>");
27556 -MODULE_DESCRIPTION("BlueZ L2CAP ver " VERSION);
27557 -MODULE_LICENSE("GPL");
27558 -
27559 diff -urN linux-2.4.18/net/bluetooth/l2cap_proc.c linux-2.4.18-mh15/net/bluetooth/l2cap_proc.c
27560 --- linux-2.4.18/net/bluetooth/l2cap_proc.c     2001-09-07 18:28:38.000000000 +0200
27561 +++ linux-2.4.18-mh15/net/bluetooth/l2cap_proc.c        1970-01-01 01:00:00.000000000 +0100
27562 @@ -1,165 +0,0 @@
27563 -/* 
27564 -   BlueZ - Bluetooth protocol stack for Linux
27565 -   Copyright (C) 2000-2001 Qualcomm Incorporated
27566 -
27567 -   Written 2000,2001 by Maxim Krasnyansky <maxk@qualcomm.com>
27568 -
27569 -   This program is free software; you can redistribute it and/or modify
27570 -   it under the terms of the GNU General Public License version 2 as
27571 -   published by the Free Software Foundation;
27572 -
27573 -   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
27574 -   OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
27575 -   FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS.
27576 -   IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) AND AUTHOR(S) BE LIABLE FOR ANY
27577 -   CLAIM, OR ANY SPECIAL INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES 
27578 -   WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 
27579 -   ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 
27580 -   OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
27581 -
27582 -   ALL LIABILITY, INCLUDING LIABILITY FOR INFRINGEMENT OF ANY PATENTS, 
27583 -   COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS, RELATING TO USE OF THIS 
27584 -   SOFTWARE IS DISCLAIMED.
27585 -*/
27586 -
27587 -/*
27588 - * BlueZ L2CAP proc fs support.
27589 - *
27590 - * $Id: l2cap_proc.c,v 1.2 2001/06/02 01:40:09 maxk Exp $
27591 - */
27592 -
27593 -#include <linux/config.h>
27594 -#include <linux/module.h>
27595 -
27596 -#include <linux/types.h>
27597 -#include <linux/errno.h>
27598 -#include <linux/kernel.h>
27599 -#include <linux/major.h>
27600 -#include <linux/sched.h>
27601 -#include <linux/slab.h>
27602 -#include <linux/poll.h>
27603 -#include <linux/fcntl.h>
27604 -#include <linux/init.h>
27605 -#include <linux/skbuff.h>
27606 -#include <linux/interrupt.h>
27607 -#include <linux/socket.h>
27608 -#include <linux/skbuff.h>
27609 -#include <linux/proc_fs.h>
27610 -#include <linux/list.h>
27611 -#include <net/sock.h>
27612 -
27613 -#include <asm/system.h>
27614 -#include <asm/uaccess.h>
27615 -
27616 -#include <net/bluetooth/bluez.h>
27617 -#include <net/bluetooth/bluetooth.h>
27618 -#include <net/bluetooth/hci_core.h>
27619 -#include <net/bluetooth/l2cap_core.h>
27620 -
27621 -#ifndef L2CAP_DEBUG
27622 -#undef  DBG
27623 -#define DBG( A... )
27624 -#endif
27625 -
27626 -/* ----- PROC fs support ----- */
27627 -static int l2cap_conn_dump(char *buf, struct l2cap_iff *iff)
27628 -{
27629 -       struct list_head *p;
27630 -       char *ptr = buf;
27631 -
27632 -       list_for_each(p, &iff->conn_list) {
27633 -               struct l2cap_conn *c;
27634 -
27635 -               c = list_entry(p, struct l2cap_conn, list);
27636 -               ptr += sprintf(ptr, "    %p %d %p %p %s %s\n", 
27637 -                               c, c->state, c->iff, c->hconn, batostr(&c->src), batostr(&c->dst));
27638 -       }
27639 -
27640 -       return ptr - buf;
27641 -}
27642 -
27643 -static int l2cap_iff_dump(char *buf)
27644 -{
27645 -       struct list_head *p;
27646 -       char *ptr = buf;
27647 -
27648 -       ptr += sprintf(ptr, "Interfaces:\n");
27649 -
27650 -       write_lock(&l2cap_rt_lock);
27651 -
27652 -       list_for_each(p, &l2cap_iff_list) {
27653 -               struct l2cap_iff *iff;
27654 -
27655 -               iff = list_entry(p, struct l2cap_iff, list);
27656 -
27657 -               ptr += sprintf(ptr, "  %s %p %p\n", iff->hdev->name, iff, iff->hdev);
27658 -               
27659 -               l2cap_iff_lock(iff);
27660 -               ptr += l2cap_conn_dump(ptr, iff);
27661 -               l2cap_iff_unlock(iff);
27662 -       }
27663 -
27664 -       write_unlock(&l2cap_rt_lock);
27665 -
27666 -       ptr += sprintf(ptr, "\n");
27667 -
27668 -       return ptr - buf;
27669 -}
27670 -
27671 -static int l2cap_sock_dump(char *buf, struct bluez_sock_list *list)
27672 -{
27673 -       struct l2cap_pinfo *pi;
27674 -       struct sock *sk;
27675 -       char *ptr = buf;
27676 -
27677 -       ptr += sprintf(ptr, "Sockets:\n");
27678 -
27679 -       write_lock(&list->lock);
27680 -
27681 -       for (sk = list->head; sk; sk = sk->next) {
27682 -               pi = l2cap_pi(sk);
27683 -               ptr += sprintf(ptr, "  %p %d %p %d %s %s 0x%4.4x 0x%4.4x %d %d\n", sk, sk->state, pi->conn, pi->psm,
27684 -                              batostr(&pi->src), batostr(&pi->dst), pi->scid, pi->dcid, pi->imtu, pi->omtu );
27685 -       }
27686 -
27687 -       write_unlock(&list->lock);
27688 -
27689 -       ptr += sprintf(ptr, "\n");
27690 -
27691 -       return ptr - buf;
27692 -}
27693 -
27694 -static int l2cap_read_proc(char *buf, char **start, off_t offset, int count, int *eof, void *priv)
27695 -{
27696 -       char *ptr = buf;
27697 -       int len;
27698 -
27699 -       DBG("count %d, offset %ld", count, offset);
27700 -
27701 -       ptr += l2cap_iff_dump(ptr);
27702 -       ptr += l2cap_sock_dump(ptr, &l2cap_sk_list);
27703 -       len  = ptr - buf;
27704 -
27705 -       if (len <= count + offset)
27706 -               *eof = 1;
27707 -
27708 -       *start = buf + offset;
27709 -       len -= offset;
27710 -
27711 -       if (len > count)
27712 -               len = count;
27713 -       if (len < 0)
27714 -               len = 0;
27715 -
27716 -       return len;
27717 -}
27718 -
27719 -void l2cap_register_proc(void)
27720 -{
27721 -       create_proc_read_entry("bluetooth/l2cap", 0, 0, l2cap_read_proc, NULL);
27722 -}
27723 -
27724 -void l2cap_unregister_proc(void)
27725 -{
27726 -       remove_proc_entry("bluetooth/l2cap", NULL);
27727 -}
27728 diff -urN linux-2.4.18/net/bluetooth/lib.c linux-2.4.18-mh15/net/bluetooth/lib.c
27729 --- linux-2.4.18/net/bluetooth/lib.c    2001-09-07 18:28:38.000000000 +0200
27730 +++ linux-2.4.18-mh15/net/bluetooth/lib.c       2004-08-01 16:26:23.000000000 +0200
27731 @@ -25,7 +25,7 @@
27732  /*
27733   * BlueZ kernel library.
27734   *
27735 - * $Id: lib.c,v 1.3 2001/06/22 23:14:23 maxk Exp $
27736 + * $Id: lib.c,v 1.2 2002/06/20 19:55:08 maxk Exp $
27737   */
27738  
27739  #include <linux/kernel.h>
27740 @@ -105,7 +105,7 @@
27741                 return EACCES;
27742  
27743         case 0x06:
27744 -               return EINVAL;
27745 +               return EBADE;
27746  
27747         case 0x07:
27748                 return ENOMEM;
27749 diff -urN linux-2.4.18/net/bluetooth/Makefile linux-2.4.18-mh15/net/bluetooth/Makefile
27750 --- linux-2.4.18/net/bluetooth/Makefile 2001-06-12 04:15:27.000000000 +0200
27751 +++ linux-2.4.18-mh15/net/bluetooth/Makefile    2004-08-01 16:26:23.000000000 +0200
27752 @@ -1,20 +1,40 @@
27753  #
27754 -# Makefile for the Bluetooth subsystem
27755 +# Makefile for the Linux Bluetooth subsystem
27756  #
27757 -O_TARGET       := bluetooth.o
27758  
27759 -list-multi     := hci.o l2cap.o
27760 -export-objs    := syms.o
27761 -hci-objs       := af_bluetooth.o hci_core.o hci_sock.o lib.o syms.o
27762 -l2cap-objs     := l2cap_core.o l2cap_proc.o
27763 +O_TARGET    := bluetooth.o
27764  
27765 -obj-$(CONFIG_BLUEZ) += hci.o
27766 +list-multi  := bluez.o
27767 +export-objs := syms.o l2cap.o
27768 +
27769 +bluez-objs  := af_bluetooth.o hci_core.o hci_conn.o hci_event.o hci_sock.o lib.o syms.o
27770 +
27771 +obj-$(CONFIG_BLUEZ) += bluez.o
27772  obj-$(CONFIG_BLUEZ_L2CAP) += l2cap.o
27773 +obj-$(CONFIG_BLUEZ_SCO) += sco.o
27774  
27775 -include $(TOPDIR)/Rules.make
27776 +subdir-$(CONFIG_BLUEZ_RFCOMM) += rfcomm
27777 +subdir-$(CONFIG_BLUEZ_BNEP) += bnep
27778 +subdir-$(CONFIG_BLUEZ_CMTP) += cmtp
27779 +subdir-$(CONFIG_BLUEZ_HIDP) += hidp
27780 +
27781 +ifeq ($(CONFIG_BLUEZ_RFCOMM),y)
27782 +obj-y += rfcomm/rfcomm.o
27783 +endif
27784  
27785 -hci.o: $(hci-objs)
27786 -       $(LD) -r -o $@ $(hci-objs)
27787 +ifeq ($(CONFIG_BLUEZ_BNEP),y)
27788 +obj-y += bnep/bnep.o
27789 +endif
27790 +
27791 +ifeq ($(CONFIG_BLUEZ_CMTP),y)
27792 +obj-y += cmtp/cmtp.o
27793 +endif
27794 +
27795 +ifeq ($(CONFIG_BLUEZ_HIDP),y)
27796 +obj-y += hidp/hidp.o
27797 +endif
27798 +
27799 +include $(TOPDIR)/Rules.make
27800  
27801 -l2cap.o: $(l2cap-objs)
27802 -       $(LD) -r -o $@ $(l2cap-objs)
27803 +bluez.o: $(bluez-objs)
27804 +       $(LD) -r -o $@ $(bluez-objs)
27805 diff -urN linux-2.4.18/net/bluetooth/rfcomm/Config.in linux-2.4.18-mh15/net/bluetooth/rfcomm/Config.in
27806 --- linux-2.4.18/net/bluetooth/rfcomm/Config.in 1970-01-01 01:00:00.000000000 +0100
27807 +++ linux-2.4.18-mh15/net/bluetooth/rfcomm/Config.in    2004-08-01 16:26:23.000000000 +0200
27808 @@ -0,0 +1,10 @@
27809 +#
27810 +# Bluetooth RFCOMM layer configuration
27811 +#
27812 +
27813 +dep_tristate 'RFCOMM protocol support' CONFIG_BLUEZ_RFCOMM $CONFIG_BLUEZ_L2CAP
27814 +
27815 +if [ "$CONFIG_BLUEZ_RFCOMM" != "n" ]; then
27816 +   bool '  RFCOMM TTY support' CONFIG_BLUEZ_RFCOMM_TTY
27817 +fi
27818 +
27819 diff -urN linux-2.4.18/net/bluetooth/rfcomm/core.c linux-2.4.18-mh15/net/bluetooth/rfcomm/core.c
27820 --- linux-2.4.18/net/bluetooth/rfcomm/core.c    1970-01-01 01:00:00.000000000 +0100
27821 +++ linux-2.4.18-mh15/net/bluetooth/rfcomm/core.c       2004-08-01 16:26:23.000000000 +0200
27822 @@ -0,0 +1,1940 @@
27823 +/* 
27824 +   RFCOMM implementation for Linux Bluetooth stack (BlueZ).
27825 +   Copyright (C) 2002 Maxim Krasnyansky <maxk@qualcomm.com>
27826 +   Copyright (C) 2002 Marcel Holtmann <marcel@holtmann.org>
27827 +
27828 +   This program is free software; you can redistribute it and/or modify
27829 +   it under the terms of the GNU General Public License version 2 as
27830 +   published by the Free Software Foundation;
27831 +
27832 +   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
27833 +   OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
27834 +   FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS.
27835 +   IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) AND AUTHOR(S) BE LIABLE FOR ANY
27836 +   CLAIM, OR ANY SPECIAL INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES 
27837 +   WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 
27838 +   ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 
27839 +   OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
27840 +
27841 +   ALL LIABILITY, INCLUDING LIABILITY FOR INFRINGEMENT OF ANY PATENTS, 
27842 +   COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS, RELATING TO USE OF THIS 
27843 +   SOFTWARE IS DISCLAIMED.
27844 +*/
27845 +
27846 +/* 
27847 +   RPN support    -    Dirk Husemann <hud@zurich.ibm.com>
27848 +*/
27849 +
27850 +/*
27851 + * RFCOMM core.
27852 + *
27853 + * $Id: core.c,v 1.46 2002/10/18 20:12:12 maxk Exp $
27854 + */
27855 +
27856 +#define __KERNEL_SYSCALLS__
27857 +
27858 +#include <linux/config.h>
27859 +#include <linux/module.h>
27860 +#include <linux/errno.h>
27861 +#include <linux/kernel.h>
27862 +#include <linux/sched.h>
27863 +#include <linux/signal.h>
27864 +#include <linux/init.h>
27865 +#include <linux/wait.h>
27866 +#include <linux/net.h>
27867 +#include <linux/proc_fs.h>
27868 +#include <net/sock.h>
27869 +#include <asm/uaccess.h>
27870 +#include <asm/unaligned.h>
27871 +
27872 +#include <net/bluetooth/bluetooth.h>
27873 +#include <net/bluetooth/l2cap.h>
27874 +#include <net/bluetooth/rfcomm.h>
27875 +
27876 +#define VERSION "1.1"
27877 +
27878 +#ifndef CONFIG_BLUEZ_RFCOMM_DEBUG
27879 +#undef  BT_DBG
27880 +#define BT_DBG(D...)
27881 +#endif
27882 +
27883 +struct task_struct *rfcomm_thread;
27884 +DECLARE_MUTEX(rfcomm_sem);
27885 +unsigned long rfcomm_event;
27886 +
27887 +static LIST_HEAD(session_list);
27888 +static atomic_t terminate, running;
27889 +
27890 +static int rfcomm_send_frame(struct rfcomm_session *s, u8 *data, int len);
27891 +static int rfcomm_send_sabm(struct rfcomm_session *s, u8 dlci);
27892 +static int rfcomm_send_disc(struct rfcomm_session *s, u8 dlci);
27893 +static int rfcomm_queue_disc(struct rfcomm_dlc *d);
27894 +static int rfcomm_send_nsc(struct rfcomm_session *s, int cr, u8 type);
27895 +static int rfcomm_send_pn(struct rfcomm_session *s, int cr, struct rfcomm_dlc *d);
27896 +static int rfcomm_send_msc(struct rfcomm_session *s, int cr, u8 dlci, u8 v24_sig);
27897 +static int rfcomm_send_test(struct rfcomm_session *s, int cr, u8 *pattern, int len);
27898 +static int rfcomm_send_credits(struct rfcomm_session *s, u8 addr, u8 credits);
27899 +static void rfcomm_make_uih(struct sk_buff *skb, u8 addr);
27900 +
27901 +static void rfcomm_process_connect(struct rfcomm_session *s);
27902 +
27903 +/* ---- RFCOMM frame parsing macros ---- */
27904 +#define __get_dlci(b)     ((b & 0xfc) >> 2)
27905 +#define __get_channel(b)  ((b & 0xf8) >> 3)
27906 +#define __get_dir(b)      ((b & 0x04) >> 2)
27907 +#define __get_type(b)     ((b & 0xef))
27908 +
27909 +#define __test_ea(b)      ((b & 0x01))
27910 +#define __test_cr(b)      ((b & 0x02))
27911 +#define __test_pf(b)      ((b & 0x10))
27912 +
27913 +#define __addr(cr, dlci)       (((dlci & 0x3f) << 2) | (cr << 1) | 0x01)
27914 +#define __ctrl(type, pf)       (((type & 0xef) | (pf << 4)))
27915 +#define __dlci(dir, chn)       (((chn & 0x1f) << 1) | dir)
27916 +#define __srv_channel(dlci)    (dlci >> 1)
27917 +#define __dir(dlci)            (dlci & 0x01)
27918 +
27919 +#define __len8(len)       (((len) << 1) | 1)
27920 +#define __len16(len)      ((len) << 1)
27921 +
27922 +/* MCC macros */
27923 +#define __mcc_type(cr, type)   (((type << 2) | (cr << 1) | 0x01))
27924 +#define __get_mcc_type(b) ((b & 0xfc) >> 2)
27925 +#define __get_mcc_len(b)  ((b & 0xfe) >> 1)
27926 +
27927 +/* RPN macros */
27928 +#define __rpn_line_settings(data, stop, parity)  ((data & 0x3) | ((stop & 0x1) << 2) | ((parity & 0x3) << 3))
27929 +#define __get_rpn_data_bits(line) ((line) & 0x3)
27930 +#define __get_rpn_stop_bits(line) (((line) >> 2) & 0x1)
27931 +#define __get_rpn_parity(line)    (((line) >> 3) & 0x3)
27932 +
27933 +/* ---- RFCOMM FCS computation ---- */
27934 +
27935 +/* CRC on 2 bytes */
27936 +#define __crc(data) (rfcomm_crc_table[rfcomm_crc_table[0xff ^ data[0]] ^ data[1]])
27937 +
27938 +/* FCS on 2 bytes */ 
27939 +static inline u8 __fcs(u8 *data)
27940 +{
27941 +       return (0xff - __crc(data));
27942 +}
27943 +
27944 +/* FCS on 3 bytes */ 
27945 +static inline u8 __fcs2(u8 *data)
27946 +{
27947 +       return (0xff - rfcomm_crc_table[__crc(data) ^ data[2]]);
27948 +}
27949 +
27950 +/* Check FCS */
27951 +static inline int __check_fcs(u8 *data, int type, u8 fcs)
27952 +{
27953 +       u8 f = __crc(data);
27954 +
27955 +       if (type != RFCOMM_UIH)
27956 +               f = rfcomm_crc_table[f ^ data[2]];
27957 +
27958 +       return rfcomm_crc_table[f ^ fcs] != 0xcf;
27959 +}
27960 +
27961 +/* ---- L2CAP callbacks ---- */
27962 +static void rfcomm_l2state_change(struct sock *sk)
27963 +{
27964 +       BT_DBG("%p state %d", sk, sk->state);
27965 +       rfcomm_schedule(RFCOMM_SCHED_STATE);
27966 +}
27967 +
27968 +static void rfcomm_l2data_ready(struct sock *sk, int bytes)
27969 +{
27970 +       BT_DBG("%p bytes %d", sk, bytes);
27971 +       rfcomm_schedule(RFCOMM_SCHED_RX);
27972 +}
27973 +
27974 +static int rfcomm_l2sock_create(struct socket **sock)
27975 +{
27976 +       int err;
27977 +
27978 +       BT_DBG("");
27979 +
27980 +       err = sock_create(PF_BLUETOOTH, SOCK_SEQPACKET, BTPROTO_L2CAP, sock);
27981 +       if (!err) {
27982 +               struct sock *sk = (*sock)->sk;
27983 +               sk->data_ready   = rfcomm_l2data_ready;
27984 +               sk->state_change = rfcomm_l2state_change;
27985 +       }
27986 +       return err;
27987 +}
27988 +
27989 +/* ---- RFCOMM DLCs ---- */
27990 +static void rfcomm_dlc_timeout(unsigned long arg)
27991 +{
27992 +       struct rfcomm_dlc *d = (void *) arg;
27993 +
27994 +       BT_DBG("dlc %p state %ld", d, d->state);
27995 +
27996 +       set_bit(RFCOMM_TIMED_OUT, &d->flags);
27997 +       rfcomm_dlc_put(d);
27998 +       rfcomm_schedule(RFCOMM_SCHED_TIMEO);
27999 +}
28000 +
28001 +static void rfcomm_dlc_set_timer(struct rfcomm_dlc *d, long timeout)
28002 +{
28003 +       BT_DBG("dlc %p state %ld timeout %ld", d, d->state, timeout);
28004 +
28005 +       if (!mod_timer(&d->timer, jiffies + timeout))
28006 +               rfcomm_dlc_hold(d);
28007 +}
28008 +
28009 +static void rfcomm_dlc_clear_timer(struct rfcomm_dlc *d)
28010 +{
28011 +       BT_DBG("dlc %p state %ld", d, d->state);
28012 +
28013 +       if (timer_pending(&d->timer) && del_timer(&d->timer))
28014 +               rfcomm_dlc_put(d);
28015 +}
28016 +
28017 +static void rfcomm_dlc_clear_state(struct rfcomm_dlc *d)
28018 +{
28019 +       BT_DBG("%p", d);
28020 +
28021 +       d->state      = BT_OPEN;
28022 +       d->flags      = 0;
28023 +       d->mscex      = 0;
28024 +       d->mtu        = RFCOMM_DEFAULT_MTU;
28025 +       d->v24_sig    = RFCOMM_V24_RTC | RFCOMM_V24_RTR | RFCOMM_V24_DV;
28026 +
28027 +       d->cfc        = RFCOMM_CFC_DISABLED;
28028 +       d->rx_credits = RFCOMM_DEFAULT_CREDITS;
28029 +}
28030 +
28031 +struct rfcomm_dlc *rfcomm_dlc_alloc(int prio)
28032 +{
28033 +       struct rfcomm_dlc *d = kmalloc(sizeof(*d), prio);
28034 +       if (!d)
28035 +               return NULL;
28036 +       memset(d, 0, sizeof(*d));
28037 +
28038 +       init_timer(&d->timer);
28039 +       d->timer.function = rfcomm_dlc_timeout;
28040 +       d->timer.data = (unsigned long) d;
28041 +
28042 +       skb_queue_head_init(&d->tx_queue);
28043 +       spin_lock_init(&d->lock);
28044 +       atomic_set(&d->refcnt, 1);
28045 +
28046 +       rfcomm_dlc_clear_state(d);
28047 +       
28048 +       BT_DBG("%p", d);
28049 +       return d;
28050 +}
28051 +
28052 +void rfcomm_dlc_free(struct rfcomm_dlc *d)
28053 +{
28054 +       BT_DBG("%p", d);
28055 +
28056 +       skb_queue_purge(&d->tx_queue);
28057 +       kfree(d);
28058 +}
28059 +
28060 +static void rfcomm_dlc_link(struct rfcomm_session *s, struct rfcomm_dlc *d)
28061 +{
28062 +       BT_DBG("dlc %p session %p", d, s);
28063 +
28064 +       rfcomm_session_hold(s);
28065 +
28066 +       rfcomm_dlc_hold(d);
28067 +       list_add(&d->list, &s->dlcs);
28068 +       d->session = s;
28069 +}
28070 +
28071 +static void rfcomm_dlc_unlink(struct rfcomm_dlc *d)
28072 +{
28073 +       struct rfcomm_session *s = d->session;
28074 +
28075 +       BT_DBG("dlc %p refcnt %d session %p", d, atomic_read(&d->refcnt), s);
28076 +
28077 +       list_del(&d->list);
28078 +       d->session = NULL;
28079 +       rfcomm_dlc_put(d);
28080 +
28081 +       rfcomm_session_put(s);
28082 +}
28083 +
28084 +static struct rfcomm_dlc *rfcomm_dlc_get(struct rfcomm_session *s, u8 dlci)
28085 +{
28086 +       struct rfcomm_dlc *d;
28087 +       struct list_head *p;
28088 +
28089 +       list_for_each(p, &s->dlcs) {
28090 +               d = list_entry(p, struct rfcomm_dlc, list);
28091 +               if (d->dlci == dlci)
28092 +                       return d;
28093 +       }
28094 +       return NULL;
28095 +}
28096 +
28097 +static int __rfcomm_dlc_open(struct rfcomm_dlc *d, bdaddr_t *src, bdaddr_t *dst, u8 channel)
28098 +{
28099 +       struct rfcomm_session *s;
28100 +       int err = 0;
28101 +       u8 dlci;
28102 +
28103 +       BT_DBG("dlc %p state %ld %s %s channel %d", 
28104 +                       d, d->state, batostr(src), batostr(dst), channel);
28105 +
28106 +       if (channel < 1 || channel > 30)
28107 +               return -EINVAL;
28108 +
28109 +       if (d->state != BT_OPEN && d->state != BT_CLOSED)
28110 +               return 0;
28111 +
28112 +       s = rfcomm_session_get(src, dst);
28113 +       if (!s) {
28114 +               s = rfcomm_session_create(src, dst, &err);
28115 +               if (!s)
28116 +                       return err;
28117 +       }
28118 +
28119 +       dlci = __dlci(!s->initiator, channel);
28120 +
28121 +       /* Check if DLCI already exists */
28122 +       if (rfcomm_dlc_get(s, dlci))
28123 +               return -EBUSY;
28124 +
28125 +       rfcomm_dlc_clear_state(d);
28126 +
28127 +       d->dlci     = dlci;
28128 +       d->addr     = __addr(s->initiator, dlci);
28129 +       d->priority = 7;
28130 +
28131 +       d->state    = BT_CONFIG;
28132 +       rfcomm_dlc_link(s, d);
28133 +
28134 +       d->mtu = s->mtu;
28135 +       d->cfc = (s->cfc == RFCOMM_CFC_UNKNOWN) ? 0 : s->cfc;
28136 +
28137 +       if (s->state == BT_CONNECTED)
28138 +               rfcomm_send_pn(s, 1, d);
28139 +       rfcomm_dlc_set_timer(d, RFCOMM_CONN_TIMEOUT);
28140 +       return 0;
28141 +}
28142 +
28143 +int rfcomm_dlc_open(struct rfcomm_dlc *d, bdaddr_t *src, bdaddr_t *dst, u8 channel)
28144 +{
28145 +       mm_segment_t fs;
28146 +       int r;
28147 +
28148 +       rfcomm_lock();
28149 +
28150 +       fs = get_fs(); set_fs(KERNEL_DS);
28151 +       r = __rfcomm_dlc_open(d, src, dst, channel);
28152 +       set_fs(fs);
28153 +
28154 +       rfcomm_unlock();
28155 +       return r;
28156 +}
28157 +
28158 +static int __rfcomm_dlc_close(struct rfcomm_dlc *d, int err)
28159 +{
28160 +       struct rfcomm_session *s = d->session;
28161 +       if (!s)
28162 +               return 0;
28163 +
28164 +       BT_DBG("dlc %p state %ld dlci %d err %d session %p",
28165 +                       d, d->state, d->dlci, err, s);
28166 +
28167 +       switch (d->state) {
28168 +       case BT_CONNECTED:
28169 +       case BT_CONFIG:
28170 +       case BT_CONNECT:
28171 +               d->state = BT_DISCONN;
28172 +               if (skb_queue_empty(&d->tx_queue)) {
28173 +                       rfcomm_send_disc(s, d->dlci);
28174 +                       rfcomm_dlc_set_timer(d, RFCOMM_DISC_TIMEOUT);
28175 +               } else {
28176 +                       rfcomm_queue_disc(d);
28177 +                       rfcomm_dlc_set_timer(d, RFCOMM_DISC_TIMEOUT * 2);
28178 +               }
28179 +               break;
28180 +
28181 +       default:
28182 +               rfcomm_dlc_clear_timer(d);
28183 +
28184 +               rfcomm_dlc_lock(d);
28185 +               d->state = BT_CLOSED;
28186 +               d->state_change(d, err);
28187 +               rfcomm_dlc_unlock(d);
28188 +
28189 +               skb_queue_purge(&d->tx_queue);
28190 +               rfcomm_dlc_unlink(d);
28191 +       }
28192 +
28193 +       return 0;
28194 +}
28195 +
28196 +int rfcomm_dlc_close(struct rfcomm_dlc *d, int err)
28197 +{
28198 +       mm_segment_t fs;
28199 +       int r;
28200 +
28201 +       rfcomm_lock();
28202 +
28203 +       fs = get_fs(); set_fs(KERNEL_DS);
28204 +       r = __rfcomm_dlc_close(d, err);
28205 +       set_fs(fs);
28206 +
28207 +       rfcomm_unlock();
28208 +       return r;
28209 +}
28210 +
28211 +int rfcomm_dlc_send(struct rfcomm_dlc *d, struct sk_buff *skb)
28212 +{
28213 +       int len = skb->len;
28214 +
28215 +       if (d->state != BT_CONNECTED)
28216 +               return -ENOTCONN;
28217 +
28218 +       BT_DBG("dlc %p mtu %d len %d", d, d->mtu, len);
28219 +
28220 +       if (len > d->mtu)
28221 +               return -EINVAL;
28222 +
28223 +       rfcomm_make_uih(skb, d->addr);
28224 +       skb_queue_tail(&d->tx_queue, skb);
28225 +
28226 +       if (!test_bit(RFCOMM_TX_THROTTLED, &d->flags))
28227 +               rfcomm_schedule(RFCOMM_SCHED_TX);
28228 +       return len;
28229 +}
28230 +
28231 +void __rfcomm_dlc_throttle(struct rfcomm_dlc *d)
28232 +{
28233 +       BT_DBG("dlc %p state %ld", d, d->state);
28234 +
28235 +       if (!d->cfc) {
28236 +               d->v24_sig |= RFCOMM_V24_FC;
28237 +               set_bit(RFCOMM_MSC_PENDING, &d->flags);
28238 +       }
28239 +       rfcomm_schedule(RFCOMM_SCHED_TX);
28240 +}
28241 +
28242 +void __rfcomm_dlc_unthrottle(struct rfcomm_dlc *d)
28243 +{
28244 +       BT_DBG("dlc %p state %ld", d, d->state);
28245 +
28246 +       if (!d->cfc) {
28247 +               d->v24_sig &= ~RFCOMM_V24_FC;
28248 +               set_bit(RFCOMM_MSC_PENDING, &d->flags);
28249 +       }
28250 +       rfcomm_schedule(RFCOMM_SCHED_TX);
28251 +}
28252 +
28253 +/* 
28254 +   Set/get modem status functions use _local_ status i.e. what we report
28255 +   to the other side.
28256 +   Remote status is provided by dlc->modem_status() callback.
28257 + */
28258 +int rfcomm_dlc_set_modem_status(struct rfcomm_dlc *d, u8 v24_sig)
28259 +{
28260 +       BT_DBG("dlc %p state %ld v24_sig 0x%x", 
28261 +                       d, d->state, v24_sig);
28262 +
28263 +       if (test_bit(RFCOMM_RX_THROTTLED, &d->flags))
28264 +               v24_sig |= RFCOMM_V24_FC;
28265 +       else
28266 +               v24_sig &= ~RFCOMM_V24_FC;
28267 +       
28268 +       d->v24_sig = v24_sig;
28269 +
28270 +       if (!test_and_set_bit(RFCOMM_MSC_PENDING, &d->flags))
28271 +               rfcomm_schedule(RFCOMM_SCHED_TX);
28272 +
28273 +       return 0;
28274 +}
28275 +
28276 +int rfcomm_dlc_get_modem_status(struct rfcomm_dlc *d, u8 *v24_sig)
28277 +{
28278 +       BT_DBG("dlc %p state %ld v24_sig 0x%x", 
28279 +                       d, d->state, d->v24_sig);
28280 +
28281 +       *v24_sig = d->v24_sig;
28282 +       return 0;
28283 +}
28284 +
28285 +/* ---- RFCOMM sessions ---- */
28286 +struct rfcomm_session *rfcomm_session_add(struct socket *sock, int state)
28287 +{
28288 +       struct rfcomm_session *s = kmalloc(sizeof(*s), GFP_KERNEL);
28289 +       if (!s)
28290 +               return NULL;
28291 +       memset(s, 0, sizeof(*s));
28292 +       
28293 +       BT_DBG("session %p sock %p", s, sock);
28294 +
28295 +       INIT_LIST_HEAD(&s->dlcs);
28296 +       s->state = state;
28297 +       s->sock  = sock;
28298 +
28299 +       s->mtu   = RFCOMM_DEFAULT_MTU;
28300 +       s->cfc   = RFCOMM_CFC_UNKNOWN;
28301 +       
28302 +       list_add(&s->list, &session_list);
28303 +
28304 +       /* Do not increment module usage count for listeting sessions.
28305 +        * Otherwise we won't be able to unload the module. */
28306 +       if (state != BT_LISTEN)
28307 +               MOD_INC_USE_COUNT;
28308 +       return s;
28309 +}
28310 +
28311 +void rfcomm_session_del(struct rfcomm_session *s)
28312 +{
28313 +       int state = s->state;
28314 +       
28315 +       BT_DBG("session %p state %ld", s, s->state);
28316 +
28317 +       list_del(&s->list);
28318 +
28319 +       if (state == BT_CONNECTED)
28320 +               rfcomm_send_disc(s, 0);
28321 +
28322 +       sock_release(s->sock);
28323 +       kfree(s);
28324 +
28325 +       if (state != BT_LISTEN)
28326 +               MOD_DEC_USE_COUNT;
28327 +}
28328 +
28329 +struct rfcomm_session *rfcomm_session_get(bdaddr_t *src, bdaddr_t *dst)
28330 +{
28331 +       struct rfcomm_session *s;
28332 +       struct list_head *p, *n;
28333 +       struct bluez_pinfo *pi;
28334 +       list_for_each_safe(p, n, &session_list) {
28335 +               s = list_entry(p, struct rfcomm_session, list);
28336 +               pi = bluez_pi(s->sock->sk); 
28337 +
28338 +               if ((!bacmp(src, BDADDR_ANY) || !bacmp(&pi->src, src)) &&
28339 +                               !bacmp(&pi->dst, dst))
28340 +                       return s;
28341 +       }
28342 +       return NULL;
28343 +}
28344 +
28345 +void rfcomm_session_close(struct rfcomm_session *s, int err)
28346 +{
28347 +       struct rfcomm_dlc *d;
28348 +       struct list_head *p, *n;
28349 +
28350 +       BT_DBG("session %p state %ld err %d", s, s->state, err);
28351 +
28352 +       rfcomm_session_hold(s);
28353 +
28354 +       s->state = BT_CLOSED;
28355 +
28356 +       /* Close all dlcs */
28357 +       list_for_each_safe(p, n, &s->dlcs) {
28358 +               d = list_entry(p, struct rfcomm_dlc, list);
28359 +               d->state = BT_CLOSED;
28360 +               __rfcomm_dlc_close(d, err);
28361 +       }
28362 +
28363 +       rfcomm_session_put(s);
28364 +}
28365 +
28366 +struct rfcomm_session *rfcomm_session_create(bdaddr_t *src, bdaddr_t *dst, int *err)
28367 +{
28368 +       struct rfcomm_session *s = NULL;
28369 +       struct sockaddr_l2 addr;
28370 +       struct l2cap_options opts;
28371 +       struct socket *sock;
28372 +       int    size;
28373 +
28374 +       BT_DBG("%s %s", batostr(src), batostr(dst));
28375 +
28376 +       *err = rfcomm_l2sock_create(&sock);
28377 +       if (*err < 0)
28378 +               return NULL;
28379 +
28380 +       bacpy(&addr.l2_bdaddr, src);
28381 +       addr.l2_family = AF_BLUETOOTH;
28382 +       addr.l2_psm    = 0;
28383 +       *err = sock->ops->bind(sock, (struct sockaddr *) &addr, sizeof(addr));
28384 +       if (*err < 0)
28385 +               goto failed;
28386 +
28387 +       /* Set L2CAP options */
28388 +       size = sizeof(opts);
28389 +       sock->ops->getsockopt(sock, SOL_L2CAP, L2CAP_OPTIONS, (void *)&opts, &size);
28390 +       
28391 +       opts.imtu = RFCOMM_MAX_L2CAP_MTU;
28392 +       sock->ops->setsockopt(sock, SOL_L2CAP, L2CAP_OPTIONS, (void *)&opts, size);
28393 +
28394 +       s = rfcomm_session_add(sock, BT_BOUND);
28395 +       if (!s) {
28396 +               *err = -ENOMEM;
28397 +               goto failed;
28398 +       }
28399 +
28400 +       s->initiator = 1;
28401 +
28402 +       bacpy(&addr.l2_bdaddr, dst);
28403 +       addr.l2_family = AF_BLUETOOTH;
28404 +       addr.l2_psm    = htobs(RFCOMM_PSM);
28405 +       *err = sock->ops->connect(sock, (struct sockaddr *) &addr, sizeof(addr), O_NONBLOCK);
28406 +       if (*err == 0 || *err == -EAGAIN)
28407 +               return s;
28408 +
28409 +       rfcomm_session_del(s);
28410 +       return NULL;
28411 +
28412 +failed:
28413 +       sock_release(sock);
28414 +       return NULL;
28415 +}
28416 +
28417 +void rfcomm_session_getaddr(struct rfcomm_session *s, bdaddr_t *src, bdaddr_t *dst)
28418 +{
28419 +       struct sock *sk = s->sock->sk;
28420 +       if (src)
28421 +               bacpy(src, &bluez_pi(sk)->src);
28422 +       if (dst)
28423 +               bacpy(dst, &bluez_pi(sk)->dst);
28424 +}
28425 +
28426 +/* ---- RFCOMM frame sending ---- */
28427 +static int rfcomm_send_frame(struct rfcomm_session *s, u8 *data, int len)
28428 +{
28429 +       struct socket *sock = s->sock;
28430 +       struct iovec iv = { data, len };
28431 +       struct msghdr msg;
28432 +       int err;
28433 +
28434 +       BT_DBG("session %p len %d", s, len);
28435 +
28436 +       memset(&msg, 0, sizeof(msg));
28437 +       msg.msg_iovlen = 1;
28438 +       msg.msg_iov = &iv;
28439 +
28440 +       err = sock->ops->sendmsg(sock, &msg, len, 0);
28441 +       return err;
28442 +}
28443 +
28444 +static int rfcomm_send_sabm(struct rfcomm_session *s, u8 dlci)
28445 +{
28446 +       struct rfcomm_cmd cmd;
28447 +
28448 +       BT_DBG("%p dlci %d", s, dlci);
28449 +
28450 +       cmd.addr = __addr(s->initiator, dlci);
28451 +       cmd.ctrl = __ctrl(RFCOMM_SABM, 1);
28452 +       cmd.len  = __len8(0);
28453 +       cmd.fcs  = __fcs2((u8 *) &cmd);
28454 +
28455 +       return rfcomm_send_frame(s, (void *) &cmd, sizeof(cmd));
28456 +}
28457 +
28458 +static int rfcomm_send_ua(struct rfcomm_session *s, u8 dlci)
28459 +{
28460 +       struct rfcomm_cmd cmd;
28461 +
28462 +       BT_DBG("%p dlci %d", s, dlci);
28463 +
28464 +       cmd.addr = __addr(!s->initiator, dlci);
28465 +       cmd.ctrl = __ctrl(RFCOMM_UA, 1);
28466 +       cmd.len  = __len8(0);
28467 +       cmd.fcs  = __fcs2((u8 *) &cmd);
28468 +
28469 +       return rfcomm_send_frame(s, (void *) &cmd, sizeof(cmd));
28470 +}
28471 +
28472 +static int rfcomm_send_disc(struct rfcomm_session *s, u8 dlci)
28473 +{
28474 +       struct rfcomm_cmd cmd;
28475 +
28476 +       BT_DBG("%p dlci %d", s, dlci);
28477 +
28478 +       cmd.addr = __addr(s->initiator, dlci);
28479 +       cmd.ctrl = __ctrl(RFCOMM_DISC, 1);
28480 +       cmd.len  = __len8(0);
28481 +       cmd.fcs  = __fcs2((u8 *) &cmd);
28482 +
28483 +       return rfcomm_send_frame(s, (void *) &cmd, sizeof(cmd));
28484 +}
28485 +
28486 +static int rfcomm_queue_disc(struct rfcomm_dlc *d)
28487 +{
28488 +       struct rfcomm_cmd *cmd;
28489 +       struct sk_buff *skb;
28490 +
28491 +       BT_DBG("dlc %p dlci %d", d, d->dlci);
28492 +
28493 +       skb = alloc_skb(sizeof(*cmd), GFP_KERNEL);
28494 +       if (!skb)
28495 +               return -ENOMEM;
28496 +
28497 +       cmd = (void *) __skb_put(skb, sizeof(*cmd));
28498 +       cmd->addr = d->addr;
28499 +       cmd->ctrl = __ctrl(RFCOMM_DISC, 1);
28500 +       cmd->len  = __len8(0);
28501 +       cmd->fcs  = __fcs2((u8 *) cmd);
28502 +
28503 +       skb_queue_tail(&d->tx_queue, skb);
28504 +       rfcomm_schedule(RFCOMM_SCHED_TX);
28505 +       return 0;
28506 +}
28507 +
28508 +static int rfcomm_send_dm(struct rfcomm_session *s, u8 dlci)
28509 +{
28510 +       struct rfcomm_cmd cmd;
28511 +
28512 +       BT_DBG("%p dlci %d", s, dlci);
28513 +
28514 +       cmd.addr = __addr(!s->initiator, dlci);
28515 +       cmd.ctrl = __ctrl(RFCOMM_DM, 1);
28516 +       cmd.len  = __len8(0);
28517 +       cmd.fcs  = __fcs2((u8 *) &cmd);
28518 +
28519 +       return rfcomm_send_frame(s, (void *) &cmd, sizeof(cmd));
28520 +}
28521 +
28522 +static int rfcomm_send_nsc(struct rfcomm_session *s, int cr, u8 type)
28523 +{
28524 +       struct rfcomm_hdr *hdr;
28525 +       struct rfcomm_mcc *mcc;
28526 +       u8 buf[16], *ptr = buf;
28527 +
28528 +       BT_DBG("%p cr %d type %d", s, cr, type);
28529 +
28530 +       hdr = (void *) ptr; ptr += sizeof(*hdr);
28531 +       hdr->addr = __addr(s->initiator, 0);
28532 +       hdr->ctrl = __ctrl(RFCOMM_UIH, 0);
28533 +       hdr->len  = __len8(sizeof(*mcc) + 1);
28534 +
28535 +       mcc = (void *) ptr; ptr += sizeof(*mcc);
28536 +       mcc->type = __mcc_type(cr, RFCOMM_NSC);
28537 +       mcc->len  = __len8(1);
28538 +
28539 +       /* Type that we didn't like */
28540 +       *ptr = __mcc_type(cr, type); ptr++;
28541 +
28542 +       *ptr = __fcs(buf); ptr++;
28543 +
28544 +       return rfcomm_send_frame(s, buf, ptr - buf);
28545 +}
28546 +
28547 +static int rfcomm_send_pn(struct rfcomm_session *s, int cr, struct rfcomm_dlc *d)
28548 +{
28549 +       struct rfcomm_hdr *hdr;
28550 +       struct rfcomm_mcc *mcc;
28551 +       struct rfcomm_pn  *pn;
28552 +       u8 buf[16], *ptr = buf;
28553 +
28554 +       BT_DBG("%p cr %d dlci %d mtu %d", s, cr, d->dlci, d->mtu);
28555 +
28556 +       hdr = (void *) ptr; ptr += sizeof(*hdr);
28557 +       hdr->addr = __addr(s->initiator, 0);
28558 +       hdr->ctrl = __ctrl(RFCOMM_UIH, 0);
28559 +       hdr->len  = __len8(sizeof(*mcc) + sizeof(*pn));
28560 +
28561 +       mcc = (void *) ptr; ptr += sizeof(*mcc);
28562 +       mcc->type = __mcc_type(cr, RFCOMM_PN);
28563 +       mcc->len  = __len8(sizeof(*pn));
28564 +
28565 +       pn = (void *) ptr; ptr += sizeof(*pn);
28566 +       pn->dlci        = d->dlci;
28567 +       pn->priority    = d->priority;
28568 +       pn->ack_timer   = 0;
28569 +       pn->max_retrans = 0;
28570 +
28571 +       if (s->cfc) {
28572 +               pn->flow_ctrl = cr ? 0xf0 : 0xe0;
28573 +               pn->credits = RFCOMM_DEFAULT_CREDITS;
28574 +       } else {
28575 +               pn->flow_ctrl = 0;
28576 +               pn->credits   = 0;
28577 +       }
28578 +
28579 +       pn->mtu = htobs(d->mtu);
28580 +
28581 +       *ptr = __fcs(buf); ptr++;
28582 +
28583 +       return rfcomm_send_frame(s, buf, ptr - buf);
28584 +}
28585 +
28586 +static int rfcomm_send_rpn(struct rfcomm_session *s, int cr, u8 dlci,
28587 +                          u8 bit_rate, u8 data_bits, u8 stop_bits,
28588 +                          u8 parity, u8 flow_ctrl_settings, 
28589 +                          u8 xon_char, u8 xoff_char, u16 param_mask)
28590 +{
28591 +       struct rfcomm_hdr *hdr;
28592 +       struct rfcomm_mcc *mcc;
28593 +       struct rfcomm_rpn *rpn;
28594 +       u8 buf[16], *ptr = buf;
28595 +
28596 +       BT_DBG("%p cr %d dlci %d bit_r 0x%x data_b 0x%x stop_b 0x%x parity 0x%x"
28597 +              "flwc_s 0x%x xon_c 0x%x xoff_c 0x%x p_mask 0x%x", 
28598 +                       s, cr, dlci, bit_rate, data_bits, stop_bits, parity, 
28599 +                       flow_ctrl_settings, xon_char, xoff_char, param_mask);
28600 +
28601 +       hdr = (void *) ptr; ptr += sizeof(*hdr);
28602 +       hdr->addr = __addr(s->initiator, 0);
28603 +       hdr->ctrl = __ctrl(RFCOMM_UIH, 0);
28604 +       hdr->len  = __len8(sizeof(*mcc) + sizeof(*rpn));
28605 +
28606 +       mcc = (void *) ptr; ptr += sizeof(*mcc);
28607 +       mcc->type = __mcc_type(cr, RFCOMM_RPN);
28608 +       mcc->len  = __len8(sizeof(*rpn));
28609 +
28610 +       rpn = (void *) ptr; ptr += sizeof(*rpn);
28611 +       rpn->dlci          = __addr(1, dlci);
28612 +       rpn->bit_rate      = bit_rate;
28613 +       rpn->line_settings = __rpn_line_settings(data_bits, stop_bits, parity);
28614 +       rpn->flow_ctrl     = flow_ctrl_settings;
28615 +       rpn->xon_char      = xon_char;
28616 +       rpn->xoff_char     = xoff_char;
28617 +       rpn->param_mask    = param_mask;
28618 +
28619 +       *ptr = __fcs(buf); ptr++;
28620 +
28621 +       return rfcomm_send_frame(s, buf, ptr - buf);
28622 +}
28623 +
28624 +static int rfcomm_send_rls(struct rfcomm_session *s, int cr, u8 dlci, u8 status)
28625 +{
28626 +       struct rfcomm_hdr *hdr;
28627 +       struct rfcomm_mcc *mcc;
28628 +       struct rfcomm_rls *rls;
28629 +       u8 buf[16], *ptr = buf;
28630 +
28631 +       BT_DBG("%p cr %d status 0x%x", s, cr, status);
28632 +
28633 +       hdr = (void *) ptr; ptr += sizeof(*hdr);
28634 +       hdr->addr = __addr(s->initiator, 0);
28635 +       hdr->ctrl = __ctrl(RFCOMM_UIH, 0);
28636 +       hdr->len  = __len8(sizeof(*mcc) + sizeof(*rls));
28637 +
28638 +       mcc = (void *) ptr; ptr += sizeof(*mcc);
28639 +       mcc->type = __mcc_type(cr, RFCOMM_RLS);
28640 +       mcc->len  = __len8(sizeof(*rls));
28641 +
28642 +       rls = (void *) ptr; ptr += sizeof(*rls);
28643 +       rls->dlci   = __addr(1, dlci);
28644 +       rls->status = status;
28645 +
28646 +       *ptr = __fcs(buf); ptr++;
28647 +
28648 +       return rfcomm_send_frame(s, buf, ptr - buf);
28649 +}
28650 +
28651 +static int rfcomm_send_msc(struct rfcomm_session *s, int cr, u8 dlci, u8 v24_sig)
28652 +{
28653 +       struct rfcomm_hdr *hdr;
28654 +       struct rfcomm_mcc *mcc;
28655 +       struct rfcomm_msc *msc;
28656 +       u8 buf[16], *ptr = buf;
28657 +
28658 +       BT_DBG("%p cr %d v24 0x%x", s, cr, v24_sig);
28659 +
28660 +       hdr = (void *) ptr; ptr += sizeof(*hdr);
28661 +       hdr->addr = __addr(s->initiator, 0);
28662 +       hdr->ctrl = __ctrl(RFCOMM_UIH, 0);
28663 +       hdr->len  = __len8(sizeof(*mcc) + sizeof(*msc));
28664 +
28665 +       mcc = (void *) ptr; ptr += sizeof(*mcc);
28666 +       mcc->type = __mcc_type(cr, RFCOMM_MSC);
28667 +       mcc->len  = __len8(sizeof(*msc));
28668 +
28669 +       msc = (void *) ptr; ptr += sizeof(*msc);
28670 +       msc->dlci    = __addr(1, dlci);
28671 +       msc->v24_sig = v24_sig | 0x01;
28672 +
28673 +       *ptr = __fcs(buf); ptr++;
28674 +
28675 +       return rfcomm_send_frame(s, buf, ptr - buf);
28676 +}
28677 +
28678 +static int rfcomm_send_fcoff(struct rfcomm_session *s, int cr)
28679 +{
28680 +       struct rfcomm_hdr *hdr;
28681 +       struct rfcomm_mcc *mcc;
28682 +       u8 buf[16], *ptr = buf;
28683 +
28684 +       BT_DBG("%p cr %d", s, cr);
28685 +
28686 +       hdr = (void *) ptr; ptr += sizeof(*hdr);
28687 +       hdr->addr = __addr(s->initiator, 0);
28688 +       hdr->ctrl = __ctrl(RFCOMM_UIH, 0);
28689 +       hdr->len  = __len8(sizeof(*mcc));
28690 +
28691 +       mcc = (void *) ptr; ptr += sizeof(*mcc);
28692 +       mcc->type = __mcc_type(cr, RFCOMM_FCOFF);
28693 +       mcc->len  = __len8(0);
28694 +
28695 +       *ptr = __fcs(buf); ptr++;
28696 +
28697 +       return rfcomm_send_frame(s, buf, ptr - buf);
28698 +}
28699 +
28700 +static int rfcomm_send_fcon(struct rfcomm_session *s, int cr)
28701 +{
28702 +       struct rfcomm_hdr *hdr;
28703 +       struct rfcomm_mcc *mcc;
28704 +       u8 buf[16], *ptr = buf;
28705 +
28706 +       BT_DBG("%p cr %d", s, cr);
28707 +
28708 +       hdr = (void *) ptr; ptr += sizeof(*hdr);
28709 +       hdr->addr = __addr(s->initiator, 0);
28710 +       hdr->ctrl = __ctrl(RFCOMM_UIH, 0);
28711 +       hdr->len  = __len8(sizeof(*mcc));
28712 +
28713 +       mcc = (void *) ptr; ptr += sizeof(*mcc);
28714 +       mcc->type = __mcc_type(cr, RFCOMM_FCON);
28715 +       mcc->len  = __len8(0);
28716 +
28717 +       *ptr = __fcs(buf); ptr++;
28718 +
28719 +       return rfcomm_send_frame(s, buf, ptr - buf);
28720 +}
28721 +
28722 +static int rfcomm_send_test(struct rfcomm_session *s, int cr, u8 *pattern, int len)
28723 +{
28724 +       struct socket *sock = s->sock;
28725 +       struct iovec iv[3];
28726 +       struct msghdr msg;
28727 +       unsigned char hdr[5], crc[1];
28728 +
28729 +       if (len > 125)
28730 +               return -EINVAL;
28731 +
28732 +       BT_DBG("%p cr %d", s, cr);
28733 +
28734 +       hdr[0] = __addr(s->initiator, 0);
28735 +       hdr[1] = __ctrl(RFCOMM_UIH, 0);
28736 +       hdr[2] = 0x01 | ((len + 2) << 1);
28737 +       hdr[3] = 0x01 | ((cr & 0x01) << 1) | (RFCOMM_TEST << 2);
28738 +       hdr[4] = 0x01 | (len << 1);
28739 +
28740 +       crc[0] = __fcs(hdr);
28741 +
28742 +       iv[0].iov_base = hdr;
28743 +       iv[0].iov_len  = 5;
28744 +       iv[1].iov_base = pattern;
28745 +       iv[1].iov_len  = len;
28746 +       iv[2].iov_base = crc;
28747 +       iv[2].iov_len  = 1;
28748 +
28749 +       memset(&msg, 0, sizeof(msg));
28750 +       msg.msg_iovlen = 3;
28751 +       msg.msg_iov = iv;
28752 +       return sock->ops->sendmsg(sock, &msg, 6 + len, 0);
28753 +}
28754 +
28755 +static int rfcomm_send_credits(struct rfcomm_session *s, u8 addr, u8 credits)
28756 +{
28757 +       struct rfcomm_hdr *hdr;
28758 +       u8 buf[16], *ptr = buf;
28759 +
28760 +       BT_DBG("%p addr %d credits %d", s, addr, credits);
28761 +
28762 +       hdr = (void *) ptr; ptr += sizeof(*hdr);
28763 +       hdr->addr = addr;
28764 +       hdr->ctrl = __ctrl(RFCOMM_UIH, 1);
28765 +       hdr->len  = __len8(0);
28766 +
28767 +       *ptr = credits; ptr++;
28768 +
28769 +       *ptr = __fcs(buf); ptr++;
28770 +
28771 +       return rfcomm_send_frame(s, buf, ptr - buf);
28772 +}
28773 +
28774 +static void rfcomm_make_uih(struct sk_buff *skb, u8 addr)
28775 +{
28776 +       struct rfcomm_hdr *hdr;
28777 +       int len = skb->len;
28778 +       u8 *crc;
28779 +
28780 +       if (len > 127) {
28781 +               hdr = (void *) skb_push(skb, 4);
28782 +               put_unaligned(htobs(__len16(len)), (u16 *) &hdr->len);
28783 +       } else {
28784 +               hdr = (void *) skb_push(skb, 3);
28785 +               hdr->len = __len8(len);
28786 +       }
28787 +       hdr->addr = addr;
28788 +       hdr->ctrl = __ctrl(RFCOMM_UIH, 0);
28789 +
28790 +       crc = skb_put(skb, 1);
28791 +       *crc = __fcs((void *) hdr);
28792 +}
28793 +
28794 +/* ---- RFCOMM frame reception ---- */
28795 +static int rfcomm_recv_ua(struct rfcomm_session *s, u8 dlci)
28796 +{
28797 +       BT_DBG("session %p state %ld dlci %d", s, s->state, dlci);
28798 +
28799 +       if (dlci) {
28800 +               /* Data channel */
28801 +               struct rfcomm_dlc *d = rfcomm_dlc_get(s, dlci);
28802 +               if (!d) {
28803 +                       rfcomm_send_dm(s, dlci);
28804 +                       return 0;
28805 +               }
28806 +
28807 +               switch (d->state) {
28808 +               case BT_CONNECT:
28809 +                       rfcomm_dlc_clear_timer(d);
28810 +
28811 +                       rfcomm_dlc_lock(d);
28812 +                       d->state = BT_CONNECTED;
28813 +                       d->state_change(d, 0);
28814 +                       rfcomm_dlc_unlock(d);
28815 +
28816 +                       rfcomm_send_msc(s, 1, dlci, d->v24_sig);
28817 +                       break;
28818 +
28819 +               case BT_DISCONN:
28820 +                       d->state = BT_CLOSED;
28821 +                       __rfcomm_dlc_close(d, 0);
28822 +                       break;
28823 +               }
28824 +       } else {
28825 +               /* Control channel */
28826 +               switch (s->state) {
28827 +               case BT_CONNECT:
28828 +                       s->state = BT_CONNECTED;
28829 +                       rfcomm_process_connect(s);
28830 +                       break;
28831 +               }
28832 +       }
28833 +       return 0;
28834 +}
28835 +
28836 +static int rfcomm_recv_dm(struct rfcomm_session *s, u8 dlci)
28837 +{
28838 +       int err = 0;
28839 +
28840 +       BT_DBG("session %p state %ld dlci %d", s, s->state, dlci);
28841 +
28842 +       if (dlci) {
28843 +               /* Data DLC */
28844 +               struct rfcomm_dlc *d = rfcomm_dlc_get(s, dlci);
28845 +               if (d) {
28846 +                       if (d->state == BT_CONNECT || d->state == BT_CONFIG)
28847 +                               err = ECONNREFUSED;
28848 +                       else
28849 +                               err = ECONNRESET;
28850 +
28851 +                       d->state = BT_CLOSED;
28852 +                       __rfcomm_dlc_close(d, err);
28853 +               }
28854 +       } else {
28855 +               if (s->state == BT_CONNECT)
28856 +                       err = ECONNREFUSED;
28857 +               else
28858 +                       err = ECONNRESET;
28859 +
28860 +               s->state = BT_CLOSED;
28861 +               rfcomm_session_close(s, err);
28862 +       }
28863 +       return 0;
28864 +}
28865 +
28866 +static int rfcomm_recv_disc(struct rfcomm_session *s, u8 dlci)
28867 +{
28868 +       int err = 0;
28869 +
28870 +       BT_DBG("session %p state %ld dlci %d", s, s->state, dlci);
28871 +
28872 +       if (dlci) {
28873 +               struct rfcomm_dlc *d = rfcomm_dlc_get(s, dlci);
28874 +               if (d) {
28875 +                       rfcomm_send_ua(s, dlci);
28876 +
28877 +                       if (d->state == BT_CONNECT || d->state == BT_CONFIG)
28878 +                               err = ECONNREFUSED;
28879 +                       else
28880 +                               err = ECONNRESET;
28881 +
28882 +                       d->state = BT_CLOSED;
28883 +                       __rfcomm_dlc_close(d, err);
28884 +               } else 
28885 +                       rfcomm_send_dm(s, dlci);
28886 +                       
28887 +       } else {
28888 +               rfcomm_send_ua(s, 0);
28889 +
28890 +               if (s->state == BT_CONNECT)
28891 +                       err = ECONNREFUSED;
28892 +               else
28893 +                       err = ECONNRESET;
28894 +
28895 +               s->state = BT_CLOSED;
28896 +               rfcomm_session_close(s, err);
28897 +       }
28898 +
28899 +       return 0;
28900 +}
28901 +
28902 +static int rfcomm_recv_sabm(struct rfcomm_session *s, u8 dlci)
28903 +{
28904 +       struct rfcomm_dlc *d;
28905 +       u8 channel;
28906 +
28907 +       BT_DBG("session %p state %ld dlci %d", s, s->state, dlci);
28908 +
28909 +       if (!dlci) {
28910 +               rfcomm_send_ua(s, 0);
28911 +
28912 +               if (s->state == BT_OPEN) {
28913 +                       s->state = BT_CONNECTED;
28914 +                       rfcomm_process_connect(s);
28915 +               }
28916 +               return 0;
28917 +       }
28918 +
28919 +       /* Check if DLC exists */
28920 +       d = rfcomm_dlc_get(s, dlci);
28921 +       if (d) {
28922 +               if (d->state == BT_OPEN) {
28923 +                       /* DLC was previously opened by PN request */
28924 +                       rfcomm_send_ua(s, dlci);
28925 +
28926 +                       rfcomm_dlc_lock(d);
28927 +                       d->state = BT_CONNECTED;
28928 +                       d->state_change(d, 0);
28929 +                       rfcomm_dlc_unlock(d);
28930 +
28931 +                       rfcomm_send_msc(s, 1, dlci, d->v24_sig);
28932 +               }
28933 +               return 0;
28934 +       }
28935 +
28936 +       /* Notify socket layer about incomming connection */
28937 +       channel = __srv_channel(dlci);
28938 +       if (rfcomm_connect_ind(s, channel, &d)) {
28939 +               d->dlci = dlci;
28940 +               d->addr = __addr(s->initiator, dlci);
28941 +               rfcomm_dlc_link(s, d);
28942 +
28943 +               rfcomm_send_ua(s, dlci);
28944 +
28945 +               rfcomm_dlc_lock(d);
28946 +               d->state = BT_CONNECTED;
28947 +               d->state_change(d, 0);
28948 +               rfcomm_dlc_unlock(d);
28949 +
28950 +               rfcomm_send_msc(s, 1, dlci, d->v24_sig);
28951 +       } else {
28952 +               rfcomm_send_dm(s, dlci);
28953 +       }
28954 +
28955 +       return 0;
28956 +}
28957 +
28958 +static int rfcomm_apply_pn(struct rfcomm_dlc *d, int cr, struct rfcomm_pn *pn)
28959 +{
28960 +       struct rfcomm_session *s = d->session;
28961 +
28962 +       BT_DBG("dlc %p state %ld dlci %d mtu %d fc 0x%x credits %d", 
28963 +                       d, d->state, d->dlci, pn->mtu, pn->flow_ctrl, pn->credits);
28964 +
28965 +       if (pn->flow_ctrl == 0xf0 || pn->flow_ctrl == 0xe0) {
28966 +               d->cfc = s->cfc = RFCOMM_CFC_ENABLED;
28967 +               d->tx_credits = pn->credits;
28968 +       } else {
28969 +               d->cfc = s->cfc = RFCOMM_CFC_DISABLED;
28970 +               set_bit(RFCOMM_TX_THROTTLED, &d->flags);
28971 +       }
28972 +
28973 +       d->priority = pn->priority;
28974 +
28975 +       d->mtu = s->mtu = btohs(pn->mtu);
28976 +
28977 +       return 0;
28978 +}
28979 +
28980 +static int rfcomm_recv_pn(struct rfcomm_session *s, int cr, struct sk_buff *skb)
28981 +{
28982 +       struct rfcomm_pn *pn = (void *) skb->data;
28983 +       struct rfcomm_dlc *d;
28984 +       u8 dlci = pn->dlci;
28985 +
28986 +       BT_DBG("session %p state %ld dlci %d", s, s->state, dlci);
28987 +
28988 +       if (!dlci)
28989 +               return 0;
28990 +
28991 +       d = rfcomm_dlc_get(s, dlci);
28992 +       if (d) {
28993 +               if (cr) {
28994 +                       /* PN request */
28995 +                       rfcomm_apply_pn(d, cr, pn);
28996 +                       rfcomm_send_pn(s, 0, d);
28997 +               } else {
28998 +                       /* PN response */
28999 +                       switch (d->state) {
29000 +                       case BT_CONFIG:
29001 +                               rfcomm_apply_pn(d, cr, pn);
29002 +
29003 +                               d->state = BT_CONNECT;
29004 +                               rfcomm_send_sabm(s, d->dlci);
29005 +                               break;
29006 +                       }
29007 +               }
29008 +       } else {
29009 +               u8 channel = __srv_channel(dlci);
29010 +
29011 +               if (!cr)
29012 +                       return 0;
29013 +
29014 +               /* PN request for non existing DLC.
29015 +                * Assume incomming connection. */
29016 +               if (rfcomm_connect_ind(s, channel, &d)) {
29017 +                       d->dlci = dlci;
29018 +                       d->addr = __addr(s->initiator, dlci);
29019 +                       rfcomm_dlc_link(s, d);
29020 +
29021 +                       rfcomm_apply_pn(d, cr, pn);
29022 +
29023 +                       d->state = BT_OPEN;
29024 +                       rfcomm_send_pn(s, 0, d);
29025 +               } else {
29026 +                       rfcomm_send_dm(s, dlci);
29027 +               }
29028 +       }
29029 +       return 0;
29030 +}
29031 +
29032 +static int rfcomm_recv_rpn(struct rfcomm_session *s, int cr, int len, struct sk_buff *skb)
29033 +{
29034 +       struct rfcomm_rpn *rpn = (void *) skb->data;
29035 +       u8 dlci = __get_dlci(rpn->dlci);
29036 +
29037 +       u8 bit_rate  = 0;
29038 +       u8 data_bits = 0;
29039 +       u8 stop_bits = 0;
29040 +       u8 parity    = 0;
29041 +       u8 flow_ctrl = 0;
29042 +       u8 xon_char  = 0;
29043 +       u8 xoff_char = 0;
29044 +       u16 rpn_mask = RFCOMM_RPN_PM_ALL;
29045 +       
29046 +       BT_DBG("dlci %d cr %d len 0x%x bitr 0x%x line 0x%x flow 0x%x xonc 0x%x xoffc 0x%x pm 0x%x", 
29047 +              dlci, cr, len, rpn->bit_rate, rpn->line_settings, rpn->flow_ctrl,
29048 +              rpn->xon_char, rpn->xoff_char, rpn->param_mask);
29049 +       
29050 +       if (!cr) 
29051 +               return 0;
29052 +       
29053 +       if (len == 1) {
29054 +               /* request: return default setting */
29055 +               bit_rate  = RFCOMM_RPN_BR_115200;
29056 +               data_bits = RFCOMM_RPN_DATA_8;
29057 +               stop_bits = RFCOMM_RPN_STOP_1;
29058 +               parity    = RFCOMM_RPN_PARITY_NONE;
29059 +               flow_ctrl = RFCOMM_RPN_FLOW_NONE;
29060 +               xon_char  = RFCOMM_RPN_XON_CHAR;
29061 +               xoff_char = RFCOMM_RPN_XOFF_CHAR;
29062 +
29063 +               goto rpn_out;
29064 +       }
29065 +       /* check for sane values: ignore/accept bit_rate, 8 bits, 1 stop bit, no parity,
29066 +                                 no flow control lines, normal XON/XOFF chars */
29067 +       if (rpn->param_mask & RFCOMM_RPN_PM_BITRATE) {
29068 +               bit_rate = rpn->bit_rate;
29069 +               if (bit_rate != RFCOMM_RPN_BR_115200) {
29070 +                       BT_DBG("RPN bit rate mismatch 0x%x", bit_rate);
29071 +                       bit_rate = RFCOMM_RPN_BR_115200;
29072 +                       rpn_mask ^= RFCOMM_RPN_PM_BITRATE;
29073 +               }
29074 +       }
29075 +       if (rpn->param_mask & RFCOMM_RPN_PM_DATA) {
29076 +               data_bits = __get_rpn_data_bits(rpn->line_settings);
29077 +               if (data_bits != RFCOMM_RPN_DATA_8) {
29078 +                       BT_DBG("RPN data bits mismatch 0x%x", data_bits);
29079 +                       data_bits = RFCOMM_RPN_DATA_8;
29080 +                       rpn_mask ^= RFCOMM_RPN_PM_DATA;
29081 +               }
29082 +       }
29083 +       if (rpn->param_mask & RFCOMM_RPN_PM_STOP) {
29084 +               stop_bits = __get_rpn_stop_bits(rpn->line_settings);
29085 +               if (stop_bits != RFCOMM_RPN_STOP_1) {
29086 +                       BT_DBG("RPN stop bits mismatch 0x%x", stop_bits);
29087 +                       stop_bits = RFCOMM_RPN_STOP_1;
29088 +                       rpn_mask ^= RFCOMM_RPN_PM_STOP;
29089 +               }
29090 +       }
29091 +       if (rpn->param_mask & RFCOMM_RPN_PM_PARITY) {
29092 +               parity = __get_rpn_parity(rpn->line_settings);
29093 +               if (parity != RFCOMM_RPN_PARITY_NONE) {
29094 +                       BT_DBG("RPN parity mismatch 0x%x", parity);
29095 +                       parity = RFCOMM_RPN_PARITY_NONE;
29096 +                       rpn_mask ^= RFCOMM_RPN_PM_PARITY;
29097 +               }
29098 +       }
29099 +       if (rpn->param_mask & RFCOMM_RPN_PM_FLOW) {
29100 +               flow_ctrl = rpn->flow_ctrl;
29101 +               if (flow_ctrl != RFCOMM_RPN_FLOW_NONE) {
29102 +                       BT_DBG("RPN flow ctrl mismatch 0x%x", flow_ctrl);
29103 +                       flow_ctrl = RFCOMM_RPN_FLOW_NONE;
29104 +                       rpn_mask ^= RFCOMM_RPN_PM_FLOW;
29105 +               }
29106 +       }
29107 +       if (rpn->param_mask & RFCOMM_RPN_PM_XON) {
29108 +               xon_char = rpn->xon_char;
29109 +               if (xon_char != RFCOMM_RPN_XON_CHAR) {
29110 +                       BT_DBG("RPN XON char mismatch 0x%x", xon_char);
29111 +                       xon_char = RFCOMM_RPN_XON_CHAR;
29112 +                       rpn_mask ^= RFCOMM_RPN_PM_XON;
29113 +               }
29114 +       }
29115 +       if (rpn->param_mask & RFCOMM_RPN_PM_XOFF) {
29116 +               xoff_char = rpn->xoff_char;
29117 +               if (xoff_char != RFCOMM_RPN_XOFF_CHAR) {
29118 +                       BT_DBG("RPN XOFF char mismatch 0x%x", xoff_char);
29119 +                       xoff_char = RFCOMM_RPN_XOFF_CHAR;
29120 +                       rpn_mask ^= RFCOMM_RPN_PM_XOFF;
29121 +               }
29122 +       }
29123 +
29124 +rpn_out:
29125 +       rfcomm_send_rpn(s, 0, dlci, 
29126 +                       bit_rate, data_bits, stop_bits, parity, flow_ctrl,
29127 +                       xon_char, xoff_char, rpn_mask);
29128 +
29129 +       return 0;
29130 +}
29131 +
29132 +static int rfcomm_recv_rls(struct rfcomm_session *s, int cr, struct sk_buff *skb)
29133 +{
29134 +       struct rfcomm_rls *rls = (void *) skb->data;
29135 +       u8 dlci = __get_dlci(rls->dlci);
29136 +
29137 +       BT_DBG("dlci %d cr %d status 0x%x", dlci, cr, rls->status);
29138 +       
29139 +       if (!cr)
29140 +               return 0;
29141 +
29142 +       /* FIXME: We should probably do something with this
29143 +          information here. But for now it's sufficient just
29144 +          to reply -- Bluetooth 1.1 says it's mandatory to 
29145 +          recognise and respond to RLS */
29146 +
29147 +       rfcomm_send_rls(s, 0, dlci, rls->status);
29148 +
29149 +       return 0;
29150 +}
29151 +
29152 +static int rfcomm_recv_msc(struct rfcomm_session *s, int cr, struct sk_buff *skb)
29153 +{
29154 +       struct rfcomm_msc *msc = (void *) skb->data;
29155 +       struct rfcomm_dlc *d;
29156 +       u8 dlci = __get_dlci(msc->dlci);
29157 +
29158 +       BT_DBG("dlci %d cr %d v24 0x%x", dlci, cr, msc->v24_sig);
29159 +
29160 +       d = rfcomm_dlc_get(s, dlci);
29161 +       if (!d)
29162 +               return 0;
29163 +
29164 +       if (cr) {
29165 +               if (msc->v24_sig & RFCOMM_V24_FC && !d->cfc)
29166 +                       set_bit(RFCOMM_TX_THROTTLED, &d->flags);
29167 +               else
29168 +                       clear_bit(RFCOMM_TX_THROTTLED, &d->flags);
29169 +               
29170 +               rfcomm_dlc_lock(d);
29171 +               if (d->modem_status)
29172 +                       d->modem_status(d, msc->v24_sig);
29173 +               rfcomm_dlc_unlock(d);
29174 +               
29175 +               rfcomm_send_msc(s, 0, dlci, msc->v24_sig);
29176 +
29177 +               d->mscex |= RFCOMM_MSCEX_RX;
29178 +       } else 
29179 +               d->mscex |= RFCOMM_MSCEX_TX;
29180 +
29181 +       return 0;
29182 +}
29183 +
29184 +static int rfcomm_recv_mcc(struct rfcomm_session *s, struct sk_buff *skb)
29185 +{
29186 +       struct rfcomm_mcc *mcc = (void *) skb->data;
29187 +       u8 type, cr, len;
29188 +
29189 +       cr   = __test_cr(mcc->type);
29190 +       type = __get_mcc_type(mcc->type);
29191 +       len  = __get_mcc_len(mcc->len);
29192 +
29193 +       BT_DBG("%p type 0x%x cr %d", s, type, cr);
29194 +
29195 +       skb_pull(skb, 2);
29196 +
29197 +       switch (type) {
29198 +       case RFCOMM_PN:
29199 +               rfcomm_recv_pn(s, cr, skb);
29200 +               break;
29201 +
29202 +       case RFCOMM_RPN:
29203 +               rfcomm_recv_rpn(s, cr, len, skb);
29204 +               break;
29205 +
29206 +       case RFCOMM_RLS:
29207 +               rfcomm_recv_rls(s, cr, skb);
29208 +               break;
29209 +
29210 +       case RFCOMM_MSC:
29211 +               rfcomm_recv_msc(s, cr, skb);
29212 +               break;
29213 +
29214 +       case RFCOMM_FCOFF:
29215 +               if (cr) {
29216 +                       set_bit(RFCOMM_TX_THROTTLED, &s->flags);
29217 +                       rfcomm_send_fcoff(s, 0);
29218 +               }
29219 +               break;
29220 +
29221 +       case RFCOMM_FCON:
29222 +               if (cr) {
29223 +                       clear_bit(RFCOMM_TX_THROTTLED, &s->flags);
29224 +                       rfcomm_send_fcon(s, 0);
29225 +               }
29226 +               break;
29227 +
29228 +       case RFCOMM_TEST:
29229 +               if (cr)
29230 +                       rfcomm_send_test(s, 0, skb->data, skb->len);
29231 +               break;
29232 +
29233 +       case RFCOMM_NSC:
29234 +               break;
29235 +
29236 +       default:
29237 +               BT_ERR("Unknown control type 0x%02x", type);
29238 +               rfcomm_send_nsc(s, cr, type);
29239 +               break;
29240 +       }
29241 +       return 0;
29242 +}
29243 +
29244 +static int rfcomm_recv_data(struct rfcomm_session *s, u8 dlci, int pf, struct sk_buff *skb)
29245 +{
29246 +       struct rfcomm_dlc *d;
29247 +
29248 +       BT_DBG("session %p state %ld dlci %d pf %d", s, s->state, dlci, pf);
29249 +
29250 +       d = rfcomm_dlc_get(s, dlci);
29251 +       if (!d) {
29252 +               rfcomm_send_dm(s, dlci);
29253 +               goto drop;
29254 +       }
29255 +
29256 +       if (pf && d->cfc) {
29257 +               u8 credits = *(u8 *) skb->data; skb_pull(skb, 1);
29258 +
29259 +               d->tx_credits += credits;
29260 +               if (d->tx_credits)
29261 +                       clear_bit(RFCOMM_TX_THROTTLED, &d->flags);
29262 +       }
29263 +
29264 +       if (skb->len && d->state == BT_CONNECTED) {
29265 +               rfcomm_dlc_lock(d);
29266 +               d->rx_credits--;
29267 +               d->data_ready(d, skb);
29268 +               rfcomm_dlc_unlock(d);
29269 +               return 0;
29270 +       }
29271 +
29272 +drop:
29273 +       kfree_skb(skb);
29274 +       return 0;
29275 +}
29276 +
29277 +static int rfcomm_recv_frame(struct rfcomm_session *s, struct sk_buff *skb)
29278 +{
29279 +       struct rfcomm_hdr *hdr = (void *) skb->data;
29280 +       u8 type, dlci, fcs;
29281 +
29282 +       dlci = __get_dlci(hdr->addr);
29283 +       type = __get_type(hdr->ctrl);
29284 +
29285 +       /* Trim FCS */
29286 +       skb->len--; skb->tail--;
29287 +       fcs = *(u8 *) skb->tail;
29288 +       
29289 +       if (__check_fcs(skb->data, type, fcs)) {
29290 +               BT_ERR("bad checksum in packet");
29291 +               kfree_skb(skb);
29292 +               return -EILSEQ;
29293 +       }
29294 +
29295 +       if (__test_ea(hdr->len))
29296 +               skb_pull(skb, 3);
29297 +       else
29298 +               skb_pull(skb, 4);
29299 +       
29300 +       switch (type) {
29301 +       case RFCOMM_SABM:
29302 +               if (__test_pf(hdr->ctrl))
29303 +                       rfcomm_recv_sabm(s, dlci);
29304 +               break;
29305 +
29306 +       case RFCOMM_DISC:
29307 +               if (__test_pf(hdr->ctrl))
29308 +                       rfcomm_recv_disc(s, dlci);
29309 +               break;
29310 +
29311 +       case RFCOMM_UA:
29312 +               if (__test_pf(hdr->ctrl))
29313 +                       rfcomm_recv_ua(s, dlci);
29314 +               break;
29315 +
29316 +       case RFCOMM_DM:
29317 +               rfcomm_recv_dm(s, dlci);
29318 +               break;
29319 +
29320 +       case RFCOMM_UIH:
29321 +               if (dlci)
29322 +                       return rfcomm_recv_data(s, dlci, __test_pf(hdr->ctrl), skb);
29323 +
29324 +               rfcomm_recv_mcc(s, skb);
29325 +               break;
29326 +
29327 +       default:
29328 +               BT_ERR("Unknown packet type 0x%02x\n", type);
29329 +               break;
29330 +       }
29331 +       kfree_skb(skb);
29332 +       return 0;
29333 +}
29334 +
29335 +/* ---- Connection and data processing ---- */
29336 +
29337 +static void rfcomm_process_connect(struct rfcomm_session *s)
29338 +{
29339 +       struct rfcomm_dlc *d;
29340 +       struct list_head *p, *n;
29341 +
29342 +       BT_DBG("session %p state %ld", s, s->state);
29343 +
29344 +       list_for_each_safe(p, n, &s->dlcs) {
29345 +               d = list_entry(p, struct rfcomm_dlc, list);
29346 +               if (d->state == BT_CONFIG) {
29347 +                       d->mtu = s->mtu;
29348 +                       rfcomm_send_pn(s, 1, d);
29349 +               }
29350 +       }
29351 +}
29352 +
29353 +/* Send data queued for the DLC.
29354 + * Return number of frames left in the queue.
29355 + */
29356 +static inline int rfcomm_process_tx(struct rfcomm_dlc *d)
29357 +{
29358 +       struct sk_buff *skb;
29359 +       int err;
29360 +
29361 +       BT_DBG("dlc %p state %ld cfc %d rx_credits %d tx_credits %d", 
29362 +                       d, d->state, d->cfc, d->rx_credits, d->tx_credits);
29363 +
29364 +       /* Send pending MSC */
29365 +       if (test_and_clear_bit(RFCOMM_MSC_PENDING, &d->flags))
29366 +               rfcomm_send_msc(d->session, 1, d->dlci, d->v24_sig);
29367 +       
29368 +       if (d->cfc) {
29369 +               /* CFC enabled. 
29370 +                * Give them some credits */
29371 +               if (!test_bit(RFCOMM_RX_THROTTLED, &d->flags) &&
29372 +                               d->rx_credits <= (d->cfc >> 2)) {
29373 +                       rfcomm_send_credits(d->session, d->addr, d->cfc - d->rx_credits);
29374 +                       d->rx_credits = d->cfc;
29375 +               }
29376 +       } else {
29377 +               /* CFC disabled. 
29378 +                * Give ourselves some credits */
29379 +               d->tx_credits = 5;
29380 +       }
29381 +
29382 +       if (test_bit(RFCOMM_TX_THROTTLED, &d->flags))
29383 +               return skb_queue_len(&d->tx_queue);
29384 +
29385 +       while (d->tx_credits && (skb = skb_dequeue(&d->tx_queue))) {
29386 +               err = rfcomm_send_frame(d->session, skb->data, skb->len);
29387 +               if (err < 0) {
29388 +                       skb_queue_head(&d->tx_queue, skb);
29389 +                       break;
29390 +               }
29391 +               kfree_skb(skb);
29392 +               d->tx_credits--;
29393 +       }
29394 +
29395 +       if (d->cfc && !d->tx_credits) {
29396 +               /* We're out of TX credits.
29397 +                * Set TX_THROTTLED flag to avoid unnesary wakeups by dlc_send. */
29398 +               set_bit(RFCOMM_TX_THROTTLED, &d->flags);
29399 +       }
29400 +
29401 +       return skb_queue_len(&d->tx_queue);
29402 +}
29403 +
29404 +static inline void rfcomm_process_dlcs(struct rfcomm_session *s)
29405 +{
29406 +       struct rfcomm_dlc *d;
29407 +       struct list_head *p, *n;
29408 +
29409 +       BT_DBG("session %p state %ld", s, s->state);
29410 +
29411 +       list_for_each_safe(p, n, &s->dlcs) {
29412 +               d = list_entry(p, struct rfcomm_dlc, list);
29413 +               if (test_bit(RFCOMM_TIMED_OUT, &d->flags)) {
29414 +                       __rfcomm_dlc_close(d, ETIMEDOUT);
29415 +                       continue;
29416 +               }
29417 +
29418 +               if (test_bit(RFCOMM_TX_THROTTLED, &s->flags))
29419 +                       continue;
29420 +
29421 +               if ((d->state == BT_CONNECTED || d->state == BT_DISCONN) &&
29422 +                               d->mscex == RFCOMM_MSCEX_OK)
29423 +                       rfcomm_process_tx(d);
29424 +       }
29425 +}
29426 +
29427 +static inline void rfcomm_process_rx(struct rfcomm_session *s)
29428 +{
29429 +       struct socket *sock = s->sock;
29430 +       struct sock *sk = sock->sk;
29431 +       struct sk_buff *skb;
29432 +
29433 +       BT_DBG("session %p state %ld qlen %d", s, s->state, skb_queue_len(&sk->receive_queue));
29434 +
29435 +       /* Get data directly from socket receive queue without copying it. */
29436 +       while ((skb = skb_dequeue(&sk->receive_queue))) {
29437 +               skb_orphan(skb);
29438 +               rfcomm_recv_frame(s, skb);
29439 +       }
29440 +
29441 +       if (sk->state == BT_CLOSED) {
29442 +               if (!s->initiator)
29443 +                       rfcomm_session_put(s);
29444 +
29445 +               rfcomm_session_close(s, sk->err);
29446 +       }
29447 +}
29448 +
29449 +static inline void rfcomm_accept_connection(struct rfcomm_session *s)
29450 +{
29451 +       struct socket *sock = s->sock, *nsock;
29452 +       int err;
29453 +
29454 +       /* Fast check for a new connection.
29455 +        * Avoids unnesesary socket allocations. */
29456 +       if (list_empty(&bluez_pi(sock->sk)->accept_q))
29457 +               return;
29458 +
29459 +       BT_DBG("session %p", s);
29460 +
29461 +       nsock = sock_alloc();
29462 +       if (!nsock)
29463 +               return;
29464 +
29465 +       nsock->type = sock->type;
29466 +       nsock->ops  = sock->ops;
29467 +       
29468 +       err = sock->ops->accept(sock, nsock, O_NONBLOCK);
29469 +       if (err < 0) {
29470 +               sock_release(nsock);
29471 +               return;
29472 +       }
29473 +
29474 +       /* Set our callbacks */
29475 +       nsock->sk->data_ready   = rfcomm_l2data_ready;
29476 +       nsock->sk->state_change = rfcomm_l2state_change;
29477 +
29478 +       s = rfcomm_session_add(nsock, BT_OPEN);
29479 +       if (s) {
29480 +               rfcomm_session_hold(s);
29481 +               rfcomm_schedule(RFCOMM_SCHED_RX);
29482 +       } else
29483 +               sock_release(nsock);
29484 +}
29485 +
29486 +static inline void rfcomm_check_connection(struct rfcomm_session *s)
29487 +{
29488 +       struct sock *sk = s->sock->sk;
29489 +
29490 +       BT_DBG("%p state %ld", s, s->state);
29491 +
29492 +       switch(sk->state) {
29493 +       case BT_CONNECTED:
29494 +               s->state = BT_CONNECT;
29495 +
29496 +               /* We can adjust MTU on outgoing sessions.
29497 +                * L2CAP MTU minus UIH header and FCS. */
29498 +               s->mtu = min(l2cap_pi(sk)->omtu, l2cap_pi(sk)->imtu) - 5;
29499 +
29500 +               rfcomm_send_sabm(s, 0);
29501 +               break;
29502 +
29503 +       case BT_CLOSED:
29504 +               s->state = BT_CLOSED;
29505 +               rfcomm_session_close(s, sk->err);
29506 +               break;
29507 +       }
29508 +}
29509 +
29510 +static inline void rfcomm_process_sessions(void)
29511 +{
29512 +       struct list_head *p, *n;
29513 +
29514 +       rfcomm_lock();
29515 +
29516 +       list_for_each_safe(p, n, &session_list) {
29517 +               struct rfcomm_session *s;
29518 +               s = list_entry(p, struct rfcomm_session, list);
29519 +
29520 +               if (s->state == BT_LISTEN) {
29521 +                       rfcomm_accept_connection(s);
29522 +                       continue;
29523 +               }
29524 +
29525 +               rfcomm_session_hold(s);
29526 +
29527 +               switch (s->state) {
29528 +               case BT_BOUND:
29529 +                       rfcomm_check_connection(s);
29530 +                       break;
29531 +
29532 +               default:
29533 +                       rfcomm_process_rx(s);
29534 +                       break;
29535 +               }
29536 +
29537 +               rfcomm_process_dlcs(s);
29538 +
29539 +               rfcomm_session_put(s);
29540 +       }
29541 +       
29542 +       rfcomm_unlock();
29543 +}
29544 +
29545 +static void rfcomm_worker(void)
29546 +{
29547 +       BT_DBG("");
29548 +
29549 +       daemonize(); reparent_to_init();
29550 +       set_fs(KERNEL_DS);
29551 +
29552 +       while (!atomic_read(&terminate)) {
29553 +               BT_DBG("worker loop event 0x%lx", rfcomm_event);
29554 +
29555 +               if (!test_bit(RFCOMM_SCHED_WAKEUP, &rfcomm_event)) {
29556 +                       /* No pending events. Let's sleep.
29557 +                        * Incomming connections and data will wake us up. */
29558 +                       set_current_state(TASK_INTERRUPTIBLE);
29559 +                       schedule();
29560 +               }
29561 +
29562 +               /* Process stuff */
29563 +               clear_bit(RFCOMM_SCHED_WAKEUP, &rfcomm_event);
29564 +               rfcomm_process_sessions();
29565 +       }
29566 +       set_current_state(TASK_RUNNING);
29567 +       return;
29568 +}
29569 +
29570 +static int rfcomm_add_listener(bdaddr_t *ba)
29571 +{
29572 +       struct sockaddr_l2 addr;
29573 +       struct l2cap_options opts;
29574 +       struct socket *sock;
29575 +       struct rfcomm_session *s;
29576 +       int    size, err = 0;
29577 +
29578 +       /* Create socket */
29579 +       err = rfcomm_l2sock_create(&sock);
29580 +       if (err < 0) { 
29581 +               BT_ERR("Create socket failed %d", err);
29582 +               return err;
29583 +       }
29584 +
29585 +       /* Bind socket */
29586 +       bacpy(&addr.l2_bdaddr, ba);
29587 +       addr.l2_family = AF_BLUETOOTH;
29588 +       addr.l2_psm    = htobs(RFCOMM_PSM);
29589 +       err = sock->ops->bind(sock, (struct sockaddr *) &addr, sizeof(addr));
29590 +       if (err < 0) {
29591 +               BT_ERR("Bind failed %d", err);
29592 +               goto failed;
29593 +       }
29594 +
29595 +       /* Set L2CAP options */
29596 +       size = sizeof(opts);
29597 +       sock->ops->getsockopt(sock, SOL_L2CAP, L2CAP_OPTIONS, (void *)&opts, &size);
29598 +
29599 +       opts.imtu = RFCOMM_MAX_L2CAP_MTU;
29600 +       sock->ops->setsockopt(sock, SOL_L2CAP, L2CAP_OPTIONS, (void *)&opts, size);
29601 +
29602 +       /* Start listening on the socket */
29603 +       err = sock->ops->listen(sock, 10);
29604 +       if (err) {
29605 +               BT_ERR("Listen failed %d", err);
29606 +               goto failed;
29607 +       }
29608 +
29609 +       /* Add listening session */
29610 +       s = rfcomm_session_add(sock, BT_LISTEN);
29611 +       if (!s)
29612 +               goto failed;
29613 +
29614 +       rfcomm_session_hold(s);
29615 +       return 0;
29616 +failed:
29617 +       sock_release(sock);
29618 +       return err;
29619 +}
29620 +
29621 +static void rfcomm_kill_listener(void)
29622 +{
29623 +       struct rfcomm_session *s;
29624 +       struct list_head *p, *n;
29625 +
29626 +       BT_DBG("");
29627 +
29628 +       list_for_each_safe(p, n, &session_list) {
29629 +               s = list_entry(p, struct rfcomm_session, list);
29630 +               rfcomm_session_del(s);
29631 +       }
29632 +}
29633 +
29634 +static int rfcomm_run(void *unused)
29635 +{
29636 +       rfcomm_thread = current;
29637 +
29638 +       atomic_inc(&running);
29639 +
29640 +       daemonize(); reparent_to_init();
29641 +
29642 +       sigfillset(&current->blocked);
29643 +       set_fs(KERNEL_DS);
29644 +
29645 +       sprintf(current->comm, "krfcommd");
29646 +
29647 +       BT_DBG("");
29648 +
29649 +       rfcomm_add_listener(BDADDR_ANY);
29650 +
29651 +       rfcomm_worker();
29652 +
29653 +       rfcomm_kill_listener();
29654 +
29655 +       atomic_dec(&running);
29656 +       return 0;
29657 +}
29658 +
29659 +/* ---- Proc fs support ---- */
29660 +static int rfcomm_dlc_dump(char *buf)
29661 +{
29662 +       struct rfcomm_session *s;
29663 +       struct sock *sk;
29664 +       struct list_head *p, *pp;
29665 +       char *ptr = buf;
29666 +
29667 +       rfcomm_lock();
29668 +
29669 +       list_for_each(p, &session_list) {
29670 +               s = list_entry(p, struct rfcomm_session, list);
29671 +               sk = s->sock->sk;
29672 +
29673 +               list_for_each(pp, &s->dlcs) {
29674 +               struct rfcomm_dlc *d;
29675 +                       d = list_entry(pp, struct rfcomm_dlc, list);
29676 +
29677 +                       ptr += sprintf(ptr, "dlc %s %s %ld %d %d %d %d\n",
29678 +                               batostr(&bluez_pi(sk)->src), batostr(&bluez_pi(sk)->dst),
29679 +                               d->state, d->dlci, d->mtu, d->rx_credits, d->tx_credits);
29680 +               }
29681 +       }
29682 +       
29683 +       rfcomm_unlock();
29684 +
29685 +       return ptr - buf;
29686 +}
29687 +
29688 +extern int rfcomm_sock_dump(char *buf);
29689 +
29690 +static int rfcomm_read_proc(char *buf, char **start, off_t offset, int count, int *eof, void *priv)
29691 +{
29692 +       char *ptr = buf;
29693 +       int len;
29694 +
29695 +       BT_DBG("count %d, offset %ld", count, offset);
29696 +
29697 +       ptr += rfcomm_dlc_dump(ptr);
29698 +       ptr += rfcomm_sock_dump(ptr);
29699 +       len  = ptr - buf;
29700 +
29701 +       if (len <= count + offset)
29702 +               *eof = 1;
29703 +
29704 +       *start = buf + offset;
29705 +       len -= offset;
29706 +
29707 +       if (len > count)
29708 +               len = count;
29709 +       if (len < 0)
29710 +               len = 0;
29711 +
29712 +       return len;
29713 +}
29714 +
29715 +/* ---- Initialization ---- */
29716 +int __init rfcomm_init(void)
29717 +{
29718 +       l2cap_load();
29719 +
29720 +       kernel_thread(rfcomm_run, NULL, CLONE_FS | CLONE_FILES | CLONE_SIGHAND);
29721 +
29722 +       rfcomm_init_sockets();
29723 +
29724 +#ifdef CONFIG_BLUEZ_RFCOMM_TTY
29725 +       rfcomm_init_ttys();
29726 +#endif
29727 +
29728 +       create_proc_read_entry("bluetooth/rfcomm", 0, 0, rfcomm_read_proc, NULL);
29729 +
29730 +       BT_INFO("BlueZ RFCOMM ver %s", VERSION);
29731 +       BT_INFO("Copyright (C) 2002 Maxim Krasnyansky <maxk@qualcomm.com>");
29732 +       BT_INFO("Copyright (C) 2002 Marcel Holtmann <marcel@holtmann.org>");
29733 +       return 0;
29734 +}
29735 +
29736 +void rfcomm_cleanup(void)
29737 +{
29738 +       /* Terminate working thread.
29739 +        * ie. Set terminate flag and wake it up */
29740 +       atomic_inc(&terminate);
29741 +       rfcomm_schedule(RFCOMM_SCHED_STATE);
29742 +
29743 +       /* Wait until thread is running */
29744 +       while (atomic_read(&running))
29745 +               schedule();
29746 +
29747 +       remove_proc_entry("bluetooth/rfcomm", NULL);
29748 +
29749 +#ifdef CONFIG_BLUEZ_RFCOMM_TTY
29750 +       rfcomm_cleanup_ttys();
29751 +#endif
29752 +
29753 +       rfcomm_cleanup_sockets();
29754 +       return;
29755 +}
29756 +
29757 +module_init(rfcomm_init);
29758 +module_exit(rfcomm_cleanup);
29759 +
29760 +MODULE_AUTHOR("Maxim Krasnyansky <maxk@qualcomm.com>, Marcel Holtmann <marcel@holtmann.org>");
29761 +MODULE_DESCRIPTION("BlueZ RFCOMM ver " VERSION);
29762 +MODULE_LICENSE("GPL");
29763 diff -urN linux-2.4.18/net/bluetooth/rfcomm/crc.c linux-2.4.18-mh15/net/bluetooth/rfcomm/crc.c
29764 --- linux-2.4.18/net/bluetooth/rfcomm/crc.c     1970-01-01 01:00:00.000000000 +0100
29765 +++ linux-2.4.18-mh15/net/bluetooth/rfcomm/crc.c        2004-08-01 16:26:23.000000000 +0200
29766 @@ -0,0 +1,71 @@
29767 +/* 
29768 +   RFCOMM implementation for Linux Bluetooth stack (BlueZ).
29769 +   Copyright (C) 2002 Maxim Krasnyansky <maxk@qualcomm.com>
29770 +   Copyright (C) 2002 Marcel Holtmann <marcel@holtmann.org>
29771 +
29772 +   This program is free software; you can redistribute it and/or modify
29773 +   it under the terms of the GNU General Public License version 2 as
29774 +   published by the Free Software Foundation;
29775 +
29776 +   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
29777 +   OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
29778 +   FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS.
29779 +   IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) AND AUTHOR(S) BE LIABLE FOR ANY
29780 +   CLAIM, OR ANY SPECIAL INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES 
29781 +   WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 
29782 +   ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 
29783 +   OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
29784 +
29785 +   ALL LIABILITY, INCLUDING LIABILITY FOR INFRINGEMENT OF ANY PATENTS, 
29786 +   COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS, RELATING TO USE OF THIS 
29787 +   SOFTWARE IS DISCLAIMED.
29788 +*/
29789 +
29790 +/*
29791 + * RFCOMM FCS calculation.
29792 + *
29793 + * $Id: crc.c,v 1.2 2002/09/21 09:54:32 holtmann Exp $
29794 + */
29795 +
29796 +/* reversed, 8-bit, poly=0x07 */
29797 +unsigned char rfcomm_crc_table[256] = { 
29798 +       0x00, 0x91, 0xe3, 0x72, 0x07, 0x96, 0xe4, 0x75,
29799 +       0x0e, 0x9f, 0xed, 0x7c, 0x09, 0x98, 0xea, 0x7b,
29800 +       0x1c, 0x8d, 0xff, 0x6e, 0x1b, 0x8a, 0xf8, 0x69,
29801 +       0x12, 0x83, 0xf1, 0x60, 0x15, 0x84, 0xf6, 0x67,
29802 +
29803 +       0x38, 0xa9, 0xdb, 0x4a, 0x3f, 0xae, 0xdc, 0x4d,
29804 +       0x36, 0xa7, 0xd5, 0x44, 0x31, 0xa0, 0xd2, 0x43,
29805 +       0x24, 0xb5, 0xc7, 0x56, 0x23, 0xb2, 0xc0, 0x51,
29806 +       0x2a, 0xbb, 0xc9, 0x58, 0x2d, 0xbc, 0xce, 0x5f,
29807 +
29808 +       0x70, 0xe1, 0x93, 0x02, 0x77, 0xe6, 0x94, 0x05,
29809 +       0x7e, 0xef, 0x9d, 0x0c, 0x79, 0xe8, 0x9a, 0x0b,
29810 +       0x6c, 0xfd, 0x8f, 0x1e, 0x6b, 0xfa, 0x88, 0x19,
29811 +       0x62, 0xf3, 0x81, 0x10, 0x65, 0xf4, 0x86, 0x17,
29812 +
29813 +       0x48, 0xd9, 0xab, 0x3a, 0x4f, 0xde, 0xac, 0x3d,
29814 +       0x46, 0xd7, 0xa5, 0x34, 0x41, 0xd0, 0xa2, 0x33,
29815 +       0x54, 0xc5, 0xb7, 0x26, 0x53, 0xc2, 0xb0, 0x21,
29816 +       0x5a, 0xcb, 0xb9, 0x28, 0x5d, 0xcc, 0xbe, 0x2f,
29817 +
29818 +       0xe0, 0x71, 0x03, 0x92, 0xe7, 0x76, 0x04, 0x95,
29819 +       0xee, 0x7f, 0x0d, 0x9c, 0xe9, 0x78, 0x0a, 0x9b,
29820 +       0xfc, 0x6d, 0x1f, 0x8e, 0xfb, 0x6a, 0x18, 0x89,
29821 +       0xf2, 0x63, 0x11, 0x80, 0xf5, 0x64, 0x16, 0x87,
29822 +
29823 +       0xd8, 0x49, 0x3b, 0xaa, 0xdf, 0x4e, 0x3c, 0xad,
29824 +       0xd6, 0x47, 0x35, 0xa4, 0xd1, 0x40, 0x32, 0xa3,
29825 +       0xc4, 0x55, 0x27, 0xb6, 0xc3, 0x52, 0x20, 0xb1,
29826 +       0xca, 0x5b, 0x29, 0xb8, 0xcd, 0x5c, 0x2e, 0xbf,
29827 +
29828 +       0x90, 0x01, 0x73, 0xe2, 0x97, 0x06, 0x74, 0xe5,
29829 +       0x9e, 0x0f, 0x7d, 0xec, 0x99, 0x08, 0x7a, 0xeb,
29830 +       0x8c, 0x1d, 0x6f, 0xfe, 0x8b, 0x1a, 0x68, 0xf9,
29831 +       0x82, 0x13, 0x61, 0xf0, 0x85, 0x14, 0x66, 0xf7,
29832 +
29833 +       0xa8, 0x39, 0x4b, 0xda, 0xaf, 0x3e, 0x4c, 0xdd,
29834 +       0xa6, 0x37, 0x45, 0xd4, 0xa1, 0x30, 0x42, 0xd3,
29835 +       0xb4, 0x25, 0x57, 0xc6, 0xb3, 0x22, 0x50, 0xc1,
29836 +       0xba, 0x2b, 0x59, 0xc8, 0xbd, 0x2c, 0x5e, 0xcf
29837 +};
29838 diff -urN linux-2.4.18/net/bluetooth/rfcomm/Makefile linux-2.4.18-mh15/net/bluetooth/rfcomm/Makefile
29839 --- linux-2.4.18/net/bluetooth/rfcomm/Makefile  1970-01-01 01:00:00.000000000 +0100
29840 +++ linux-2.4.18-mh15/net/bluetooth/rfcomm/Makefile     2004-08-01 16:26:23.000000000 +0200
29841 @@ -0,0 +1,11 @@
29842 +#
29843 +# Makefile for the Linux Bluetooth RFCOMM layer
29844 +#
29845 +
29846 +O_TARGET := rfcomm.o
29847 +
29848 +obj-y                          := core.o sock.o crc.o
29849 +obj-$(CONFIG_BLUEZ_RFCOMM_TTY) += tty.o
29850 +obj-m                          += $(O_TARGET)
29851 +
29852 +include $(TOPDIR)/Rules.make
29853 diff -urN linux-2.4.18/net/bluetooth/rfcomm/sock.c linux-2.4.18-mh15/net/bluetooth/rfcomm/sock.c
29854 --- linux-2.4.18/net/bluetooth/rfcomm/sock.c    1970-01-01 01:00:00.000000000 +0100
29855 +++ linux-2.4.18-mh15/net/bluetooth/rfcomm/sock.c       2004-08-01 16:26:23.000000000 +0200
29856 @@ -0,0 +1,847 @@
29857 +/* 
29858 +   RFCOMM implementation for Linux Bluetooth stack (BlueZ).
29859 +   Copyright (C) 2002 Maxim Krasnyansky <maxk@qualcomm.com>
29860 +   Copyright (C) 2002 Marcel Holtmann <marcel@holtmann.org>
29861 +
29862 +   This program is free software; you can redistribute it and/or modify
29863 +   it under the terms of the GNU General Public License version 2 as
29864 +   published by the Free Software Foundation;
29865 +
29866 +   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
29867 +   OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
29868 +   FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS.
29869 +   IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) AND AUTHOR(S) BE LIABLE FOR ANY
29870 +   CLAIM, OR ANY SPECIAL INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES 
29871 +   WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 
29872 +   ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 
29873 +   OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
29874 +
29875 +   ALL LIABILITY, INCLUDING LIABILITY FOR INFRINGEMENT OF ANY PATENTS, 
29876 +   COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS, RELATING TO USE OF THIS 
29877 +   SOFTWARE IS DISCLAIMED.
29878 +*/
29879 +
29880 +/*
29881 + * RFCOMM sockets.
29882 + *
29883 + * $Id: sock.c,v 1.30 2002/10/18 20:12:12 maxk Exp $
29884 + */
29885 +
29886 +#include <linux/config.h>
29887 +#include <linux/module.h>
29888 +
29889 +#include <linux/types.h>
29890 +#include <linux/errno.h>
29891 +#include <linux/kernel.h>
29892 +#include <linux/major.h>
29893 +#include <linux/sched.h>
29894 +#include <linux/slab.h>
29895 +#include <linux/poll.h>
29896 +#include <linux/fcntl.h>
29897 +#include <linux/init.h>
29898 +#include <linux/skbuff.h>
29899 +#include <linux/interrupt.h>
29900 +#include <linux/socket.h>
29901 +#include <linux/skbuff.h>
29902 +#include <linux/list.h>
29903 +#include <net/sock.h>
29904 +
29905 +#include <asm/system.h>
29906 +#include <asm/uaccess.h>
29907 +
29908 +#include <net/bluetooth/bluetooth.h>
29909 +#include <net/bluetooth/rfcomm.h>
29910 +
29911 +#ifndef CONFIG_BLUEZ_RFCOMM_DEBUG
29912 +#undef  BT_DBG
29913 +#define BT_DBG(D...)
29914 +#endif
29915 +
29916 +static struct proto_ops rfcomm_sock_ops;
29917 +
29918 +static struct bluez_sock_list rfcomm_sk_list = {
29919 +       lock: RW_LOCK_UNLOCKED
29920 +};
29921 +
29922 +static void rfcomm_sock_close(struct sock *sk);
29923 +static void rfcomm_sock_kill(struct sock *sk);
29924 +
29925 +/* ---- DLC callbacks ----
29926 + *
29927 + * called under rfcomm_dlc_lock()
29928 + */
29929 +static void rfcomm_sk_data_ready(struct rfcomm_dlc *d, struct sk_buff *skb)
29930 +{
29931 +       struct sock *sk = d->owner;
29932 +       if (!sk)
29933 +               return;
29934 +
29935 +       atomic_add(skb->len, &sk->rmem_alloc);
29936 +       skb_queue_tail(&sk->receive_queue, skb);
29937 +       sk->data_ready(sk, skb->len);
29938 +
29939 +       if (atomic_read(&sk->rmem_alloc) >= sk->rcvbuf)
29940 +               rfcomm_dlc_throttle(d);
29941 +}
29942 +
29943 +static void rfcomm_sk_state_change(struct rfcomm_dlc *d, int err)
29944 +{
29945 +       struct sock *sk = d->owner, *parent;
29946 +       if (!sk)
29947 +               return;
29948 +
29949 +       BT_DBG("dlc %p state %ld err %d", d, d->state, err);
29950 +
29951 +       bh_lock_sock(sk);
29952 +
29953 +       if (err)
29954 +               sk->err = err;
29955 +       sk->state = d->state;
29956 +
29957 +       parent = bluez_pi(sk)->parent;
29958 +       if (!parent) {
29959 +               if (d->state == BT_CONNECTED)
29960 +                       rfcomm_session_getaddr(d->session, &bluez_pi(sk)->src, NULL);
29961 +               sk->state_change(sk);
29962 +       } else
29963 +               parent->data_ready(parent, 0);
29964 +
29965 +       bh_unlock_sock(sk);
29966 +}
29967 +
29968 +/* ---- Socket functions ---- */
29969 +static struct sock *__rfcomm_get_sock_by_addr(u8 channel, bdaddr_t *src)
29970 +{
29971 +       struct sock *sk;
29972 +
29973 +       for (sk = rfcomm_sk_list.head; sk; sk = sk->next) {
29974 +               if (rfcomm_pi(sk)->channel == channel && 
29975 +                               !bacmp(&bluez_pi(sk)->src, src))
29976 +                       break;
29977 +       }
29978 +
29979 +       return sk;
29980 +}
29981 +
29982 +/* Find socket with channel and source bdaddr.
29983 + * Returns closest match.
29984 + */
29985 +static struct sock *__rfcomm_get_sock_by_channel(int state, u8 channel, bdaddr_t *src)
29986 +{
29987 +       struct sock *sk, *sk1 = NULL;
29988 +
29989 +       for (sk = rfcomm_sk_list.head; sk; sk = sk->next) {
29990 +               if (state && sk->state != state)
29991 +                       continue;
29992 +
29993 +               if (rfcomm_pi(sk)->channel == channel) {
29994 +                       /* Exact match. */
29995 +                       if (!bacmp(&bluez_pi(sk)->src, src))
29996 +                               break;
29997 +
29998 +                       /* Closest match */
29999 +                       if (!bacmp(&bluez_pi(sk)->src, BDADDR_ANY))
30000 +                               sk1 = sk;
30001 +               }
30002 +       }
30003 +       return sk ? sk : sk1;
30004 +}
30005 +
30006 +/* Find socket with given address (channel, src).
30007 + * Returns locked socket */
30008 +static inline struct sock *rfcomm_get_sock_by_channel(int state, u8 channel, bdaddr_t *src)
30009 +{
30010 +       struct sock *s;
30011 +       read_lock(&rfcomm_sk_list.lock);
30012 +       s = __rfcomm_get_sock_by_channel(state, channel, src);
30013 +       if (s) bh_lock_sock(s);
30014 +       read_unlock(&rfcomm_sk_list.lock);
30015 +       return s;
30016 +}
30017 +
30018 +static void rfcomm_sock_destruct(struct sock *sk)
30019 +{
30020 +       struct rfcomm_dlc *d = rfcomm_pi(sk)->dlc;
30021 +
30022 +       BT_DBG("sk %p dlc %p", sk, d);
30023 +
30024 +       skb_queue_purge(&sk->receive_queue);
30025 +       skb_queue_purge(&sk->write_queue);
30026 +
30027 +       rfcomm_dlc_lock(d);
30028 +       rfcomm_pi(sk)->dlc = NULL;
30029 +       
30030 +       /* Detach DLC if it's owned by this socket */
30031 +       if (d->owner == sk)
30032 +               d->owner = NULL;
30033 +       rfcomm_dlc_unlock(d);
30034 +
30035 +       rfcomm_dlc_put(d);
30036 +
30037 +       MOD_DEC_USE_COUNT;
30038 +}
30039 +
30040 +static void rfcomm_sock_cleanup_listen(struct sock *parent)
30041 +{
30042 +       struct sock *sk;
30043 +
30044 +       BT_DBG("parent %p", parent);
30045 +
30046 +       /* Close not yet accepted dlcs */
30047 +       while ((sk = bluez_accept_dequeue(parent, NULL))) {
30048 +               rfcomm_sock_close(sk);
30049 +               rfcomm_sock_kill(sk);
30050 +       }
30051 +
30052 +       parent->state  = BT_CLOSED;
30053 +       parent->zapped = 1;
30054 +}
30055 +
30056 +/* Kill socket (only if zapped and orphan)
30057 + * Must be called on unlocked socket.
30058 + */
30059 +static void rfcomm_sock_kill(struct sock *sk)
30060 +{
30061 +       if (!sk->zapped || sk->socket)
30062 +               return;
30063 +
30064 +       BT_DBG("sk %p state %d refcnt %d", sk, sk->state, atomic_read(&sk->refcnt));
30065 +
30066 +       /* Kill poor orphan */
30067 +       bluez_sock_unlink(&rfcomm_sk_list, sk);
30068 +       sk->dead = 1;
30069 +       sock_put(sk);
30070 +}
30071 +
30072 +static void __rfcomm_sock_close(struct sock *sk)
30073 +{
30074 +       struct rfcomm_dlc *d = rfcomm_pi(sk)->dlc;
30075 +
30076 +       BT_DBG("sk %p state %d socket %p", sk, sk->state, sk->socket);
30077 +
30078 +       switch (sk->state) {
30079 +       case BT_LISTEN:
30080 +               rfcomm_sock_cleanup_listen(sk);
30081 +               break;
30082 +
30083 +       case BT_CONNECT:
30084 +       case BT_CONNECT2:
30085 +       case BT_CONFIG:
30086 +       case BT_CONNECTED:
30087 +               rfcomm_dlc_close(d, 0);
30088 +
30089 +       default:
30090 +               sk->zapped = 1;
30091 +               break;
30092 +       }
30093 +}
30094 +
30095 +/* Close socket.
30096 + * Must be called on unlocked socket.
30097 + */
30098 +static void rfcomm_sock_close(struct sock *sk)
30099 +{
30100 +       lock_sock(sk);
30101 +       __rfcomm_sock_close(sk);
30102 +       release_sock(sk);
30103 +}
30104 +
30105 +static void rfcomm_sock_init(struct sock *sk, struct sock *parent)
30106 +{
30107 +       BT_DBG("sk %p", sk);
30108 +
30109 +       if (parent) 
30110 +               sk->type = parent->type;
30111 +}
30112 +
30113 +static struct sock *rfcomm_sock_alloc(struct socket *sock, int proto, int prio)
30114 +{
30115 +       struct rfcomm_dlc *d;
30116 +       struct sock *sk;
30117 +
30118 +       sk = sk_alloc(PF_BLUETOOTH, prio, 1);
30119 +       if (!sk)
30120 +               return NULL;
30121 +
30122 +       d = rfcomm_dlc_alloc(prio);
30123 +       if (!d) {
30124 +               sk_free(sk);
30125 +               return NULL;
30126 +       }
30127 +       d->data_ready   = rfcomm_sk_data_ready;
30128 +       d->state_change = rfcomm_sk_state_change;
30129 +
30130 +       rfcomm_pi(sk)->dlc = d;
30131 +       d->owner = sk;
30132 +
30133 +       bluez_sock_init(sock, sk);
30134 +
30135 +       sk->zapped   = 0;
30136 +
30137 +       sk->destruct = rfcomm_sock_destruct;
30138 +       sk->sndtimeo = RFCOMM_CONN_TIMEOUT;
30139 +
30140 +       sk->sndbuf   = RFCOMM_MAX_CREDITS * RFCOMM_DEFAULT_MTU * 10;
30141 +       sk->rcvbuf   = RFCOMM_MAX_CREDITS * RFCOMM_DEFAULT_MTU * 10;
30142 +
30143 +       sk->protocol = proto;
30144 +       sk->state    = BT_OPEN;
30145 +
30146 +       bluez_sock_link(&rfcomm_sk_list, sk);
30147 +
30148 +       BT_DBG("sk %p", sk);
30149 +
30150 +       MOD_INC_USE_COUNT;
30151 +       return sk;
30152 +}
30153 +
30154 +static int rfcomm_sock_create(struct socket *sock, int protocol)
30155 +{
30156 +       struct sock *sk;
30157 +
30158 +       BT_DBG("sock %p", sock);
30159 +
30160 +       sock->state = SS_UNCONNECTED;
30161 +
30162 +       if (sock->type != SOCK_STREAM && sock->type != SOCK_RAW)
30163 +               return -ESOCKTNOSUPPORT;
30164 +
30165 +       sock->ops = &rfcomm_sock_ops;
30166 +
30167 +       if (!(sk = rfcomm_sock_alloc(sock, protocol, GFP_KERNEL)))
30168 +               return -ENOMEM;
30169 +
30170 +       rfcomm_sock_init(sk, NULL);
30171 +       return 0;
30172 +}
30173 +
30174 +static int rfcomm_sock_bind(struct socket *sock, struct sockaddr *addr, int addr_len)
30175 +{
30176 +       struct sockaddr_rc *sa = (struct sockaddr_rc *) addr;
30177 +       struct sock *sk = sock->sk;
30178 +       int err = 0;
30179 +
30180 +       BT_DBG("sk %p %s", sk, batostr(&sa->rc_bdaddr));
30181 +
30182 +       if (!addr || addr->sa_family != AF_BLUETOOTH)
30183 +               return -EINVAL;
30184 +
30185 +       lock_sock(sk);
30186 +
30187 +       if (sk->state != BT_OPEN) {
30188 +               err = -EBADFD;
30189 +               goto done;
30190 +       }
30191 +
30192 +       write_lock_bh(&rfcomm_sk_list.lock);
30193 +
30194 +       if (sa->rc_channel && __rfcomm_get_sock_by_addr(sa->rc_channel, &sa->rc_bdaddr)) {
30195 +               err = -EADDRINUSE;
30196 +       } else {
30197 +               /* Save source address */
30198 +               bacpy(&bluez_pi(sk)->src, &sa->rc_bdaddr);
30199 +               rfcomm_pi(sk)->channel = sa->rc_channel;
30200 +               sk->state = BT_BOUND;
30201 +       }
30202 +
30203 +       write_unlock_bh(&rfcomm_sk_list.lock);
30204 +
30205 +done:
30206 +       release_sock(sk);
30207 +       return err;
30208 +}
30209 +
30210 +static int rfcomm_sock_connect(struct socket *sock, struct sockaddr *addr, int alen, int flags)
30211 +{
30212 +       struct sockaddr_rc *sa = (struct sockaddr_rc *) addr;
30213 +       struct sock *sk = sock->sk;
30214 +       struct rfcomm_dlc *d = rfcomm_pi(sk)->dlc;
30215 +       int err = 0;
30216 +
30217 +       BT_DBG("sk %p", sk);
30218 +
30219 +       if (addr->sa_family != AF_BLUETOOTH || alen < sizeof(struct sockaddr_rc))
30220 +               return -EINVAL;
30221 +
30222 +       if (sk->state != BT_OPEN && sk->state != BT_BOUND)
30223 +               return -EBADFD;
30224 +
30225 +       if (sk->type != SOCK_STREAM)
30226 +               return -EINVAL;
30227 +
30228 +       lock_sock(sk);
30229 +
30230 +       sk->state = BT_CONNECT;
30231 +       bacpy(&bluez_pi(sk)->dst, &sa->rc_bdaddr);
30232 +       rfcomm_pi(sk)->channel = sa->rc_channel;
30233 +       
30234 +       err = rfcomm_dlc_open(d, &bluez_pi(sk)->src, &sa->rc_bdaddr, sa->rc_channel);
30235 +       if (!err)
30236 +               err = bluez_sock_wait_state(sk, BT_CONNECTED,
30237 +                               sock_sndtimeo(sk, flags & O_NONBLOCK));
30238 +
30239 +       release_sock(sk);
30240 +       return err;
30241 +}
30242 +
30243 +int rfcomm_sock_listen(struct socket *sock, int backlog)
30244 +{
30245 +       struct sock *sk = sock->sk;
30246 +       int err = 0;
30247 +
30248 +       BT_DBG("sk %p backlog %d", sk, backlog);
30249 +
30250 +       lock_sock(sk);
30251 +
30252 +       if (sk->state != BT_BOUND) {
30253 +               err = -EBADFD;
30254 +               goto done;
30255 +       }
30256 +
30257 +       sk->max_ack_backlog = backlog;
30258 +       sk->ack_backlog = 0;
30259 +       sk->state = BT_LISTEN;
30260 +
30261 +done:
30262 +       release_sock(sk);
30263 +       return err;
30264 +}
30265 +
30266 +int rfcomm_sock_accept(struct socket *sock, struct socket *newsock, int flags)
30267 +{
30268 +       DECLARE_WAITQUEUE(wait, current);
30269 +       struct sock *sk = sock->sk, *nsk;
30270 +       long timeo;
30271 +       int err = 0;
30272 +
30273 +       lock_sock(sk);
30274 +
30275 +       if (sk->state != BT_LISTEN) {
30276 +               err = -EBADFD;
30277 +               goto done;
30278 +       }
30279 +
30280 +       timeo = sock_rcvtimeo(sk, flags & O_NONBLOCK);
30281 +
30282 +       BT_DBG("sk %p timeo %ld", sk, timeo);
30283 +
30284 +       /* Wait for an incoming connection. (wake-one). */
30285 +       add_wait_queue_exclusive(sk->sleep, &wait);
30286 +       while (!(nsk = bluez_accept_dequeue(sk, newsock))) {
30287 +               set_current_state(TASK_INTERRUPTIBLE);
30288 +               if (!timeo) {
30289 +                       err = -EAGAIN;
30290 +                       break;
30291 +               }
30292 +
30293 +               release_sock(sk);
30294 +               timeo = schedule_timeout(timeo);
30295 +               lock_sock(sk);
30296 +
30297 +               if (sk->state != BT_LISTEN) {
30298 +                       err = -EBADFD;
30299 +                       break;
30300 +               }
30301 +
30302 +               if (signal_pending(current)) {
30303 +                       err = sock_intr_errno(timeo);
30304 +                       break;
30305 +               }
30306 +       }
30307 +       set_current_state(TASK_RUNNING);
30308 +       remove_wait_queue(sk->sleep, &wait);
30309 +
30310 +       if (err)
30311 +               goto done;
30312 +
30313 +       newsock->state = SS_CONNECTED;
30314 +
30315 +       BT_DBG("new socket %p", nsk);
30316 +
30317 +done:
30318 +       release_sock(sk);
30319 +       return err;
30320 +}
30321 +
30322 +static int rfcomm_sock_getname(struct socket *sock, struct sockaddr *addr, int *len, int peer)
30323 +{
30324 +       struct sockaddr_rc *sa = (struct sockaddr_rc *) addr;
30325 +       struct sock *sk = sock->sk;
30326 +
30327 +       BT_DBG("sock %p, sk %p", sock, sk);
30328 +
30329 +       sa->rc_family  = AF_BLUETOOTH;
30330 +       sa->rc_channel = rfcomm_pi(sk)->channel;
30331 +       if (peer)
30332 +               bacpy(&sa->rc_bdaddr, &bluez_pi(sk)->dst);
30333 +       else
30334 +               bacpy(&sa->rc_bdaddr, &bluez_pi(sk)->src);
30335 +
30336 +       *len = sizeof(struct sockaddr_rc);
30337 +       return 0;
30338 +}
30339 +
30340 +static int rfcomm_sock_sendmsg(struct socket *sock, struct msghdr *msg, int len,
30341 +                              struct scm_cookie *scm)
30342 +{
30343 +       struct sock *sk = sock->sk;
30344 +       struct rfcomm_dlc *d = rfcomm_pi(sk)->dlc;
30345 +       struct sk_buff *skb;
30346 +       int err, size;
30347 +       int sent = 0;
30348 +
30349 +       if (msg->msg_flags & MSG_OOB)
30350 +               return -EOPNOTSUPP;
30351 +
30352 +       if (sk->shutdown & SEND_SHUTDOWN)
30353 +               return -EPIPE;
30354 +
30355 +       BT_DBG("sock %p, sk %p", sock, sk);
30356 +
30357 +       lock_sock(sk);
30358 +
30359 +       while (len) {
30360 +               size = min_t(uint, len, d->mtu);
30361 +               
30362 +               skb = sock_alloc_send_skb(sk, size + RFCOMM_SKB_RESERVE,
30363 +                               msg->msg_flags & MSG_DONTWAIT, &err);
30364 +               if (!skb)
30365 +                       break;
30366 +               skb_reserve(skb, RFCOMM_SKB_HEAD_RESERVE);
30367 +
30368 +               err = memcpy_fromiovec(skb_put(skb, size), msg->msg_iov, size);
30369 +               if (err) {
30370 +                       kfree_skb(skb);
30371 +                       sent = err;
30372 +                       break;
30373 +               }
30374 +
30375 +               err = rfcomm_dlc_send(d, skb);
30376 +               if (err < 0) {
30377 +                       kfree_skb(skb);
30378 +                       break;
30379 +               }
30380 +
30381 +               sent += size;
30382 +               len  -= size;
30383 +       }
30384 +
30385 +       release_sock(sk);
30386 +
30387 +       return sent ? sent : err;
30388 +}
30389 +
30390 +static long rfcomm_sock_data_wait(struct sock *sk, long timeo)
30391 +{
30392 +       DECLARE_WAITQUEUE(wait, current);
30393 +
30394 +       add_wait_queue(sk->sleep, &wait);
30395 +       for (;;) {
30396 +               set_current_state(TASK_INTERRUPTIBLE);
30397 +
30398 +               if (skb_queue_len(&sk->receive_queue) || sk->err || (sk->shutdown & RCV_SHUTDOWN) ||
30399 +                               signal_pending(current) || !timeo)
30400 +                       break;
30401 +
30402 +               set_bit(SOCK_ASYNC_WAITDATA, &sk->socket->flags);
30403 +               release_sock(sk);
30404 +               timeo = schedule_timeout(timeo);
30405 +               lock_sock(sk);
30406 +               clear_bit(SOCK_ASYNC_WAITDATA, &sk->socket->flags);
30407 +       }
30408 +
30409 +       __set_current_state(TASK_RUNNING);
30410 +       remove_wait_queue(sk->sleep, &wait);
30411 +       return timeo;
30412 +}
30413 +
30414 +static int rfcomm_sock_recvmsg(struct socket *sock, struct msghdr *msg, int size,
30415 +                              int flags, struct scm_cookie *scm)
30416 +{
30417 +       struct sock *sk = sock->sk;
30418 +       int target, err = 0, copied = 0;
30419 +       long timeo;
30420 +
30421 +       if (flags & MSG_OOB)
30422 +               return -EOPNOTSUPP;
30423 +
30424 +       msg->msg_namelen = 0;
30425 +
30426 +       BT_DBG("sk %p size %d", sk, size);
30427 +
30428 +       lock_sock(sk);
30429 +
30430 +       target = sock_rcvlowat(sk, flags & MSG_WAITALL, size);
30431 +       timeo  = sock_rcvtimeo(sk, flags & MSG_DONTWAIT);
30432 +
30433 +       do {
30434 +               struct sk_buff *skb;
30435 +               int chunk;
30436 +
30437 +               skb = skb_dequeue(&sk->receive_queue);
30438 +               if (!skb) {
30439 +                       if (copied >= target)
30440 +                               break;
30441 +
30442 +                       if ((err = sock_error(sk)) != 0)
30443 +                               break;
30444 +                       if (sk->shutdown & RCV_SHUTDOWN)
30445 +                               break;
30446 +
30447 +                       err = -EAGAIN;
30448 +                       if (!timeo)
30449 +                               break;
30450 +
30451 +                       timeo = rfcomm_sock_data_wait(sk, timeo);
30452 +
30453 +                       if (signal_pending(current)) {
30454 +                               err = sock_intr_errno(timeo);
30455 +                               goto out;
30456 +                       }
30457 +                       continue;
30458 +               }
30459 +
30460 +               chunk = min_t(unsigned int, skb->len, size);
30461 +               if (memcpy_toiovec(msg->msg_iov, skb->data, chunk)) {
30462 +                       skb_queue_head(&sk->receive_queue, skb);
30463 +                       if (!copied)
30464 +                               copied = -EFAULT;
30465 +                       break;
30466 +               }
30467 +               copied += chunk;
30468 +               size   -= chunk;
30469 +
30470 +               if (!(flags & MSG_PEEK)) {
30471 +                       atomic_sub(chunk, &sk->rmem_alloc);
30472 +
30473 +                       skb_pull(skb, chunk);
30474 +                       if (skb->len) {
30475 +                               skb_queue_head(&sk->receive_queue, skb);
30476 +                               break;
30477 +                       }
30478 +                       kfree_skb(skb);
30479 +
30480 +               } else {
30481 +                       /* put message back and return */
30482 +                       skb_queue_head(&sk->receive_queue, skb);
30483 +                       break;
30484 +               }
30485 +       } while (size);
30486 +
30487 +out:
30488 +       if (atomic_read(&sk->rmem_alloc) <= (sk->rcvbuf >> 2))
30489 +               rfcomm_dlc_unthrottle(rfcomm_pi(sk)->dlc);
30490 +
30491 +       release_sock(sk);
30492 +       return copied ? : err;
30493 +}
30494 +
30495 +static int rfcomm_sock_setsockopt(struct socket *sock, int level, int optname, char *optval, int optlen)
30496 +{
30497 +       struct sock *sk = sock->sk;
30498 +       int err = 0;
30499 +
30500 +       BT_DBG("sk %p", sk);
30501 +
30502 +       lock_sock(sk);
30503 +
30504 +       switch (optname) {
30505 +       default:
30506 +               err = -ENOPROTOOPT;
30507 +               break;
30508 +       };
30509 +
30510 +       release_sock(sk);
30511 +       return err;
30512 +}
30513 +
30514 +static int rfcomm_sock_getsockopt(struct socket *sock, int level, int optname, char *optval, int *optlen)
30515 +{
30516 +       struct sock *sk = sock->sk;
30517 +       int len, err = 0; 
30518 +
30519 +       BT_DBG("sk %p", sk);
30520 +
30521 +       if (get_user(len, optlen))
30522 +               return -EFAULT;
30523 +
30524 +       lock_sock(sk);
30525 +
30526 +       switch (optname) {
30527 +       default:
30528 +               err = -ENOPROTOOPT;
30529 +               break;
30530 +       };
30531 +
30532 +       release_sock(sk);
30533 +       return err;
30534 +}
30535 +
30536 +static int rfcomm_sock_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
30537 +{
30538 +       struct sock *sk = sock->sk;
30539 +       int err;
30540 +
30541 +       lock_sock(sk);
30542 +
30543 +#ifdef CONFIG_BLUEZ_RFCOMM_TTY
30544 +       err = rfcomm_dev_ioctl(sk, cmd, arg);
30545 +#else
30546 +       err = -EOPNOTSUPP;
30547 +#endif
30548 +
30549 +       release_sock(sk);
30550 +
30551 +       return err;
30552 +}
30553 +
30554 +static int rfcomm_sock_shutdown(struct socket *sock, int how)
30555 +{
30556 +       struct sock *sk = sock->sk;
30557 +       int err = 0;
30558 +
30559 +       BT_DBG("sock %p, sk %p", sock, sk);
30560 +
30561 +       if (!sk) return 0;
30562 +
30563 +       lock_sock(sk);
30564 +       if (!sk->shutdown) {
30565 +               sk->shutdown = SHUTDOWN_MASK;
30566 +               __rfcomm_sock_close(sk);
30567 +
30568 +               if (sk->linger)
30569 +                       err = bluez_sock_wait_state(sk, BT_CLOSED, sk->lingertime);
30570 +       }
30571 +       release_sock(sk);
30572 +       return err;
30573 +}
30574 +
30575 +static int rfcomm_sock_release(struct socket *sock)
30576 +{
30577 +       struct sock *sk = sock->sk;
30578 +       int err = 0;
30579 +
30580 +       BT_DBG("sock %p, sk %p", sock, sk);
30581 +
30582 +       if (!sk)
30583 +               return 0;
30584 +
30585 +       err = rfcomm_sock_shutdown(sock, 2);
30586 +
30587 +       sock_orphan(sk);
30588 +       rfcomm_sock_kill(sk);
30589 +       return err;
30590 +}
30591 +
30592 +/* ---- RFCOMM core layer callbacks ---- 
30593 + *
30594 + * called under rfcomm_lock()
30595 + */
30596 +int rfcomm_connect_ind(struct rfcomm_session *s, u8 channel, struct rfcomm_dlc **d)
30597 +{
30598 +       struct sock *sk, *parent;
30599 +       bdaddr_t src, dst;
30600 +       int result = 0;
30601 +
30602 +       BT_DBG("session %p channel %d", s, channel);
30603 +
30604 +       rfcomm_session_getaddr(s, &src, &dst);
30605 +
30606 +       /* Check if we have socket listening on this channel */
30607 +       parent = rfcomm_get_sock_by_channel(BT_LISTEN, channel, &src);
30608 +       if (!parent)
30609 +               return 0;
30610 +
30611 +       /* Check for backlog size */
30612 +       if (parent->ack_backlog > parent->max_ack_backlog) {
30613 +               BT_DBG("backlog full %d", parent->ack_backlog); 
30614 +               goto done;
30615 +       }
30616 +
30617 +       sk = rfcomm_sock_alloc(NULL, BTPROTO_RFCOMM, GFP_ATOMIC);
30618 +       if (!sk)
30619 +               goto done;
30620 +
30621 +       rfcomm_sock_init(sk, parent);
30622 +       bacpy(&bluez_pi(sk)->src, &src);
30623 +       bacpy(&bluez_pi(sk)->dst, &dst);
30624 +       rfcomm_pi(sk)->channel = channel;
30625 +
30626 +       sk->state = BT_CONFIG;
30627 +       bluez_accept_enqueue(parent, sk);
30628 +
30629 +       /* Accept connection and return socket DLC */
30630 +       *d = rfcomm_pi(sk)->dlc;
30631 +       result = 1;
30632 +
30633 +done:
30634 +       bh_unlock_sock(parent);
30635 +       return result;
30636 +}
30637 +
30638 +/* ---- Proc fs support ---- */
30639 +int rfcomm_sock_dump(char *buf)
30640 +{
30641 +       struct bluez_sock_list *list = &rfcomm_sk_list;
30642 +       struct rfcomm_pinfo *pi;
30643 +       struct sock *sk;
30644 +       char *ptr = buf;
30645 +
30646 +       write_lock_bh(&list->lock);
30647 +
30648 +       for (sk = list->head; sk; sk = sk->next) {
30649 +               pi = rfcomm_pi(sk);
30650 +               ptr += sprintf(ptr, "sk  %s %s %d %d\n",
30651 +                               batostr(&bluez_pi(sk)->src), batostr(&bluez_pi(sk)->dst),
30652 +                               sk->state, rfcomm_pi(sk)->channel);
30653 +       }
30654 +
30655 +       write_unlock_bh(&list->lock);
30656 +
30657 +       return ptr - buf;
30658 +}
30659 +
30660 +static struct proto_ops rfcomm_sock_ops = {
30661 +       family:         PF_BLUETOOTH,
30662 +       release:        rfcomm_sock_release,
30663 +       bind:           rfcomm_sock_bind,
30664 +       connect:        rfcomm_sock_connect,
30665 +       listen:         rfcomm_sock_listen,
30666 +       accept:         rfcomm_sock_accept,
30667 +       getname:        rfcomm_sock_getname,
30668 +       sendmsg:        rfcomm_sock_sendmsg,
30669 +       recvmsg:        rfcomm_sock_recvmsg,
30670 +       shutdown:       rfcomm_sock_shutdown,
30671 +       setsockopt:     rfcomm_sock_setsockopt,
30672 +       getsockopt:     rfcomm_sock_getsockopt,
30673 +       ioctl:          rfcomm_sock_ioctl,
30674 +       poll:           bluez_sock_poll,
30675 +       socketpair:     sock_no_socketpair,
30676 +       mmap:           sock_no_mmap
30677 +};
30678 +
30679 +static struct net_proto_family rfcomm_sock_family_ops = {
30680 +       family:         PF_BLUETOOTH,
30681 +       create:         rfcomm_sock_create
30682 +};
30683 +
30684 +int rfcomm_init_sockets(void)
30685 +{
30686 +       int err;
30687 +
30688 +       if ((err = bluez_sock_register(BTPROTO_RFCOMM, &rfcomm_sock_family_ops))) {
30689 +               BT_ERR("Can't register RFCOMM socket layer");
30690 +               return err;
30691 +       }
30692 +
30693 +       return 0;
30694 +}
30695 +
30696 +void rfcomm_cleanup_sockets(void)
30697 +{
30698 +       int err;
30699 +
30700 +       /* Unregister socket, protocol and notifier */
30701 +       if ((err = bluez_sock_unregister(BTPROTO_RFCOMM)))
30702 +               BT_ERR("Can't unregister RFCOMM socket layer %d", err);
30703 +}
30704 diff -urN linux-2.4.18/net/bluetooth/rfcomm/tty.c linux-2.4.18-mh15/net/bluetooth/rfcomm/tty.c
30705 --- linux-2.4.18/net/bluetooth/rfcomm/tty.c     1970-01-01 01:00:00.000000000 +0100
30706 +++ linux-2.4.18-mh15/net/bluetooth/rfcomm/tty.c        2004-08-01 16:26:23.000000000 +0200
30707 @@ -0,0 +1,960 @@
30708 +/* 
30709 +   RFCOMM implementation for Linux Bluetooth stack (BlueZ).
30710 +   Copyright (C) 2002 Maxim Krasnyansky <maxk@qualcomm.com>
30711 +   Copyright (C) 2002 Marcel Holtmann <marcel@holtmann.org>
30712 +
30713 +   This program is free software; you can redistribute it and/or modify
30714 +   it under the terms of the GNU General Public License version 2 as
30715 +   published by the Free Software Foundation;
30716 +
30717 +   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
30718 +   OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
30719 +   FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS.
30720 +   IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) AND AUTHOR(S) BE LIABLE FOR ANY
30721 +   CLAIM, OR ANY SPECIAL INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES 
30722 +   WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 
30723 +   ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 
30724 +   OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
30725 +
30726 +   ALL LIABILITY, INCLUDING LIABILITY FOR INFRINGEMENT OF ANY PATENTS, 
30727 +   COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS, RELATING TO USE OF THIS 
30728 +   SOFTWARE IS DISCLAIMED.
30729 +*/
30730 +
30731 +/*
30732 + * RFCOMM TTY.
30733 + *
30734 + * $Id: tty.c,v 1.26 2002/10/18 20:12:12 maxk Exp $
30735 + */
30736 +
30737 +#include <linux/config.h>
30738 +#include <linux/module.h>
30739 +
30740 +#include <linux/tty.h>
30741 +#include <linux/tty_driver.h>
30742 +#include <linux/tty_flip.h>
30743 +
30744 +#include <linux/slab.h>
30745 +#include <linux/skbuff.h>
30746 +
30747 +#include <net/bluetooth/bluetooth.h>
30748 +#include <net/bluetooth/rfcomm.h>
30749 +
30750 +#ifndef CONFIG_BLUEZ_RFCOMM_DEBUG
30751 +#undef  BT_DBG
30752 +#define BT_DBG(D...)
30753 +#endif
30754 +
30755 +#define RFCOMM_TTY_MAGIC 0x6d02                /* magic number for rfcomm struct */
30756 +#define RFCOMM_TTY_PORTS RFCOMM_MAX_DEV        /* whole lotta rfcomm devices */
30757 +#define RFCOMM_TTY_MAJOR 216           /* device node major id of the usb/bluetooth.c driver */
30758 +#define RFCOMM_TTY_MINOR 0
30759 +
30760 +struct rfcomm_dev {
30761 +       struct list_head        list;
30762 +       atomic_t                refcnt;
30763 +
30764 +       char                    name[12];
30765 +       int                     id;
30766 +       unsigned long           flags;
30767 +       int                     opened;
30768 +       int                     err;
30769 +
30770 +       bdaddr_t                src;
30771 +       bdaddr_t                dst;
30772 +       u8                      channel;
30773 +
30774 +       uint                    modem_status;
30775 +
30776 +       struct rfcomm_dlc       *dlc;
30777 +       struct tty_struct       *tty;
30778 +       wait_queue_head_t       wait;
30779 +       struct tasklet_struct   wakeup_task;
30780 +
30781 +       atomic_t                wmem_alloc;
30782 +};
30783 +
30784 +static LIST_HEAD(rfcomm_dev_list);
30785 +static rwlock_t rfcomm_dev_lock = RW_LOCK_UNLOCKED;
30786 +
30787 +static void rfcomm_dev_data_ready(struct rfcomm_dlc *dlc, struct sk_buff *skb);
30788 +static void rfcomm_dev_state_change(struct rfcomm_dlc *dlc, int err);
30789 +static void rfcomm_dev_modem_status(struct rfcomm_dlc *dlc, u8 v24_sig);
30790 +
30791 +static void rfcomm_tty_wakeup(unsigned long arg);
30792 +
30793 +/* ---- Device functions ---- */
30794 +static void rfcomm_dev_destruct(struct rfcomm_dev *dev)
30795 +{
30796 +       struct rfcomm_dlc *dlc = dev->dlc;
30797 +
30798 +       BT_DBG("dev %p dlc %p", dev, dlc);
30799 +
30800 +       rfcomm_dlc_lock(dlc);
30801 +       /* Detach DLC if it's owned by this dev */
30802 +       if (dlc->owner == dev)
30803 +               dlc->owner = NULL;
30804 +       rfcomm_dlc_unlock(dlc);
30805 +
30806 +       rfcomm_dlc_put(dlc);
30807 +       kfree(dev);
30808 +
30809 +       MOD_DEC_USE_COUNT;
30810 +}
30811 +
30812 +static inline void rfcomm_dev_hold(struct rfcomm_dev *dev)
30813 +{
30814 +       atomic_inc(&dev->refcnt);
30815 +}
30816 +
30817 +static inline void rfcomm_dev_put(struct rfcomm_dev *dev)
30818 +{
30819 +       /* The reason this isn't actually a race, as you no
30820 +          doubt have a little voice screaming at you in your
30821 +          head, is that the refcount should never actually
30822 +          reach zero unless the device has already been taken
30823 +          off the list, in rfcomm_dev_del(). And if that's not
30824 +          true, we'll hit the BUG() in rfcomm_dev_destruct()
30825 +          anyway. */
30826 +       if (atomic_dec_and_test(&dev->refcnt))
30827 +               rfcomm_dev_destruct(dev);
30828 +}
30829 +
30830 +static struct rfcomm_dev *__rfcomm_dev_get(int id)
30831 +{
30832 +       struct rfcomm_dev *dev;
30833 +       struct list_head  *p;
30834 +
30835 +       list_for_each(p, &rfcomm_dev_list) {
30836 +               dev = list_entry(p, struct rfcomm_dev, list);
30837 +               if (dev->id == id)
30838 +                       return dev;
30839 +       }
30840 +
30841 +       return NULL;
30842 +}
30843 +
30844 +static inline struct rfcomm_dev *rfcomm_dev_get(int id)
30845 +{
30846 +       struct rfcomm_dev *dev;
30847 +
30848 +       read_lock(&rfcomm_dev_lock);
30849 +
30850 +       dev = __rfcomm_dev_get(id);
30851 +       if (dev)
30852 +               rfcomm_dev_hold(dev);
30853 +
30854 +       read_unlock(&rfcomm_dev_lock);
30855 +
30856 +       return dev;
30857 +}
30858 +
30859 +static int rfcomm_dev_add(struct rfcomm_dev_req *req, struct rfcomm_dlc *dlc)
30860 +{
30861 +       struct rfcomm_dev *dev;
30862 +       struct list_head *head = &rfcomm_dev_list, *p;
30863 +       int err = 0;
30864 +
30865 +       BT_DBG("id %d channel %d", req->dev_id, req->channel);
30866 +       
30867 +       dev = kmalloc(sizeof(struct rfcomm_dev), GFP_KERNEL);
30868 +       if (!dev)
30869 +               return -ENOMEM;
30870 +       memset(dev, 0, sizeof(struct rfcomm_dev));
30871 +
30872 +       write_lock_bh(&rfcomm_dev_lock);
30873 +
30874 +       if (req->dev_id < 0) {
30875 +               dev->id = 0;
30876 +
30877 +               list_for_each(p, &rfcomm_dev_list) {
30878 +                       if (list_entry(p, struct rfcomm_dev, list)->id != dev->id)
30879 +                               break;
30880 +
30881 +                       dev->id++;
30882 +                       head = p;
30883 +               }
30884 +       } else {
30885 +               dev->id = req->dev_id;
30886 +
30887 +               list_for_each(p, &rfcomm_dev_list) {
30888 +                       struct rfcomm_dev *entry = list_entry(p, struct rfcomm_dev, list);
30889 +
30890 +                       if (entry->id == dev->id) {
30891 +                               err = -EADDRINUSE;
30892 +                               goto out;
30893 +                       }
30894 +
30895 +                       if (entry->id > dev->id - 1)
30896 +                               break;
30897 +
30898 +                       head = p;
30899 +               }
30900 +       }
30901 +
30902 +       if ((dev->id < 0) || (dev->id > RFCOMM_MAX_DEV - 1)) {
30903 +               err = -ENFILE;
30904 +               goto out;
30905 +       }
30906 +
30907 +       sprintf(dev->name, "rfcomm%d", dev->id);
30908 +
30909 +       list_add(&dev->list, head);
30910 +       atomic_set(&dev->refcnt, 1);
30911 +
30912 +       bacpy(&dev->src, &req->src);
30913 +       bacpy(&dev->dst, &req->dst);
30914 +       dev->channel = req->channel;
30915 +
30916 +       dev->flags = req->flags & 
30917 +               ((1 << RFCOMM_RELEASE_ONHUP) | (1 << RFCOMM_REUSE_DLC));
30918 +
30919 +       init_waitqueue_head(&dev->wait);
30920 +       tasklet_init(&dev->wakeup_task, rfcomm_tty_wakeup, (unsigned long) dev);
30921 +
30922 +       rfcomm_dlc_lock(dlc);
30923 +       dlc->data_ready   = rfcomm_dev_data_ready;
30924 +       dlc->state_change = rfcomm_dev_state_change;
30925 +       dlc->modem_status = rfcomm_dev_modem_status;
30926 +
30927 +       dlc->owner = dev;
30928 +       dev->dlc   = dlc;
30929 +       rfcomm_dlc_unlock(dlc);
30930 +
30931 +       MOD_INC_USE_COUNT;
30932 +       
30933 +out:
30934 +       write_unlock_bh(&rfcomm_dev_lock);
30935 +
30936 +       if (err) {
30937 +               kfree(dev);
30938 +               return err;
30939 +       } else
30940 +               return dev->id;
30941 +}
30942 +
30943 +static void rfcomm_dev_del(struct rfcomm_dev *dev)
30944 +{
30945 +       BT_DBG("dev %p", dev);
30946 +
30947 +       write_lock_bh(&rfcomm_dev_lock);
30948 +       list_del_init(&dev->list);
30949 +       write_unlock_bh(&rfcomm_dev_lock);
30950 +
30951 +       rfcomm_dev_put(dev);
30952 +}
30953 +
30954 +/* ---- Send buffer ---- */
30955 +
30956 +static inline unsigned int rfcomm_room(struct rfcomm_dlc *dlc)
30957 +{
30958 +       /* We can't let it be zero, because we don't get a callback 
30959 +          when tx_credits becomes nonzero, hence we'd never wake up */
30960 +       return dlc->mtu * (dlc->tx_credits?:1);
30961 +}
30962 +
30963 +static void rfcomm_wfree(struct sk_buff *skb)
30964 +{
30965 +       struct rfcomm_dev *dev = (void *) skb->sk;
30966 +       atomic_sub(skb->truesize, &dev->wmem_alloc);
30967 +       if (test_bit(RFCOMM_TTY_ATTACHED, &dev->flags))
30968 +               tasklet_schedule(&dev->wakeup_task);
30969 +       rfcomm_dev_put(dev);
30970 +}
30971 +
30972 +static inline void rfcomm_set_owner_w(struct sk_buff *skb, struct rfcomm_dev *dev)
30973 +{
30974 +       rfcomm_dev_hold(dev);
30975 +       atomic_add(skb->truesize, &dev->wmem_alloc);
30976 +       skb->sk = (void *) dev;
30977 +       skb->destructor = rfcomm_wfree;
30978 +}
30979 +
30980 +static struct sk_buff *rfcomm_wmalloc(struct rfcomm_dev *dev, unsigned long size, int force, int priority)
30981 +{
30982 +       if (force || atomic_read(&dev->wmem_alloc) < rfcomm_room(dev->dlc)) {
30983 +               struct sk_buff *skb = alloc_skb(size, priority);
30984 +               if (skb) {
30985 +                       rfcomm_set_owner_w(skb, dev);
30986 +                       return skb;
30987 +               }
30988 +       }
30989 +       return NULL;
30990 +}
30991 +
30992 +/* ---- Device IOCTLs ---- */
30993 +
30994 +#define NOCAP_FLAGS ((1 << RFCOMM_REUSE_DLC) | (1 << RFCOMM_RELEASE_ONHUP))
30995 +
30996 +static int rfcomm_create_dev(struct sock *sk, unsigned long arg)
30997 +{
30998 +       struct rfcomm_dev_req req;
30999 +       struct rfcomm_dlc *dlc;
31000 +       int id;
31001 +
31002 +       if (copy_from_user(&req, (void *) arg, sizeof(req)))
31003 +               return -EFAULT;
31004 +
31005 +       BT_DBG("sk %p dev_id %id flags 0x%x", sk, req.dev_id, req.flags);
31006 +
31007 +       if (req.flags != NOCAP_FLAGS && !capable(CAP_NET_ADMIN))
31008 +               return -EPERM;
31009 +
31010 +       if (req.flags & (1 << RFCOMM_REUSE_DLC)) {
31011 +               /* Socket must be connected */
31012 +               if (sk->state != BT_CONNECTED)
31013 +                       return -EBADFD;
31014 +
31015 +               dlc = rfcomm_pi(sk)->dlc;
31016 +               rfcomm_dlc_hold(dlc);
31017 +       } else {
31018 +               dlc = rfcomm_dlc_alloc(GFP_KERNEL);
31019 +               if (!dlc)
31020 +                       return -ENOMEM;
31021 +       }
31022 +
31023 +       id = rfcomm_dev_add(&req, dlc);
31024 +       if (id < 0) {
31025 +               rfcomm_dlc_put(dlc);
31026 +               return id;
31027 +       }
31028 +
31029 +       if (req.flags & (1 << RFCOMM_REUSE_DLC)) {
31030 +               /* DLC is now used by device.
31031 +                * Socket must be disconnected */
31032 +               sk->state = BT_CLOSED;
31033 +       }
31034 +
31035 +       return id;
31036 +}
31037 +
31038 +static int rfcomm_release_dev(unsigned long arg)
31039 +{
31040 +       struct rfcomm_dev_req req;
31041 +       struct rfcomm_dev *dev;
31042 +
31043 +       if (copy_from_user(&req, (void *) arg, sizeof(req)))
31044 +               return -EFAULT;
31045 +
31046 +       BT_DBG("dev_id %id flags 0x%x", req.dev_id, req.flags);
31047 +
31048 +       if (!(dev = rfcomm_dev_get(req.dev_id)))
31049 +               return -ENODEV;
31050 +
31051 +       if (dev->flags != NOCAP_FLAGS && !capable(CAP_NET_ADMIN)) {
31052 +               rfcomm_dev_put(dev);
31053 +               return -EPERM;
31054 +       }
31055 +
31056 +       if (req.flags & (1 << RFCOMM_HANGUP_NOW))
31057 +               rfcomm_dlc_close(dev->dlc, 0);
31058 +
31059 +       rfcomm_dev_del(dev);
31060 +       rfcomm_dev_put(dev);
31061 +       return 0;
31062 +}
31063 +
31064 +static int rfcomm_get_dev_list(unsigned long arg)
31065 +{
31066 +       struct rfcomm_dev_list_req *dl;
31067 +       struct rfcomm_dev_info *di;
31068 +       struct list_head *p;
31069 +       int n = 0, size, err;
31070 +       u16 dev_num;
31071 +
31072 +       BT_DBG("");
31073 +
31074 +       if (get_user(dev_num, (u16 *) arg))
31075 +               return -EFAULT;
31076 +
31077 +       if (!dev_num || dev_num > (PAGE_SIZE * 4) / sizeof(*di))
31078 +               return -EINVAL;
31079 +
31080 +       size = sizeof(*dl) + dev_num * sizeof(*di);
31081 +
31082 +       if (!(dl = kmalloc(size, GFP_KERNEL)))
31083 +               return -ENOMEM;
31084 +
31085 +       di = dl->dev_info;
31086 +
31087 +       read_lock_bh(&rfcomm_dev_lock);
31088 +
31089 +       list_for_each(p, &rfcomm_dev_list) {
31090 +               struct rfcomm_dev *dev = list_entry(p, struct rfcomm_dev, list);
31091 +               (di + n)->id      = dev->id;
31092 +               (di + n)->flags   = dev->flags;
31093 +               (di + n)->state   = dev->dlc->state;
31094 +               (di + n)->channel = dev->channel;
31095 +               bacpy(&(di + n)->src, &dev->src);
31096 +               bacpy(&(di + n)->dst, &dev->dst);
31097 +               if (++n >= dev_num)
31098 +                       break;
31099 +       }
31100 +
31101 +       read_unlock_bh(&rfcomm_dev_lock);
31102 +
31103 +       dl->dev_num = n;
31104 +       size = sizeof(*dl) + n * sizeof(*di);
31105 +
31106 +       err = copy_to_user((void *) arg, dl, size);
31107 +       kfree(dl);
31108 +
31109 +       return err ? -EFAULT : 0;
31110 +}
31111 +
31112 +static int rfcomm_get_dev_info(unsigned long arg)
31113 +{
31114 +       struct rfcomm_dev *dev;
31115 +       struct rfcomm_dev_info di;
31116 +       int err = 0;
31117 +
31118 +       BT_DBG("");
31119 +
31120 +       if (copy_from_user(&di, (void *)arg, sizeof(di)))
31121 +               return -EFAULT;
31122 +
31123 +       if (!(dev = rfcomm_dev_get(di.id)))
31124 +               return -ENODEV;
31125 +
31126 +       di.flags   = dev->flags;
31127 +       di.channel = dev->channel;
31128 +       di.state   = dev->dlc->state;
31129 +       bacpy(&di.src, &dev->src);
31130 +       bacpy(&di.dst, &dev->dst);
31131 +
31132 +       if (copy_to_user((void *)arg, &di, sizeof(di)))
31133 +               err = -EFAULT;
31134 +
31135 +       rfcomm_dev_put(dev);
31136 +       return err;
31137 +}
31138 +
31139 +int rfcomm_dev_ioctl(struct sock *sk, unsigned int cmd, unsigned long arg)
31140 +{
31141 +       BT_DBG("cmd %d arg %ld", cmd, arg);
31142 +
31143 +       switch (cmd) {
31144 +       case RFCOMMCREATEDEV:
31145 +               return rfcomm_create_dev(sk, arg);
31146 +
31147 +       case RFCOMMRELEASEDEV:
31148 +               return rfcomm_release_dev(arg);
31149 +
31150 +       case RFCOMMGETDEVLIST:
31151 +               return rfcomm_get_dev_list(arg);
31152 +
31153 +       case RFCOMMGETDEVINFO:
31154 +               return rfcomm_get_dev_info(arg);
31155 +       }
31156 +
31157 +       return -EINVAL;
31158 +}
31159 +
31160 +/* ---- DLC callbacks ---- */
31161 +static void rfcomm_dev_data_ready(struct rfcomm_dlc *dlc, struct sk_buff *skb)
31162 +{
31163 +       struct rfcomm_dev *dev = dlc->owner;
31164 +       struct tty_struct *tty;
31165 +       
31166 +       if (!dev || !(tty = dev->tty)) {
31167 +               kfree_skb(skb);
31168 +               return;
31169 +       }
31170 +
31171 +       BT_DBG("dlc %p tty %p len %d", dlc, tty, skb->len);
31172 +
31173 +       if (test_bit(TTY_DONT_FLIP, &tty->flags)) {
31174 +               register int i;
31175 +               for (i = 0; i < skb->len; i++) {
31176 +                       if (tty->flip.count >= TTY_FLIPBUF_SIZE)
31177 +                               tty_flip_buffer_push(tty);
31178 +
31179 +                       tty_insert_flip_char(tty, skb->data[i], 0);
31180 +               }
31181 +               tty_flip_buffer_push(tty);
31182 +       } else
31183 +               tty->ldisc.receive_buf(tty, skb->data, NULL, skb->len);
31184 +
31185 +       kfree_skb(skb);
31186 +}
31187 +
31188 +static void rfcomm_dev_state_change(struct rfcomm_dlc *dlc, int err)
31189 +{
31190 +       struct rfcomm_dev *dev = dlc->owner;
31191 +       if (!dev)
31192 +               return;
31193 +       
31194 +       BT_DBG("dlc %p dev %p err %d", dlc, dev, err);
31195 +
31196 +       dev->err = err;
31197 +       wake_up_interruptible(&dev->wait);
31198 +
31199 +       if (dlc->state == BT_CLOSED) {
31200 +               if (!dev->tty) {
31201 +                       if (test_bit(RFCOMM_RELEASE_ONHUP, &dev->flags)) {
31202 +                               rfcomm_dev_hold(dev);
31203 +                               rfcomm_dev_del(dev);
31204 +
31205 +                               /* We have to drop DLC lock here, otherwise
31206 +                                  rfcomm_dev_put() will dead lock if it's
31207 +                                  the last reference. */
31208 +                               rfcomm_dlc_unlock(dlc);
31209 +                               rfcomm_dev_put(dev);
31210 +                               rfcomm_dlc_lock(dlc);
31211 +                       }
31212 +               } else 
31213 +                       tty_hangup(dev->tty);
31214 +       }
31215 +}
31216 +
31217 +static void rfcomm_dev_modem_status(struct rfcomm_dlc *dlc, u8 v24_sig)
31218 +{
31219 +       struct rfcomm_dev *dev = dlc->owner;
31220 +       if (!dev)
31221 +               return;
31222 +       
31223 +       BT_DBG("dlc %p dev %p v24_sig 0x%02x", dlc, dev, v24_sig);
31224 +
31225 +       dev->modem_status = 
31226 +               ((v24_sig & RFCOMM_V24_RTC) ? (TIOCM_DSR | TIOCM_DTR) : 0) |
31227 +               ((v24_sig & RFCOMM_V24_RTR) ? (TIOCM_RTS | TIOCM_CTS) : 0) |
31228 +               ((v24_sig & RFCOMM_V24_IC)  ? TIOCM_RI : 0) |
31229 +               ((v24_sig & RFCOMM_V24_DV)  ? TIOCM_CD : 0);
31230 +}
31231 +
31232 +/* ---- TTY functions ---- */
31233 +static void rfcomm_tty_wakeup(unsigned long arg)
31234 +{
31235 +       struct rfcomm_dev *dev = (void *) arg;
31236 +       struct tty_struct *tty = dev->tty;
31237 +       if (!tty)
31238 +               return;
31239 +
31240 +       BT_DBG("dev %p tty %p", dev, tty);
31241 +
31242 +       if (test_bit(TTY_DO_WRITE_WAKEUP, &tty->flags) && tty->ldisc.write_wakeup)
31243 +                (tty->ldisc.write_wakeup)(tty);
31244 +
31245 +       wake_up_interruptible(&tty->write_wait);
31246 +#ifdef SERIAL_HAVE_POLL_WAIT
31247 +       wake_up_interruptible(&tty->poll_wait);
31248 +#endif
31249 +}
31250 +
31251 +static int rfcomm_tty_open(struct tty_struct *tty, struct file *filp)
31252 +{
31253 +       DECLARE_WAITQUEUE(wait, current);
31254 +       struct rfcomm_dev *dev;
31255 +       struct rfcomm_dlc *dlc;
31256 +       int err, id;
31257 +
31258 +       id = MINOR(tty->device) - tty->driver.minor_start;
31259 +
31260 +       BT_DBG("tty %p id %d", tty, id);
31261 +
31262 +       /* We don't leak this refcount. For reasons which are not entirely
31263 +          clear, the TTY layer will call our ->close() method even if the
31264 +          open fails. We decrease the refcount there, and decreasing it
31265 +          here too would cause breakage. */
31266 +       dev = rfcomm_dev_get(id);
31267 +       if (!dev)
31268 +               return -ENODEV;
31269 +
31270 +       BT_DBG("dev %p dst %s channel %d opened %d", dev, batostr(&dev->dst), dev->channel, dev->opened);
31271 +
31272 +       if (dev->opened++ != 0)
31273 +               return 0;
31274 +
31275 +       dlc = dev->dlc;
31276 +
31277 +       /* Attach TTY and open DLC */
31278 +
31279 +       rfcomm_dlc_lock(dlc);
31280 +       tty->driver_data = dev;
31281 +       dev->tty = tty;
31282 +       rfcomm_dlc_unlock(dlc);
31283 +       set_bit(RFCOMM_TTY_ATTACHED, &dev->flags);
31284 +
31285 +       err = rfcomm_dlc_open(dlc, &dev->src, &dev->dst, dev->channel);
31286 +       if (err < 0)
31287 +               return err;
31288 +
31289 +       /* Wait for DLC to connect */
31290 +       add_wait_queue(&dev->wait, &wait);
31291 +       while (1) {
31292 +               set_current_state(TASK_INTERRUPTIBLE);
31293 +
31294 +               if (dlc->state == BT_CLOSED) {
31295 +                       err = -dev->err;
31296 +                       break;
31297 +               }
31298 +
31299 +               if (dlc->state == BT_CONNECTED)
31300 +                       break;
31301 +
31302 +               if (signal_pending(current)) {
31303 +                       err = -EINTR;
31304 +                       break;
31305 +               }
31306 +
31307 +               schedule();
31308 +       }
31309 +       set_current_state(TASK_RUNNING);
31310 +       remove_wait_queue(&dev->wait, &wait);
31311 +
31312 +       return err;
31313 +}
31314 +
31315 +static void rfcomm_tty_close(struct tty_struct *tty, struct file *filp)
31316 +{
31317 +       struct rfcomm_dev *dev = (struct rfcomm_dev *) tty->driver_data;
31318 +       if (!dev)
31319 +               return;
31320 +
31321 +       BT_DBG("tty %p dev %p dlc %p opened %d", tty, dev, dev->dlc, dev->opened);
31322 +
31323 +       if (--dev->opened == 0) {
31324 +               /* Close DLC and dettach TTY */
31325 +               rfcomm_dlc_close(dev->dlc, 0);
31326 +
31327 +               clear_bit(RFCOMM_TTY_ATTACHED, &dev->flags);
31328 +               tasklet_kill(&dev->wakeup_task);
31329 +
31330 +               rfcomm_dlc_lock(dev->dlc);
31331 +               tty->driver_data = NULL;
31332 +               dev->tty = NULL;
31333 +               rfcomm_dlc_unlock(dev->dlc);
31334 +       }
31335 +
31336 +       rfcomm_dev_put(dev);
31337 +}
31338 +
31339 +static int rfcomm_tty_write(struct tty_struct *tty, int from_user, const unsigned char *buf, int count)
31340 +{
31341 +       struct rfcomm_dev *dev = (struct rfcomm_dev *) tty->driver_data;
31342 +       struct rfcomm_dlc *dlc = dev->dlc;
31343 +       struct sk_buff *skb;
31344 +       int err = 0, sent = 0, size;
31345 +
31346 +       BT_DBG("tty %p from_user %d count %d", tty, from_user, count);
31347 +
31348 +       while (count) {
31349 +               size = min_t(uint, count, dlc->mtu);
31350 +
31351 +               if (from_user)
31352 +                       skb = rfcomm_wmalloc(dev, size + RFCOMM_SKB_RESERVE, 0, GFP_KERNEL);
31353 +               else
31354 +                       skb = rfcomm_wmalloc(dev, size + RFCOMM_SKB_RESERVE, 0, GFP_ATOMIC);
31355 +               
31356 +               if (!skb)
31357 +                       break;
31358 +
31359 +               skb_reserve(skb, RFCOMM_SKB_HEAD_RESERVE);
31360 +
31361 +               if (from_user)
31362 +                       copy_from_user(skb_put(skb, size), buf + sent, size);
31363 +               else
31364 +                       memcpy(skb_put(skb, size), buf + sent, size);
31365 +
31366 +               if ((err = rfcomm_dlc_send(dlc, skb)) < 0) {
31367 +                       kfree_skb(skb);
31368 +                       break;
31369 +               }
31370 +
31371 +               sent  += size;
31372 +               count -= size;
31373 +       }
31374 +
31375 +       return sent ? sent : err;
31376 +}
31377 +
31378 +static void rfcomm_tty_put_char(struct tty_struct *tty, unsigned char ch)
31379 +{
31380 +       struct rfcomm_dev *dev = (struct rfcomm_dev *) tty->driver_data;
31381 +       struct rfcomm_dlc *dlc = dev->dlc;
31382 +       struct sk_buff *skb;
31383 +
31384 +       BT_DBG("tty %p char %x", tty, ch);
31385 +
31386 +       skb = rfcomm_wmalloc(dev, 1 + RFCOMM_SKB_RESERVE, 1, GFP_ATOMIC);
31387 +
31388 +       if (!skb)
31389 +               return;
31390 +
31391 +       skb_reserve(skb, RFCOMM_SKB_HEAD_RESERVE);
31392 +
31393 +       *(char *)skb_put(skb, 1) = ch;
31394 +
31395 +       if ((rfcomm_dlc_send(dlc, skb)) < 0)
31396 +               kfree_skb(skb); 
31397 +}
31398 +
31399 +static int rfcomm_tty_write_room(struct tty_struct *tty)
31400 +{
31401 +       struct rfcomm_dev *dev = (struct rfcomm_dev *) tty->driver_data;
31402 +       int room;
31403 +       
31404 +       BT_DBG("tty %p", tty);
31405 +
31406 +       room = rfcomm_room(dev->dlc) - atomic_read(&dev->wmem_alloc);
31407 +       if (room < 0)
31408 +               room = 0;
31409 +
31410 +       return room;
31411 +}
31412 +
31413 +static int rfcomm_tty_set_modem_status(uint cmd, struct rfcomm_dlc *dlc, uint status)
31414 +{
31415 +       u8 v24_sig, mask;
31416 +
31417 +       BT_DBG("dlc %p cmd 0x%02x", dlc, cmd);
31418 +
31419 +       if (cmd == TIOCMSET)
31420 +               v24_sig = 0;
31421 +       else
31422 +               rfcomm_dlc_get_modem_status(dlc, &v24_sig);
31423 +
31424 +       mask =  ((status & TIOCM_DSR) ? RFCOMM_V24_RTC : 0) |
31425 +               ((status & TIOCM_DTR) ? RFCOMM_V24_RTC : 0) |
31426 +               ((status & TIOCM_RTS) ? RFCOMM_V24_RTR : 0) |
31427 +               ((status & TIOCM_CTS) ? RFCOMM_V24_RTR : 0) |
31428 +               ((status & TIOCM_RI)  ? RFCOMM_V24_IC  : 0) |
31429 +               ((status & TIOCM_CD)  ? RFCOMM_V24_DV  : 0);
31430 +
31431 +       if (cmd == TIOCMBIC)
31432 +               v24_sig &= ~mask;
31433 +       else
31434 +               v24_sig |= mask;
31435 +
31436 +       rfcomm_dlc_set_modem_status(dlc, v24_sig);
31437 +       return 0;
31438 +}
31439 +
31440 +static int rfcomm_tty_ioctl(struct tty_struct *tty, struct file *filp, unsigned int cmd, unsigned long arg)
31441 +{
31442 +       struct rfcomm_dev *dev = (struct rfcomm_dev *) tty->driver_data;
31443 +       struct rfcomm_dlc *dlc = dev->dlc;
31444 +       uint status;
31445 +       int err;
31446 +
31447 +       BT_DBG("tty %p cmd 0x%02x", tty, cmd);
31448 +
31449 +       switch (cmd) {
31450 +       case TCGETS:
31451 +               BT_DBG("TCGETS is not supported");
31452 +               return -ENOIOCTLCMD;
31453 +
31454 +       case TCSETS:
31455 +               BT_DBG("TCSETS is not supported");
31456 +               return -ENOIOCTLCMD;
31457 +
31458 +       case TIOCMGET:
31459 +               BT_DBG("TIOCMGET");
31460 +
31461 +               return put_user(dev->modem_status, (unsigned int *)arg);
31462 +
31463 +       case TIOCMSET: /* Turns on and off the lines as specified by the mask */
31464 +       case TIOCMBIS: /* Turns on the lines as specified by the mask */
31465 +       case TIOCMBIC: /* Turns off the lines as specified by the mask */
31466 +               if ((err = get_user(status, (unsigned int *)arg)))
31467 +                       return err;
31468 +               return rfcomm_tty_set_modem_status(cmd, dlc, status);
31469 +
31470 +       case TIOCMIWAIT:
31471 +               BT_DBG("TIOCMIWAIT");
31472 +               break;
31473 +
31474 +       case TIOCGICOUNT:
31475 +               BT_DBG("TIOCGICOUNT");
31476 +               break;
31477 +
31478 +       case TIOCGSERIAL:
31479 +               BT_ERR("TIOCGSERIAL is not supported");
31480 +               return -ENOIOCTLCMD;
31481 +
31482 +       case TIOCSSERIAL:
31483 +               BT_ERR("TIOCSSERIAL is not supported");
31484 +               return -ENOIOCTLCMD;
31485 +
31486 +       case TIOCSERGSTRUCT:
31487 +               BT_ERR("TIOCSERGSTRUCT is not supported");
31488 +               return -ENOIOCTLCMD;
31489 +
31490 +       case TIOCSERGETLSR:
31491 +               BT_ERR("TIOCSERGETLSR is not supported");
31492 +               return -ENOIOCTLCMD;
31493 +
31494 +       case TIOCSERCONFIG:
31495 +               BT_ERR("TIOCSERCONFIG is not supported");
31496 +               return -ENOIOCTLCMD;
31497 +
31498 +       default:
31499 +               return -ENOIOCTLCMD;    /* ioctls which we must ignore */
31500 +
31501 +       }
31502 +
31503 +       return -ENOIOCTLCMD;
31504 +}
31505 +
31506 +#define RELEVANT_IFLAG(iflag) (iflag & (IGNBRK|BRKINT|IGNPAR|PARMRK|INPCK))
31507 +
31508 +static void rfcomm_tty_set_termios(struct tty_struct *tty, struct termios *old)
31509 +{
31510 +       BT_DBG("tty %p", tty);
31511 +
31512 +       if ((tty->termios->c_cflag == old->c_cflag) &&
31513 +               (RELEVANT_IFLAG(tty->termios->c_iflag) == RELEVANT_IFLAG(old->c_iflag)))
31514 +               return;
31515 +
31516 +       /* handle turning off CRTSCTS */
31517 +       if ((old->c_cflag & CRTSCTS) && !(tty->termios->c_cflag & CRTSCTS)) {
31518 +               BT_DBG("turning off CRTSCTS");
31519 +       }
31520 +}
31521 +
31522 +static void rfcomm_tty_throttle(struct tty_struct *tty)
31523 +{
31524 +       struct rfcomm_dev *dev = (struct rfcomm_dev *) tty->driver_data;
31525 +
31526 +       BT_DBG("tty %p dev %p", tty, dev);
31527 +       
31528 +       rfcomm_dlc_throttle(dev->dlc);
31529 +}
31530 +
31531 +static void rfcomm_tty_unthrottle(struct tty_struct *tty)
31532 +{
31533 +       struct rfcomm_dev *dev = (struct rfcomm_dev *) tty->driver_data;
31534 +
31535 +       BT_DBG("tty %p dev %p", tty, dev);
31536 +       
31537 +       rfcomm_dlc_unthrottle(dev->dlc);
31538 +}
31539 +
31540 +static int rfcomm_tty_chars_in_buffer(struct tty_struct *tty)
31541 +{
31542 +       struct rfcomm_dev *dev = (struct rfcomm_dev *) tty->driver_data;
31543 +       struct rfcomm_dlc *dlc = dev->dlc;
31544 +
31545 +       BT_DBG("tty %p dev %p", tty, dev);
31546 +
31547 +       if (skb_queue_len(&dlc->tx_queue))
31548 +               return dlc->mtu;
31549 +
31550 +       return 0;
31551 +}
31552 +
31553 +static void rfcomm_tty_flush_buffer(struct tty_struct *tty)
31554 +{
31555 +       struct rfcomm_dev *dev = (struct rfcomm_dev *) tty->driver_data;
31556 +       if (!dev)
31557 +               return;
31558 +
31559 +       BT_DBG("tty %p dev %p", tty, dev);
31560 +
31561 +       skb_queue_purge(&dev->dlc->tx_queue);
31562 +
31563 +       if (test_bit(TTY_DO_WRITE_WAKEUP, &tty->flags) && tty->ldisc.write_wakeup)
31564 +               tty->ldisc.write_wakeup(tty);
31565 +}
31566 +
31567 +static void rfcomm_tty_send_xchar(struct tty_struct *tty, char ch)
31568 +{
31569 +       BT_DBG("tty %p ch %c", tty, ch);
31570 +}
31571 +
31572 +static void rfcomm_tty_wait_until_sent(struct tty_struct *tty, int timeout)
31573 +{
31574 +       BT_DBG("tty %p timeout %d", tty, timeout);
31575 +}
31576 +
31577 +static void rfcomm_tty_hangup(struct tty_struct *tty)
31578 +{
31579 +       struct rfcomm_dev *dev = (struct rfcomm_dev *) tty->driver_data;
31580 +       if (!dev)
31581 +               return;
31582 +
31583 +       BT_DBG("tty %p dev %p", tty, dev);
31584 +
31585 +       rfcomm_tty_flush_buffer(tty);
31586 +
31587 +       if (test_bit(RFCOMM_RELEASE_ONHUP, &dev->flags))
31588 +               rfcomm_dev_del(dev);
31589 +}
31590 +
31591 +static int rfcomm_tty_read_proc(char *buf, char **start, off_t offset, int len, int *eof, void *unused)
31592 +{
31593 +       return 0;
31594 +}
31595 +
31596 +/* ---- TTY structure ---- */
31597 +static int    rfcomm_tty_refcount;       /* If we manage several devices */
31598 +
31599 +static struct tty_struct *rfcomm_tty_table[RFCOMM_TTY_PORTS];
31600 +static struct termios *rfcomm_tty_termios[RFCOMM_TTY_PORTS];
31601 +static struct termios *rfcomm_tty_termios_locked[RFCOMM_TTY_PORTS];
31602 +
31603 +static struct tty_driver rfcomm_tty_driver = {
31604 +       magic:                  TTY_DRIVER_MAGIC,
31605 +       driver_name:            "rfcomm",
31606 +#ifdef CONFIG_DEVFS_FS
31607 +       name:                   "bluetooth/rfcomm/%d",
31608 +#else
31609 +       name:                   "rfcomm",
31610 +#endif
31611 +       major:                  RFCOMM_TTY_MAJOR,
31612 +       minor_start:            RFCOMM_TTY_MINOR,
31613 +       num:                    RFCOMM_TTY_PORTS,
31614 +       type:                   TTY_DRIVER_TYPE_SERIAL,
31615 +       subtype:                SERIAL_TYPE_NORMAL,
31616 +       flags:                  TTY_DRIVER_REAL_RAW,
31617 +
31618 +       refcount:               &rfcomm_tty_refcount,
31619 +       table:                  rfcomm_tty_table,
31620 +       termios:                rfcomm_tty_termios,
31621 +       termios_locked:         rfcomm_tty_termios_locked,
31622 +
31623 +       open:                   rfcomm_tty_open,
31624 +       close:                  rfcomm_tty_close,
31625 +       put_char:               rfcomm_tty_put_char,
31626 +       write:                  rfcomm_tty_write,
31627 +       write_room:             rfcomm_tty_write_room,
31628 +       chars_in_buffer:        rfcomm_tty_chars_in_buffer,
31629 +       flush_buffer:           rfcomm_tty_flush_buffer,
31630 +       ioctl:                  rfcomm_tty_ioctl,
31631 +       throttle:               rfcomm_tty_throttle,
31632 +       unthrottle:             rfcomm_tty_unthrottle,
31633 +       set_termios:            rfcomm_tty_set_termios,
31634 +       send_xchar:             rfcomm_tty_send_xchar,
31635 +       stop:                   NULL,
31636 +       start:                  NULL,
31637 +       hangup:                 rfcomm_tty_hangup,
31638 +       wait_until_sent:        rfcomm_tty_wait_until_sent,
31639 +       read_proc:              rfcomm_tty_read_proc,
31640 +};
31641 +
31642 +int rfcomm_init_ttys(void)
31643 +{
31644 +       int i;
31645 +
31646 +       /* Initalize our global data */
31647 +       for (i = 0; i < RFCOMM_TTY_PORTS; i++)
31648 +               rfcomm_tty_table[i] = NULL;
31649 +
31650 +       /* Register the TTY driver */
31651 +       rfcomm_tty_driver.init_termios = tty_std_termios;
31652 +       rfcomm_tty_driver.init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL | CLOCAL;
31653 +       rfcomm_tty_driver.flags = TTY_DRIVER_REAL_RAW;
31654 +
31655 +       if (tty_register_driver(&rfcomm_tty_driver)) {
31656 +               BT_ERR("Can't register RFCOMM TTY driver");
31657 +               return -1;
31658 +       }
31659 +
31660 +       return 0;
31661 +}
31662 +
31663 +void rfcomm_cleanup_ttys(void)
31664 +{
31665 +       tty_unregister_driver(&rfcomm_tty_driver);
31666 +       return;
31667 +}
31668 diff -urN linux-2.4.18/net/bluetooth/sco.c linux-2.4.18-mh15/net/bluetooth/sco.c
31669 --- linux-2.4.18/net/bluetooth/sco.c    1970-01-01 01:00:00.000000000 +0100
31670 +++ linux-2.4.18-mh15/net/bluetooth/sco.c       2004-08-01 16:26:23.000000000 +0200
31671 @@ -0,0 +1,1019 @@
31672 +/* 
31673 +   BlueZ - Bluetooth protocol stack for Linux
31674 +   Copyright (C) 2000-2001 Qualcomm Incorporated
31675 +
31676 +   Written 2000,2001 by Maxim Krasnyansky <maxk@qualcomm.com>
31677 +
31678 +   This program is free software; you can redistribute it and/or modify
31679 +   it under the terms of the GNU General Public License version 2 as
31680 +   published by the Free Software Foundation;
31681 +
31682 +   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
31683 +   OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
31684 +   FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS.
31685 +   IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) AND AUTHOR(S) BE LIABLE FOR ANY
31686 +   CLAIM, OR ANY SPECIAL INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES 
31687 +   WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 
31688 +   ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 
31689 +   OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
31690 +
31691 +   ALL LIABILITY, INCLUDING LIABILITY FOR INFRINGEMENT OF ANY PATENTS, 
31692 +   COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS, RELATING TO USE OF THIS 
31693 +   SOFTWARE IS DISCLAIMED.
31694 +*/
31695 +
31696 +/*
31697 + * BlueZ SCO sockets.
31698 + *
31699 + * $Id: sco.c,v 1.4 2002/07/22 20:32:54 maxk Exp $
31700 + */
31701 +#define VERSION "0.3"
31702 +
31703 +#include <linux/config.h>
31704 +#include <linux/module.h>
31705 +
31706 +#include <linux/types.h>
31707 +#include <linux/errno.h>
31708 +#include <linux/kernel.h>
31709 +#include <linux/major.h>
31710 +#include <linux/sched.h>
31711 +#include <linux/slab.h>
31712 +#include <linux/poll.h>
31713 +#include <linux/fcntl.h>
31714 +#include <linux/init.h>
31715 +#include <linux/skbuff.h>
31716 +#include <linux/interrupt.h>
31717 +#include <linux/socket.h>
31718 +#include <linux/skbuff.h>
31719 +#include <linux/proc_fs.h>
31720 +#include <linux/list.h>
31721 +#include <net/sock.h>
31722 +
31723 +#include <asm/system.h>
31724 +#include <asm/uaccess.h>
31725 +
31726 +#include <net/bluetooth/bluetooth.h>
31727 +#include <net/bluetooth/hci_core.h>
31728 +#include <net/bluetooth/sco.h>
31729 +
31730 +#ifndef SCO_DEBUG
31731 +#undef  BT_DBG
31732 +#define BT_DBG( A... )
31733 +#endif
31734 +
31735 +static struct proto_ops sco_sock_ops;
31736 +
31737 +static struct bluez_sock_list sco_sk_list = {
31738 +       lock: RW_LOCK_UNLOCKED
31739 +};
31740 +
31741 +static inline int sco_chan_add(struct sco_conn *conn, struct sock *sk, struct sock *parent);
31742 +static void sco_chan_del(struct sock *sk, int err);
31743 +static inline struct sock * sco_chan_get(struct sco_conn *conn);
31744 +
31745 +static int  sco_conn_del(struct hci_conn *conn, int err);
31746 +
31747 +static void sco_sock_close(struct sock *sk);
31748 +static void sco_sock_kill(struct sock *sk);
31749 +
31750 +/* ----- SCO timers ------ */
31751 +static void sco_sock_timeout(unsigned long arg)
31752 +{
31753 +       struct sock *sk = (struct sock *) arg;
31754 +
31755 +       BT_DBG("sock %p state %d", sk, sk->state);
31756 +
31757 +       bh_lock_sock(sk);
31758 +       sk->err = ETIMEDOUT;
31759 +       sk->state_change(sk);
31760 +       bh_unlock_sock(sk);
31761 +
31762 +       sco_sock_kill(sk);
31763 +       sock_put(sk);
31764 +}
31765 +
31766 +static void sco_sock_set_timer(struct sock *sk, long timeout)
31767 +{
31768 +       BT_DBG("sock %p state %d timeout %ld", sk, sk->state, timeout);
31769 +
31770 +       if (!mod_timer(&sk->timer, jiffies + timeout))
31771 +               sock_hold(sk);
31772 +}
31773 +
31774 +static void sco_sock_clear_timer(struct sock *sk)
31775 +{
31776 +       BT_DBG("sock %p state %d", sk, sk->state);
31777 +
31778 +       if (timer_pending(&sk->timer) && del_timer(&sk->timer))
31779 +               __sock_put(sk);
31780 +}
31781 +
31782 +static void sco_sock_init_timer(struct sock *sk)
31783 +{
31784 +       init_timer(&sk->timer);
31785 +       sk->timer.function = sco_sock_timeout;
31786 +       sk->timer.data = (unsigned long)sk;
31787 +}
31788 +
31789 +/* -------- SCO connections --------- */
31790 +static struct sco_conn *sco_conn_add(struct hci_conn *hcon, __u8 status)
31791 +{
31792 +       struct hci_dev *hdev = hcon->hdev;
31793 +       struct sco_conn *conn;
31794 +
31795 +       if ((conn = hcon->sco_data))
31796 +               return conn;
31797 +
31798 +       if (status)
31799 +               return conn;
31800 +
31801 +       if (!(conn = kmalloc(sizeof(struct sco_conn), GFP_ATOMIC)))
31802 +               return NULL;
31803 +       memset(conn, 0, sizeof(struct sco_conn));
31804 +
31805 +       spin_lock_init(&conn->lock);
31806 +
31807 +       hcon->sco_data = conn;
31808 +       conn->hcon = hcon;
31809 +
31810 +       conn->src = &hdev->bdaddr;
31811 +       conn->dst = &hcon->dst;
31812 +       
31813 +       if (hdev->sco_mtu > 0)
31814 +               conn->mtu = hdev->sco_mtu;
31815 +       else
31816 +               conn->mtu = 60;
31817 +
31818 +       BT_DBG("hcon %p conn %p", hcon, conn);
31819 +
31820 +       MOD_INC_USE_COUNT;
31821 +       return conn;
31822 +}
31823 +
31824 +static int sco_conn_del(struct hci_conn *hcon, int err)
31825 +{
31826 +       struct sco_conn *conn;
31827 +       struct sock *sk;
31828 +
31829 +       if (!(conn = hcon->sco_data)) 
31830 +               return 0;
31831 +
31832 +       BT_DBG("hcon %p conn %p, err %d", hcon, conn, err);
31833 +
31834 +       /* Kill socket */
31835 +       if ((sk = sco_chan_get(conn))) {
31836 +               bh_lock_sock(sk);
31837 +               sco_sock_clear_timer(sk);
31838 +               sco_chan_del(sk, err);
31839 +               bh_unlock_sock(sk);
31840 +               sco_sock_kill(sk);
31841 +       }
31842 +
31843 +       hcon->sco_data = NULL;
31844 +       kfree(conn);
31845 +
31846 +       MOD_DEC_USE_COUNT;
31847 +       return 0;
31848 +}
31849 +
31850 +int sco_connect(struct sock *sk)
31851 +{
31852 +       bdaddr_t *src = &bluez_pi(sk)->src;
31853 +       bdaddr_t *dst = &bluez_pi(sk)->dst;
31854 +       struct sco_conn *conn;
31855 +       struct hci_conn *hcon;
31856 +       struct hci_dev  *hdev;
31857 +       int err = 0;
31858 +
31859 +       BT_DBG("%s -> %s", batostr(src), batostr(dst));
31860 +
31861 +       if (!(hdev = hci_get_route(dst, src)))
31862 +               return -EHOSTUNREACH;
31863 +
31864 +       hci_dev_lock_bh(hdev);
31865 +
31866 +       err = -ENOMEM;
31867 +
31868 +       hcon = hci_connect(hdev, SCO_LINK, dst);
31869 +       if (!hcon)
31870 +               goto done;
31871 +
31872 +       conn = sco_conn_add(hcon, 0);
31873 +       if (!conn) {
31874 +               hci_conn_put(hcon);
31875 +               goto done;
31876 +       }
31877 +
31878 +       /* Update source addr of the socket */
31879 +       bacpy(src, conn->src);
31880 +
31881 +       err = sco_chan_add(conn, sk, NULL);
31882 +       if (err)
31883 +               goto done;
31884 +
31885 +       if (hcon->state == BT_CONNECTED) {
31886 +               sco_sock_clear_timer(sk);
31887 +               sk->state = BT_CONNECTED;
31888 +       } else {
31889 +               sk->state = BT_CONNECT;
31890 +               sco_sock_set_timer(sk, sk->sndtimeo);
31891 +       }
31892 +done:
31893 +       hci_dev_unlock_bh(hdev);
31894 +       hci_dev_put(hdev);
31895 +       return err;
31896 +}
31897 +
31898 +static inline int sco_send_frame(struct sock *sk, struct msghdr *msg, int len)
31899 +{
31900 +       struct sco_conn *conn = sco_pi(sk)->conn;
31901 +       struct sk_buff *skb;
31902 +       int err, count;
31903 +
31904 +       /* Check outgoing MTU */
31905 +       if (len > conn->mtu)
31906 +               return -EINVAL;
31907 +
31908 +       BT_DBG("sk %p len %d", sk, len);
31909 +
31910 +       count = MIN(conn->mtu, len);
31911 +       if (!(skb = bluez_skb_send_alloc(sk, count, msg->msg_flags & MSG_DONTWAIT, &err)))
31912 +               return err;
31913 +
31914 +       if (memcpy_fromiovec(skb_put(skb, count), msg->msg_iov, count)) {
31915 +               err = -EFAULT;
31916 +               goto fail;
31917 +       }
31918 +
31919 +       if ((err = hci_send_sco(conn->hcon, skb)) < 0)
31920 +               goto fail;
31921 +
31922 +       return count;
31923 +
31924 +fail:
31925 +       kfree_skb(skb);
31926 +       return err;
31927 +}
31928 +
31929 +static inline void sco_recv_frame(struct sco_conn *conn, struct sk_buff *skb)
31930 +{
31931 +       struct sock *sk = sco_chan_get(conn);
31932 +
31933 +       if (!sk)
31934 +               goto drop;
31935 +
31936 +       BT_DBG("sk %p len %d", sk, skb->len);
31937 +
31938 +       if (sk->state != BT_CONNECTED)
31939 +               goto drop;
31940 +
31941 +       if (!sock_queue_rcv_skb(sk, skb))
31942 +               return;
31943 +
31944 +drop:
31945 +       kfree_skb(skb);
31946 +       return;
31947 +}
31948 +
31949 +/* -------- Socket interface ---------- */
31950 +static struct sock *__sco_get_sock_by_addr(bdaddr_t *ba)
31951 +{
31952 +       struct sock *sk;
31953 +
31954 +       for (sk = sco_sk_list.head; sk; sk = sk->next) {
31955 +               if (!bacmp(&bluez_pi(sk)->src, ba))
31956 +                       break;
31957 +       }
31958 +
31959 +       return sk;
31960 +}
31961 +
31962 +/* Find socket listening on source bdaddr.
31963 + * Returns closest match.
31964 + */
31965 +static struct sock *sco_get_sock_listen(bdaddr_t *src)
31966 +{
31967 +       struct sock *sk, *sk1 = NULL;
31968 +
31969 +       read_lock(&sco_sk_list.lock);
31970 +
31971 +       for (sk = sco_sk_list.head; sk; sk = sk->next) {
31972 +               if (sk->state != BT_LISTEN)
31973 +                       continue;
31974 +
31975 +               /* Exact match. */
31976 +               if (!bacmp(&bluez_pi(sk)->src, src))
31977 +                       break;
31978 +
31979 +               /* Closest match */
31980 +               if (!bacmp(&bluez_pi(sk)->src, BDADDR_ANY))
31981 +                       sk1 = sk;
31982 +       }
31983 +
31984 +       read_unlock(&sco_sk_list.lock);
31985 +
31986 +       return sk ? sk : sk1;
31987 +}
31988 +
31989 +static void sco_sock_destruct(struct sock *sk)
31990 +{
31991 +       BT_DBG("sk %p", sk);
31992 +
31993 +       skb_queue_purge(&sk->receive_queue);
31994 +       skb_queue_purge(&sk->write_queue);
31995 +
31996 +       MOD_DEC_USE_COUNT;
31997 +}
31998 +
31999 +static void sco_sock_cleanup_listen(struct sock *parent)
32000 +{
32001 +       struct sock *sk;
32002 +
32003 +       BT_DBG("parent %p", parent);
32004 +
32005 +       /* Close not yet accepted channels */
32006 +       while ((sk = bluez_accept_dequeue(parent, NULL))) {
32007 +               sco_sock_close(sk);
32008 +               sco_sock_kill(sk);
32009 +       }
32010 +
32011 +       parent->state  = BT_CLOSED;
32012 +       parent->zapped = 1;
32013 +}
32014 +
32015 +/* Kill socket (only if zapped and orphan)
32016 + * Must be called on unlocked socket.
32017 + */
32018 +static void sco_sock_kill(struct sock *sk)
32019 +{
32020 +       if (!sk->zapped || sk->socket)
32021 +               return;
32022 +
32023 +       BT_DBG("sk %p state %d", sk, sk->state);
32024 +
32025 +       /* Kill poor orphan */
32026 +       bluez_sock_unlink(&sco_sk_list, sk);
32027 +       sk->dead = 1;
32028 +       sock_put(sk);
32029 +}
32030 +
32031 +/* Close socket.
32032 + * Must be called on unlocked socket.
32033 + */
32034 +static void sco_sock_close(struct sock *sk)
32035 +{
32036 +       struct sco_conn *conn;
32037 +
32038 +       sco_sock_clear_timer(sk);
32039 +
32040 +       lock_sock(sk);
32041 +
32042 +       conn = sco_pi(sk)->conn;
32043 +
32044 +       BT_DBG("sk %p state %d conn %p socket %p", sk, sk->state, conn, sk->socket);
32045 +
32046 +       switch (sk->state) {
32047 +       case BT_LISTEN:
32048 +               sco_sock_cleanup_listen(sk);
32049 +               break;
32050 +
32051 +       case BT_CONNECTED:
32052 +       case BT_CONFIG:
32053 +       case BT_CONNECT:
32054 +       case BT_DISCONN:
32055 +               sco_chan_del(sk, ECONNRESET);
32056 +               break;
32057 +
32058 +       default:
32059 +               sk->zapped = 1;
32060 +               break;
32061 +       };
32062 +
32063 +       release_sock(sk);
32064 +}
32065 +
32066 +static void sco_sock_init(struct sock *sk, struct sock *parent)
32067 +{
32068 +       BT_DBG("sk %p", sk);
32069 +
32070 +       if (parent) 
32071 +               sk->type = parent->type;
32072 +}
32073 +
32074 +static struct sock *sco_sock_alloc(struct socket *sock, int proto, int prio)
32075 +{
32076 +       struct sock *sk;
32077 +
32078 +       if (!(sk = sk_alloc(PF_BLUETOOTH, prio, 1)))
32079 +               return NULL;
32080 +
32081 +       bluez_sock_init(sock, sk);
32082 +
32083 +       sk->zapped   = 0;
32084 +
32085 +       sk->destruct = sco_sock_destruct;
32086 +       sk->sndtimeo = SCO_CONN_TIMEOUT;
32087 +
32088 +       sk->protocol = proto;
32089 +       sk->state    = BT_OPEN;
32090 +
32091 +       sco_sock_init_timer(sk);
32092 +
32093 +       bluez_sock_link(&sco_sk_list, sk);
32094 +
32095 +       MOD_INC_USE_COUNT;
32096 +       return sk;
32097 +}
32098 +
32099 +static int sco_sock_create(struct socket *sock, int protocol)
32100 +{
32101 +       struct sock *sk;
32102 +
32103 +       BT_DBG("sock %p", sock);
32104 +
32105 +       sock->state = SS_UNCONNECTED;
32106 +
32107 +       if (sock->type != SOCK_SEQPACKET)
32108 +               return -ESOCKTNOSUPPORT;
32109 +
32110 +       sock->ops = &sco_sock_ops;
32111 +
32112 +       if (!(sk = sco_sock_alloc(sock, protocol, GFP_KERNEL)))
32113 +               return -ENOMEM;
32114 +
32115 +       sco_sock_init(sk, NULL);
32116 +       return 0;
32117 +}
32118 +
32119 +static int sco_sock_bind(struct socket *sock, struct sockaddr *addr, int addr_len)
32120 +{
32121 +       struct sockaddr_sco *sa = (struct sockaddr_sco *) addr;
32122 +       struct sock *sk = sock->sk;
32123 +       bdaddr_t *src = &sa->sco_bdaddr;
32124 +       int err = 0;
32125 +
32126 +       BT_DBG("sk %p %s", sk, batostr(&sa->sco_bdaddr));
32127 +
32128 +       if (!addr || addr->sa_family != AF_BLUETOOTH)
32129 +               return -EINVAL;
32130 +
32131 +       lock_sock(sk);
32132 +
32133 +       if (sk->state != BT_OPEN) {
32134 +               err = -EBADFD;
32135 +               goto done;
32136 +       }
32137 +
32138 +       write_lock_bh(&sco_sk_list.lock);
32139 +
32140 +       if (bacmp(src, BDADDR_ANY) && __sco_get_sock_by_addr(src)) {
32141 +               err = -EADDRINUSE;
32142 +       } else {
32143 +               /* Save source address */
32144 +               bacpy(&bluez_pi(sk)->src, &sa->sco_bdaddr);
32145 +               sk->state = BT_BOUND;
32146 +       }
32147 +
32148 +       write_unlock_bh(&sco_sk_list.lock);
32149 +
32150 +done:
32151 +       release_sock(sk);
32152 +
32153 +       return err;
32154 +}
32155 +
32156 +static int sco_sock_connect(struct socket *sock, struct sockaddr *addr, int alen, int flags)
32157 +{
32158 +       struct sockaddr_sco *sa = (struct sockaddr_sco *) addr;
32159 +       struct sock *sk = sock->sk;
32160 +       int err = 0;
32161 +
32162 +
32163 +       BT_DBG("sk %p", sk);
32164 +
32165 +       if (addr->sa_family != AF_BLUETOOTH || alen < sizeof(struct sockaddr_sco))
32166 +               return -EINVAL;
32167 +
32168 +       if (sk->state != BT_OPEN && sk->state != BT_BOUND)
32169 +               return -EBADFD;
32170 +
32171 +       if (sk->type != SOCK_SEQPACKET)
32172 +               return -EINVAL;
32173 +
32174 +       lock_sock(sk);
32175 +
32176 +       /* Set destination address and psm */
32177 +       bacpy(&bluez_pi(sk)->dst, &sa->sco_bdaddr);
32178 +
32179 +       if ((err = sco_connect(sk)))
32180 +               goto done;
32181 +
32182 +       err = bluez_sock_wait_state(sk, BT_CONNECTED,
32183 +                       sock_sndtimeo(sk, flags & O_NONBLOCK));
32184 +
32185 +done:
32186 +       release_sock(sk);
32187 +       return err;
32188 +}
32189 +
32190 +int sco_sock_listen(struct socket *sock, int backlog)
32191 +{
32192 +       struct sock *sk = sock->sk;
32193 +       int err = 0;
32194 +
32195 +       BT_DBG("sk %p backlog %d", sk, backlog);
32196 +
32197 +       lock_sock(sk);
32198 +
32199 +       if (sk->state != BT_BOUND || sock->type != SOCK_SEQPACKET) {
32200 +               err = -EBADFD;
32201 +               goto done;
32202 +       }
32203 +
32204 +       sk->max_ack_backlog = backlog;
32205 +       sk->ack_backlog = 0;
32206 +       sk->state = BT_LISTEN;
32207 +
32208 +done:
32209 +       release_sock(sk);
32210 +       return err;
32211 +}
32212 +
32213 +int sco_sock_accept(struct socket *sock, struct socket *newsock, int flags)
32214 +{
32215 +       DECLARE_WAITQUEUE(wait, current);
32216 +       struct sock *sk = sock->sk, *ch;
32217 +       long timeo;
32218 +       int err = 0;
32219 +
32220 +       lock_sock(sk);
32221 +
32222 +       if (sk->state != BT_LISTEN) {
32223 +               err = -EBADFD;
32224 +               goto done;
32225 +       }
32226 +
32227 +       timeo = sock_rcvtimeo(sk, flags & O_NONBLOCK);
32228 +
32229 +       BT_DBG("sk %p timeo %ld", sk, timeo);
32230 +
32231 +       /* Wait for an incoming connection. (wake-one). */
32232 +       add_wait_queue_exclusive(sk->sleep, &wait);
32233 +       while (!(ch = bluez_accept_dequeue(sk, newsock))) {
32234 +               set_current_state(TASK_INTERRUPTIBLE);
32235 +               if (!timeo) {
32236 +                       err = -EAGAIN;
32237 +                       break;
32238 +               }
32239 +
32240 +               release_sock(sk);
32241 +               timeo = schedule_timeout(timeo);
32242 +               lock_sock(sk);
32243 +
32244 +               if (sk->state != BT_LISTEN) {
32245 +                       err = -EBADFD;
32246 +                       break;
32247 +               }
32248 +
32249 +               if (signal_pending(current)) {
32250 +                       err = sock_intr_errno(timeo);
32251 +                       break;
32252 +               }
32253 +       }
32254 +       set_current_state(TASK_RUNNING);
32255 +       remove_wait_queue(sk->sleep, &wait);
32256 +
32257 +       if (err)
32258 +               goto done;
32259 +
32260 +       newsock->state = SS_CONNECTED;
32261 +
32262 +       BT_DBG("new socket %p", ch);
32263 +
32264 +done:
32265 +       release_sock(sk);
32266 +       return err;
32267 +}
32268 +
32269 +static int sco_sock_getname(struct socket *sock, struct sockaddr *addr, int *len, int peer)
32270 +{
32271 +       struct sockaddr_sco *sa = (struct sockaddr_sco *) addr;
32272 +       struct sock *sk = sock->sk;
32273 +
32274 +       BT_DBG("sock %p, sk %p", sock, sk);
32275 +
32276 +       addr->sa_family = AF_BLUETOOTH;
32277 +       *len = sizeof(struct sockaddr_sco);
32278 +
32279 +       if (peer)
32280 +               bacpy(&sa->sco_bdaddr, &bluez_pi(sk)->dst);
32281 +       else
32282 +               bacpy(&sa->sco_bdaddr, &bluez_pi(sk)->src);
32283 +
32284 +       return 0;
32285 +}
32286 +
32287 +static int sco_sock_sendmsg(struct socket *sock, struct msghdr *msg, int len, struct scm_cookie *scm)
32288 +{
32289 +       struct sock *sk = sock->sk;
32290 +       int err = 0;
32291 +
32292 +       BT_DBG("sock %p, sk %p", sock, sk);
32293 +
32294 +       if (sk->err)
32295 +               return sock_error(sk);
32296 +
32297 +       if (msg->msg_flags & MSG_OOB)
32298 +               return -EOPNOTSUPP;
32299 +
32300 +       lock_sock(sk);
32301 +
32302 +       if (sk->state == BT_CONNECTED)
32303 +               err = sco_send_frame(sk, msg, len);
32304 +       else
32305 +               err = -ENOTCONN;
32306 +
32307 +       release_sock(sk);
32308 +       return err;
32309 +}
32310 +
32311 +int sco_sock_setsockopt(struct socket *sock, int level, int optname, char *optval, int optlen)
32312 +{
32313 +       struct sock *sk = sock->sk;
32314 +       int err = 0;
32315 +
32316 +       BT_DBG("sk %p", sk);
32317 +
32318 +       lock_sock(sk);
32319 +
32320 +       switch (optname) {
32321 +       default:
32322 +               err = -ENOPROTOOPT;
32323 +               break;
32324 +       };
32325 +
32326 +       release_sock(sk);
32327 +       return err;
32328 +}
32329 +
32330 +int sco_sock_getsockopt(struct socket *sock, int level, int optname, char *optval, int *optlen)
32331 +{
32332 +       struct sock *sk = sock->sk;
32333 +       struct sco_options opts;
32334 +       struct sco_conninfo cinfo;
32335 +       int len, err = 0; 
32336 +
32337 +       BT_DBG("sk %p", sk);
32338 +
32339 +       if (get_user(len, optlen))
32340 +               return -EFAULT;
32341 +
32342 +       lock_sock(sk);
32343 +
32344 +       switch (optname) {
32345 +       case SCO_OPTIONS:
32346 +               if (sk->state != BT_CONNECTED) {
32347 +                       err = -ENOTCONN;
32348 +                       break;
32349 +               }
32350 +               
32351 +               opts.mtu = sco_pi(sk)->conn->mtu;
32352 +
32353 +               BT_DBG("mtu %d", opts.mtu);
32354 +
32355 +               len = MIN(len, sizeof(opts));
32356 +               if (copy_to_user(optval, (char *)&opts, len))
32357 +                       err = -EFAULT;
32358 +
32359 +               break;
32360 +
32361 +       case SCO_CONNINFO:
32362 +               if (sk->state != BT_CONNECTED) {
32363 +                       err = -ENOTCONN;
32364 +                       break;
32365 +               }
32366 +
32367 +               cinfo.hci_handle = sco_pi(sk)->conn->hcon->handle;
32368 +
32369 +               len = MIN(len, sizeof(cinfo));
32370 +               if (copy_to_user(optval, (char *)&cinfo, len))
32371 +                       err = -EFAULT;
32372 +
32373 +               break;
32374 +
32375 +       default:
32376 +               err = -ENOPROTOOPT;
32377 +               break;
32378 +       };
32379 +
32380 +       release_sock(sk);
32381 +       return err;
32382 +}
32383 +
32384 +static int sco_sock_release(struct socket *sock)
32385 +{
32386 +       struct sock *sk = sock->sk;
32387 +       int err = 0;
32388 +
32389 +       BT_DBG("sock %p, sk %p", sock, sk);
32390 +
32391 +       if (!sk)
32392 +               return 0;
32393 +
32394 +       sco_sock_close(sk);
32395 +       if (sk->linger) {
32396 +               lock_sock(sk);
32397 +               err = bluez_sock_wait_state(sk, BT_CLOSED, sk->lingertime);
32398 +               release_sock(sk);
32399 +       }
32400 +
32401 +       sock_orphan(sk);
32402 +       sco_sock_kill(sk);
32403 +       return err;
32404 +}
32405 +
32406 +static void __sco_chan_add(struct sco_conn *conn, struct sock *sk, struct sock *parent)
32407 +{
32408 +       BT_DBG("conn %p", conn);
32409 +
32410 +       sco_pi(sk)->conn = conn;
32411 +       conn->sk = sk;
32412 +
32413 +       if (parent)
32414 +               bluez_accept_enqueue(parent, sk);
32415 +}
32416 +
32417 +static inline int sco_chan_add(struct sco_conn *conn, struct sock *sk, struct sock *parent)
32418 +{
32419 +       int err = 0;
32420 +
32421 +       sco_conn_lock(conn);
32422 +       if (conn->sk) {
32423 +               err = -EBUSY;
32424 +       } else {
32425 +               __sco_chan_add(conn, sk, parent);
32426 +       }
32427 +       sco_conn_unlock(conn);
32428 +       return err;
32429 +}
32430 +
32431 +static inline struct sock * sco_chan_get(struct sco_conn *conn)
32432 +{
32433 +       struct sock *sk = NULL;
32434 +       sco_conn_lock(conn);
32435 +       sk = conn->sk;
32436 +       sco_conn_unlock(conn);
32437 +       return sk;
32438 +}
32439 +
32440 +/* Delete channel. 
32441 + * Must be called on the locked socket. */
32442 +static void sco_chan_del(struct sock *sk, int err)
32443 +{
32444 +       struct sco_conn *conn;
32445 +
32446 +       conn = sco_pi(sk)->conn;
32447 +
32448 +       BT_DBG("sk %p, conn %p, err %d", sk, conn, err);
32449 +
32450 +       if (conn) { 
32451 +               sco_conn_lock(conn);
32452 +               conn->sk = NULL;
32453 +               sco_pi(sk)->conn = NULL;
32454 +               sco_conn_unlock(conn);
32455 +               hci_conn_put(conn->hcon);
32456 +       }
32457 +
32458 +       sk->state = BT_CLOSED;
32459 +       sk->err   = err;
32460 +       sk->state_change(sk);
32461 +
32462 +       sk->zapped = 1;
32463 +}
32464 +
32465 +static void sco_conn_ready(struct sco_conn *conn)
32466 +{
32467 +       struct sock *parent, *sk;
32468 +
32469 +       BT_DBG("conn %p", conn);
32470 +
32471 +       sco_conn_lock(conn);
32472 +
32473 +       if ((sk = conn->sk)) {
32474 +               sco_sock_clear_timer(sk);
32475 +               bh_lock_sock(sk);
32476 +               sk->state = BT_CONNECTED;
32477 +               sk->state_change(sk);
32478 +               bh_unlock_sock(sk);
32479 +       } else {
32480 +               parent = sco_get_sock_listen(conn->src);
32481 +               if (!parent)
32482 +                       goto done;
32483 +
32484 +               bh_lock_sock(parent);
32485 +
32486 +               sk = sco_sock_alloc(NULL, BTPROTO_SCO, GFP_ATOMIC);
32487 +               if (!sk) {
32488 +                       bh_unlock_sock(parent);
32489 +                       goto done;
32490 +               }
32491 +
32492 +               sco_sock_init(sk, parent);
32493 +
32494 +               bacpy(&bluez_pi(sk)->src, conn->src);
32495 +               bacpy(&bluez_pi(sk)->dst, conn->dst);
32496 +
32497 +               hci_conn_hold(conn->hcon);
32498 +               __sco_chan_add(conn, sk, parent);
32499 +
32500 +               sk->state = BT_CONNECTED;
32501 +
32502 +               /* Wake up parent */
32503 +               parent->data_ready(parent, 1);
32504 +       
32505 +               bh_unlock_sock(parent);
32506 +       }
32507 +
32508 +done:
32509 +       sco_conn_unlock(conn);
32510 +}
32511 +
32512 +/* ----- SCO interface with lower layer (HCI) ----- */
32513 +int sco_connect_ind(struct hci_dev *hdev, bdaddr_t *bdaddr, __u8 type)
32514 +{
32515 +       BT_DBG("hdev %s, bdaddr %s", hdev->name, batostr(bdaddr));
32516 +
32517 +       /* Always accept connection */
32518 +       return HCI_LM_ACCEPT;
32519 +}
32520 +
32521 +int sco_connect_cfm(struct hci_conn *hcon, __u8 status)
32522 +{
32523 +       BT_DBG("hcon %p bdaddr %s status %d", hcon, batostr(&hcon->dst), status);
32524 +
32525 +       if (hcon->type != SCO_LINK)
32526 +               return 0;
32527 +
32528 +       if (!status) {
32529 +               struct sco_conn *conn;
32530 +
32531 +               conn = sco_conn_add(hcon, status);
32532 +               if (conn)
32533 +                       sco_conn_ready(conn);
32534 +       } else 
32535 +               sco_conn_del(hcon, bterr(status));
32536 +       
32537 +       return 0;
32538 +}
32539 +
32540 +int sco_disconn_ind(struct hci_conn *hcon, __u8 reason)
32541 +{
32542 +       BT_DBG("hcon %p reason %d", hcon, reason);
32543 +
32544 +       if (hcon->type != SCO_LINK)
32545 +               return 0;
32546 +
32547 +       sco_conn_del(hcon, bterr(reason));
32548 +       return 0;
32549 +}
32550 +
32551 +int sco_recv_scodata(struct hci_conn *hcon, struct sk_buff *skb)
32552 +{
32553 +       struct sco_conn *conn = hcon->sco_data;
32554 +
32555 +       if (!conn)
32556 +               goto drop;
32557 +
32558 +       BT_DBG("conn %p len %d", conn, skb->len);
32559 +
32560 +       if (skb->len) {
32561 +               sco_recv_frame(conn, skb);
32562 +               return 0;
32563 +       }
32564 +
32565 +drop:
32566 +       kfree_skb(skb); 
32567 +       return 0;
32568 +}
32569 +
32570 +/* ----- Proc fs support ------ */
32571 +static int sco_sock_dump(char *buf, struct bluez_sock_list *list)
32572 +{
32573 +       struct sco_pinfo *pi;
32574 +       struct sock *sk;
32575 +       char *ptr = buf;
32576 +
32577 +       write_lock_bh(&list->lock);
32578 +
32579 +       for (sk = list->head; sk; sk = sk->next) {
32580 +               pi = sco_pi(sk);
32581 +               ptr += sprintf(ptr, "%s %s %d\n",
32582 +                               batostr(&bluez_pi(sk)->src), batostr(&bluez_pi(sk)->dst),
32583 +                               sk->state); 
32584 +       }
32585 +
32586 +       write_unlock_bh(&list->lock);
32587 +
32588 +       ptr += sprintf(ptr, "\n");
32589 +
32590 +       return ptr - buf;
32591 +}
32592 +
32593 +static int sco_read_proc(char *buf, char **start, off_t offset, int count, int *eof, void *priv)
32594 +{
32595 +       char *ptr = buf;
32596 +       int len;
32597 +
32598 +       BT_DBG("count %d, offset %ld", count, offset);
32599 +
32600 +       ptr += sco_sock_dump(ptr, &sco_sk_list);
32601 +       len  = ptr - buf;
32602 +
32603 +       if (len <= count + offset)
32604 +               *eof = 1;
32605 +
32606 +       *start = buf + offset;
32607 +       len -= offset;
32608 +
32609 +       if (len > count)
32610 +               len = count;
32611 +       if (len < 0)
32612 +               len = 0;
32613 +
32614 +       return len;
32615 +}
32616 +
32617 +static struct proto_ops sco_sock_ops = {
32618 +       family:         PF_BLUETOOTH,
32619 +       release:        sco_sock_release,
32620 +       bind:           sco_sock_bind,
32621 +       connect:        sco_sock_connect,
32622 +       listen:         sco_sock_listen,
32623 +       accept:         sco_sock_accept,
32624 +       getname:        sco_sock_getname,
32625 +       sendmsg:        sco_sock_sendmsg,
32626 +       recvmsg:        bluez_sock_recvmsg,
32627 +       poll:           bluez_sock_poll,
32628 +       socketpair:     sock_no_socketpair,
32629 +       ioctl:          sock_no_ioctl,
32630 +       shutdown:       sock_no_shutdown,
32631 +       setsockopt:     sco_sock_setsockopt,
32632 +       getsockopt:     sco_sock_getsockopt,
32633 +       mmap:           sock_no_mmap
32634 +};
32635 +
32636 +static struct net_proto_family sco_sock_family_ops = {
32637 +       family:         PF_BLUETOOTH,
32638 +       create:         sco_sock_create
32639 +};
32640 +
32641 +static struct hci_proto sco_hci_proto = {
32642 +       name:           "SCO",
32643 +       id:             HCI_PROTO_SCO,
32644 +       connect_ind:    sco_connect_ind,
32645 +       connect_cfm:    sco_connect_cfm,
32646 +       disconn_ind:    sco_disconn_ind,
32647 +       recv_scodata:   sco_recv_scodata,
32648 +};
32649 +
32650 +int __init sco_init(void)
32651 +{
32652 +       int err;
32653 +
32654 +       if ((err = bluez_sock_register(BTPROTO_SCO, &sco_sock_family_ops))) {
32655 +               BT_ERR("Can't register SCO socket layer");
32656 +               return err;
32657 +       }
32658 +
32659 +       if ((err = hci_register_proto(&sco_hci_proto))) {
32660 +               BT_ERR("Can't register SCO protocol");
32661 +               return err;
32662 +       }
32663 +
32664 +       create_proc_read_entry("bluetooth/sco", 0, 0, sco_read_proc, NULL);
32665 +
32666 +       BT_INFO("BlueZ SCO ver %s Copyright (C) 2000,2001 Qualcomm Inc", VERSION);
32667 +       BT_INFO("Written 2000,2001 by Maxim Krasnyansky <maxk@qualcomm.com>");
32668 +       return 0;
32669 +}
32670 +
32671 +void sco_cleanup(void)
32672 +{
32673 +       int err;
32674 +
32675 +       remove_proc_entry("bluetooth/sco", NULL);
32676 +
32677 +       /* Unregister socket, protocol and notifier */
32678 +       if ((err = bluez_sock_unregister(BTPROTO_SCO)))
32679 +               BT_ERR("Can't unregister SCO socket layer %d", err);
32680 +
32681 +       if ((err = hci_unregister_proto(&sco_hci_proto)))
32682 +               BT_ERR("Can't unregister SCO protocol %d", err);
32683 +}
32684 +
32685 +module_init(sco_init);
32686 +module_exit(sco_cleanup);
32687 +
32688 +MODULE_AUTHOR("Maxim Krasnyansky <maxk@qualcomm.com>");
32689 +MODULE_DESCRIPTION("BlueZ SCO ver " VERSION);
32690 +MODULE_LICENSE("GPL");
32691 diff -urN linux-2.4.18/net/bluetooth/syms.c linux-2.4.18-mh15/net/bluetooth/syms.c
32692 --- linux-2.4.18/net/bluetooth/syms.c   2001-09-07 18:28:38.000000000 +0200
32693 +++ linux-2.4.18-mh15/net/bluetooth/syms.c      2004-08-01 16:26:23.000000000 +0200
32694 @@ -25,7 +25,7 @@
32695  /*
32696   * BlueZ symbols.
32697   *
32698 - * $Id: syms.c,v 1.1 2001/07/12 19:31:24 maxk Exp $
32699 + * $Id: syms.c,v 1.1 2002/03/08 21:06:59 maxk Exp $
32700   */
32701  
32702  #include <linux/config.h>
32703 @@ -39,25 +39,28 @@
32704  #include <linux/socket.h>
32705  
32706  #include <net/bluetooth/bluetooth.h>
32707 -#include <net/bluetooth/bluez.h>
32708  #include <net/bluetooth/hci_core.h>
32709  
32710  /* HCI Core */
32711  EXPORT_SYMBOL(hci_register_dev);
32712  EXPORT_SYMBOL(hci_unregister_dev);
32713 +EXPORT_SYMBOL(hci_suspend_dev);
32714 +EXPORT_SYMBOL(hci_resume_dev);
32715 +
32716  EXPORT_SYMBOL(hci_register_proto);
32717  EXPORT_SYMBOL(hci_unregister_proto);
32718 -EXPORT_SYMBOL(hci_register_notifier);
32719 -EXPORT_SYMBOL(hci_unregister_notifier);
32720  
32721 +EXPORT_SYMBOL(hci_get_route);
32722  EXPORT_SYMBOL(hci_connect);
32723 -EXPORT_SYMBOL(hci_disconnect);
32724  EXPORT_SYMBOL(hci_dev_get);
32725 +EXPORT_SYMBOL(hci_conn_auth);
32726 +EXPORT_SYMBOL(hci_conn_encrypt);
32727  
32728  EXPORT_SYMBOL(hci_recv_frame);
32729  EXPORT_SYMBOL(hci_send_acl);
32730  EXPORT_SYMBOL(hci_send_sco);
32731 -EXPORT_SYMBOL(hci_send_raw);
32732 +EXPORT_SYMBOL(hci_send_cmd);
32733 +EXPORT_SYMBOL(hci_si_event);
32734  
32735  /* BlueZ lib */
32736  EXPORT_SYMBOL(bluez_dump);
32737 @@ -68,5 +71,11 @@
32738  /* BlueZ sockets */
32739  EXPORT_SYMBOL(bluez_sock_register);
32740  EXPORT_SYMBOL(bluez_sock_unregister);
32741 +EXPORT_SYMBOL(bluez_sock_init);
32742  EXPORT_SYMBOL(bluez_sock_link);
32743  EXPORT_SYMBOL(bluez_sock_unlink);
32744 +EXPORT_SYMBOL(bluez_sock_recvmsg);
32745 +EXPORT_SYMBOL(bluez_sock_poll);
32746 +EXPORT_SYMBOL(bluez_accept_enqueue);
32747 +EXPORT_SYMBOL(bluez_accept_dequeue);
32748 +EXPORT_SYMBOL(bluez_sock_wait_state);
32749 diff -urN linux-2.4.18/net/netsyms.c linux-2.4.18-mh15/net/netsyms.c
32750 --- linux-2.4.18/net/netsyms.c  2002-02-25 20:38:14.000000000 +0100
32751 +++ linux-2.4.18-mh15/net/netsyms.c     2004-08-01 16:26:23.000000000 +0200
32752 @@ -159,6 +159,7 @@
32753  EXPORT_SYMBOL(put_cmsg);
32754  EXPORT_SYMBOL(sock_kmalloc);
32755  EXPORT_SYMBOL(sock_kfree_s);
32756 +EXPORT_SYMBOL(sockfd_lookup);
32757  
32758  #ifdef CONFIG_FILTER
32759  EXPORT_SYMBOL(sk_run_filter);