11da177e4SLinus Torvalds /* 21da177e4SLinus Torvalds CMTP implementation for Linux Bluetooth stack (BlueZ). 31da177e4SLinus Torvalds Copyright (C) 2002-2003 Marcel Holtmann <marcel@holtmann.org> 41da177e4SLinus Torvalds 51da177e4SLinus Torvalds This program is free software; you can redistribute it and/or modify 61da177e4SLinus Torvalds it under the terms of the GNU General Public License version 2 as 71da177e4SLinus Torvalds published by the Free Software Foundation; 81da177e4SLinus Torvalds 91da177e4SLinus Torvalds THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 101da177e4SLinus Torvalds OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 111da177e4SLinus Torvalds FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS. 121da177e4SLinus Torvalds IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) AND AUTHOR(S) BE LIABLE FOR ANY 131da177e4SLinus Torvalds CLAIM, OR ANY SPECIAL INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES 141da177e4SLinus Torvalds WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 151da177e4SLinus Torvalds ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 161da177e4SLinus Torvalds OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 171da177e4SLinus Torvalds 181da177e4SLinus Torvalds ALL LIABILITY, INCLUDING LIABILITY FOR INFRINGEMENT OF ANY PATENTS, 191da177e4SLinus Torvalds COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS, RELATING TO USE OF THIS 201da177e4SLinus Torvalds SOFTWARE IS DISCLAIMED. 211da177e4SLinus Torvalds */ 221da177e4SLinus Torvalds 231da177e4SLinus Torvalds #include <linux/module.h> 249a58a80aSAlexey Dobriyan #include <linux/proc_fs.h> 259a58a80aSAlexey Dobriyan #include <linux/seq_file.h> 261da177e4SLinus Torvalds #include <linux/types.h> 271da177e4SLinus Torvalds #include <linux/errno.h> 281da177e4SLinus Torvalds #include <linux/kernel.h> 291da177e4SLinus Torvalds #include <linux/sched.h> 301da177e4SLinus Torvalds #include <linux/slab.h> 311da177e4SLinus Torvalds #include <linux/poll.h> 321da177e4SLinus Torvalds #include <linux/fcntl.h> 331da177e4SLinus Torvalds #include <linux/skbuff.h> 341da177e4SLinus Torvalds #include <linux/socket.h> 351da177e4SLinus Torvalds #include <linux/ioctl.h> 361da177e4SLinus Torvalds #include <linux/file.h> 371da177e4SLinus Torvalds #include <linux/wait.h> 38*fada4ac3SSzymon Janc #include <linux/kthread.h> 391da177e4SLinus Torvalds #include <net/sock.h> 401da177e4SLinus Torvalds 411da177e4SLinus Torvalds #include <linux/isdn/capilli.h> 421da177e4SLinus Torvalds #include <linux/isdn/capicmd.h> 431da177e4SLinus Torvalds #include <linux/isdn/capiutil.h> 441da177e4SLinus Torvalds 451da177e4SLinus Torvalds #include "cmtp.h" 461da177e4SLinus Torvalds 471da177e4SLinus Torvalds #define CAPI_INTEROPERABILITY 0x20 481da177e4SLinus Torvalds 491da177e4SLinus Torvalds #define CAPI_INTEROPERABILITY_REQ CAPICMD(CAPI_INTEROPERABILITY, CAPI_REQ) 501da177e4SLinus Torvalds #define CAPI_INTEROPERABILITY_CONF CAPICMD(CAPI_INTEROPERABILITY, CAPI_CONF) 511da177e4SLinus Torvalds #define CAPI_INTEROPERABILITY_IND CAPICMD(CAPI_INTEROPERABILITY, CAPI_IND) 521da177e4SLinus Torvalds #define CAPI_INTEROPERABILITY_RESP CAPICMD(CAPI_INTEROPERABILITY, CAPI_RESP) 531da177e4SLinus Torvalds 541da177e4SLinus Torvalds #define CAPI_INTEROPERABILITY_REQ_LEN (CAPI_MSG_BASELEN + 2) 551da177e4SLinus Torvalds #define CAPI_INTEROPERABILITY_CONF_LEN (CAPI_MSG_BASELEN + 4) 561da177e4SLinus Torvalds #define CAPI_INTEROPERABILITY_IND_LEN (CAPI_MSG_BASELEN + 2) 571da177e4SLinus Torvalds #define CAPI_INTEROPERABILITY_RESP_LEN (CAPI_MSG_BASELEN + 2) 581da177e4SLinus Torvalds 591da177e4SLinus Torvalds #define CAPI_FUNCTION_REGISTER 0 601da177e4SLinus Torvalds #define CAPI_FUNCTION_RELEASE 1 611da177e4SLinus Torvalds #define CAPI_FUNCTION_GET_PROFILE 2 621da177e4SLinus Torvalds #define CAPI_FUNCTION_GET_MANUFACTURER 3 631da177e4SLinus Torvalds #define CAPI_FUNCTION_GET_VERSION 4 641da177e4SLinus Torvalds #define CAPI_FUNCTION_GET_SERIAL_NUMBER 5 651da177e4SLinus Torvalds #define CAPI_FUNCTION_MANUFACTURER 6 661da177e4SLinus Torvalds #define CAPI_FUNCTION_LOOPBACK 7 671da177e4SLinus Torvalds 681da177e4SLinus Torvalds 691da177e4SLinus Torvalds #define CMTP_MSGNUM 1 701da177e4SLinus Torvalds #define CMTP_APPLID 2 711da177e4SLinus Torvalds #define CMTP_MAPPING 3 721da177e4SLinus Torvalds 731da177e4SLinus Torvalds static struct cmtp_application *cmtp_application_add(struct cmtp_session *session, __u16 appl) 741da177e4SLinus Torvalds { 7525ea6db0SMarcel Holtmann struct cmtp_application *app = kzalloc(sizeof(*app), GFP_KERNEL); 761da177e4SLinus Torvalds 771da177e4SLinus Torvalds BT_DBG("session %p application %p appl %d", session, app, appl); 781da177e4SLinus Torvalds 791da177e4SLinus Torvalds if (!app) 801da177e4SLinus Torvalds return NULL; 811da177e4SLinus Torvalds 821da177e4SLinus Torvalds app->state = BT_OPEN; 831da177e4SLinus Torvalds app->appl = appl; 841da177e4SLinus Torvalds 851da177e4SLinus Torvalds list_add_tail(&app->list, &session->applications); 861da177e4SLinus Torvalds 871da177e4SLinus Torvalds return app; 881da177e4SLinus Torvalds } 891da177e4SLinus Torvalds 901da177e4SLinus Torvalds static void cmtp_application_del(struct cmtp_session *session, struct cmtp_application *app) 911da177e4SLinus Torvalds { 921da177e4SLinus Torvalds BT_DBG("session %p application %p", session, app); 931da177e4SLinus Torvalds 941da177e4SLinus Torvalds if (app) { 951da177e4SLinus Torvalds list_del(&app->list); 961da177e4SLinus Torvalds kfree(app); 971da177e4SLinus Torvalds } 981da177e4SLinus Torvalds } 991da177e4SLinus Torvalds 1001da177e4SLinus Torvalds static struct cmtp_application *cmtp_application_get(struct cmtp_session *session, int pattern, __u16 value) 1011da177e4SLinus Torvalds { 1021da177e4SLinus Torvalds struct cmtp_application *app; 1031da177e4SLinus Torvalds struct list_head *p, *n; 1041da177e4SLinus Torvalds 1051da177e4SLinus Torvalds list_for_each_safe(p, n, &session->applications) { 1061da177e4SLinus Torvalds app = list_entry(p, struct cmtp_application, list); 1071da177e4SLinus Torvalds switch (pattern) { 1081da177e4SLinus Torvalds case CMTP_MSGNUM: 1091da177e4SLinus Torvalds if (app->msgnum == value) 1101da177e4SLinus Torvalds return app; 1111da177e4SLinus Torvalds break; 1121da177e4SLinus Torvalds case CMTP_APPLID: 1131da177e4SLinus Torvalds if (app->appl == value) 1141da177e4SLinus Torvalds return app; 1151da177e4SLinus Torvalds break; 1161da177e4SLinus Torvalds case CMTP_MAPPING: 1171da177e4SLinus Torvalds if (app->mapping == value) 1181da177e4SLinus Torvalds return app; 1191da177e4SLinus Torvalds break; 1201da177e4SLinus Torvalds } 1211da177e4SLinus Torvalds } 1221da177e4SLinus Torvalds 1231da177e4SLinus Torvalds return NULL; 1241da177e4SLinus Torvalds } 1251da177e4SLinus Torvalds 1261da177e4SLinus Torvalds static int cmtp_msgnum_get(struct cmtp_session *session) 1271da177e4SLinus Torvalds { 1281da177e4SLinus Torvalds session->msgnum++; 1291da177e4SLinus Torvalds 1301da177e4SLinus Torvalds if ((session->msgnum & 0xff) > 200) 1311da177e4SLinus Torvalds session->msgnum = CMTP_INITIAL_MSGNUM + 1; 1321da177e4SLinus Torvalds 1331da177e4SLinus Torvalds return session->msgnum; 1341da177e4SLinus Torvalds } 1351da177e4SLinus Torvalds 1361da177e4SLinus Torvalds static void cmtp_send_capimsg(struct cmtp_session *session, struct sk_buff *skb) 1371da177e4SLinus Torvalds { 1381da177e4SLinus Torvalds struct cmtp_scb *scb = (void *) skb->cb; 1391da177e4SLinus Torvalds 1401da177e4SLinus Torvalds BT_DBG("session %p skb %p len %d", session, skb, skb->len); 1411da177e4SLinus Torvalds 1421da177e4SLinus Torvalds scb->id = -1; 1431da177e4SLinus Torvalds scb->data = (CAPIMSG_COMMAND(skb->data) == CAPI_DATA_B3); 1441da177e4SLinus Torvalds 1451da177e4SLinus Torvalds skb_queue_tail(&session->transmit, skb); 1461da177e4SLinus Torvalds 147*fada4ac3SSzymon Janc wake_up_interruptible(sk_sleep(session->sock->sk)); 1481da177e4SLinus Torvalds } 1491da177e4SLinus Torvalds 1501da177e4SLinus Torvalds static void cmtp_send_interopmsg(struct cmtp_session *session, 1511da177e4SLinus Torvalds __u8 subcmd, __u16 appl, __u16 msgnum, 1521da177e4SLinus Torvalds __u16 function, unsigned char *buf, int len) 1531da177e4SLinus Torvalds { 1541da177e4SLinus Torvalds struct sk_buff *skb; 1551da177e4SLinus Torvalds unsigned char *s; 1561da177e4SLinus Torvalds 1571da177e4SLinus Torvalds BT_DBG("session %p subcmd 0x%02x appl %d msgnum %d", session, subcmd, appl, msgnum); 1581da177e4SLinus Torvalds 1595a08ecceSAndrei Emeltchenko skb = alloc_skb(CAPI_MSG_BASELEN + 6 + len, GFP_ATOMIC); 1605a08ecceSAndrei Emeltchenko if (!skb) { 1611da177e4SLinus Torvalds BT_ERR("Can't allocate memory for interoperability packet"); 1621da177e4SLinus Torvalds return; 1631da177e4SLinus Torvalds } 1641da177e4SLinus Torvalds 1651da177e4SLinus Torvalds s = skb_put(skb, CAPI_MSG_BASELEN + 6 + len); 1661da177e4SLinus Torvalds 1671da177e4SLinus Torvalds capimsg_setu16(s, 0, CAPI_MSG_BASELEN + 6 + len); 1681da177e4SLinus Torvalds capimsg_setu16(s, 2, appl); 1691da177e4SLinus Torvalds capimsg_setu8 (s, 4, CAPI_INTEROPERABILITY); 1701da177e4SLinus Torvalds capimsg_setu8 (s, 5, subcmd); 1711da177e4SLinus Torvalds capimsg_setu16(s, 6, msgnum); 1721da177e4SLinus Torvalds 1731da177e4SLinus Torvalds /* Interoperability selector (Bluetooth Device Management) */ 1741da177e4SLinus Torvalds capimsg_setu16(s, 8, 0x0001); 1751da177e4SLinus Torvalds 1761da177e4SLinus Torvalds capimsg_setu8 (s, 10, 3 + len); 1771da177e4SLinus Torvalds capimsg_setu16(s, 11, function); 1781da177e4SLinus Torvalds capimsg_setu8 (s, 13, len); 1791da177e4SLinus Torvalds 1801da177e4SLinus Torvalds if (len > 0) 1811da177e4SLinus Torvalds memcpy(s + 14, buf, len); 1821da177e4SLinus Torvalds 1831da177e4SLinus Torvalds cmtp_send_capimsg(session, skb); 1841da177e4SLinus Torvalds } 1851da177e4SLinus Torvalds 1861da177e4SLinus Torvalds static void cmtp_recv_interopmsg(struct cmtp_session *session, struct sk_buff *skb) 1871da177e4SLinus Torvalds { 1881da177e4SLinus Torvalds struct capi_ctr *ctrl = &session->ctrl; 1891da177e4SLinus Torvalds struct cmtp_application *application; 1901da177e4SLinus Torvalds __u16 appl, msgnum, func, info; 1911da177e4SLinus Torvalds __u32 controller; 1921da177e4SLinus Torvalds 1931da177e4SLinus Torvalds BT_DBG("session %p skb %p len %d", session, skb, skb->len); 1941da177e4SLinus Torvalds 1951da177e4SLinus Torvalds switch (CAPIMSG_SUBCOMMAND(skb->data)) { 1961da177e4SLinus Torvalds case CAPI_CONF: 197f4777569SMarcel Holtmann if (skb->len < CAPI_MSG_BASELEN + 10) 198f4777569SMarcel Holtmann break; 199f4777569SMarcel Holtmann 2001da177e4SLinus Torvalds func = CAPIMSG_U16(skb->data, CAPI_MSG_BASELEN + 5); 2011da177e4SLinus Torvalds info = CAPIMSG_U16(skb->data, CAPI_MSG_BASELEN + 8); 2021da177e4SLinus Torvalds 2031da177e4SLinus Torvalds switch (func) { 2041da177e4SLinus Torvalds case CAPI_FUNCTION_REGISTER: 2051da177e4SLinus Torvalds msgnum = CAPIMSG_MSGID(skb->data); 2061da177e4SLinus Torvalds 2071da177e4SLinus Torvalds application = cmtp_application_get(session, CMTP_MSGNUM, msgnum); 2081da177e4SLinus Torvalds if (application) { 2091da177e4SLinus Torvalds application->state = BT_CONNECTED; 2101da177e4SLinus Torvalds application->msgnum = 0; 2111da177e4SLinus Torvalds application->mapping = CAPIMSG_APPID(skb->data); 2121da177e4SLinus Torvalds wake_up_interruptible(&session->wait); 2131da177e4SLinus Torvalds } 2141da177e4SLinus Torvalds 2151da177e4SLinus Torvalds break; 2161da177e4SLinus Torvalds 2171da177e4SLinus Torvalds case CAPI_FUNCTION_RELEASE: 2181da177e4SLinus Torvalds appl = CAPIMSG_APPID(skb->data); 2191da177e4SLinus Torvalds 2201da177e4SLinus Torvalds application = cmtp_application_get(session, CMTP_MAPPING, appl); 2211da177e4SLinus Torvalds if (application) { 2221da177e4SLinus Torvalds application->state = BT_CLOSED; 2231da177e4SLinus Torvalds application->msgnum = 0; 2241da177e4SLinus Torvalds wake_up_interruptible(&session->wait); 2251da177e4SLinus Torvalds } 2261da177e4SLinus Torvalds 2271da177e4SLinus Torvalds break; 2281da177e4SLinus Torvalds 2291da177e4SLinus Torvalds case CAPI_FUNCTION_GET_PROFILE: 230f4777569SMarcel Holtmann if (skb->len < CAPI_MSG_BASELEN + 11 + sizeof(capi_profile)) 231f4777569SMarcel Holtmann break; 232f4777569SMarcel Holtmann 2331da177e4SLinus Torvalds controller = CAPIMSG_U16(skb->data, CAPI_MSG_BASELEN + 11); 2341da177e4SLinus Torvalds msgnum = CAPIMSG_MSGID(skb->data); 2351da177e4SLinus Torvalds 2361da177e4SLinus Torvalds if (!info && (msgnum == CMTP_INITIAL_MSGNUM)) { 2371da177e4SLinus Torvalds session->ncontroller = controller; 2381da177e4SLinus Torvalds wake_up_interruptible(&session->wait); 2391da177e4SLinus Torvalds break; 2401da177e4SLinus Torvalds } 2411da177e4SLinus Torvalds 2421da177e4SLinus Torvalds if (!info && ctrl) { 2431da177e4SLinus Torvalds memcpy(&ctrl->profile, 2441da177e4SLinus Torvalds skb->data + CAPI_MSG_BASELEN + 11, 2451da177e4SLinus Torvalds sizeof(capi_profile)); 2461da177e4SLinus Torvalds session->state = BT_CONNECTED; 2471da177e4SLinus Torvalds capi_ctr_ready(ctrl); 2481da177e4SLinus Torvalds } 2491da177e4SLinus Torvalds 2501da177e4SLinus Torvalds break; 2511da177e4SLinus Torvalds 2521da177e4SLinus Torvalds case CAPI_FUNCTION_GET_MANUFACTURER: 253f4777569SMarcel Holtmann if (skb->len < CAPI_MSG_BASELEN + 15) 254f4777569SMarcel Holtmann break; 255f4777569SMarcel Holtmann 2561da177e4SLinus Torvalds controller = CAPIMSG_U32(skb->data, CAPI_MSG_BASELEN + 10); 2571da177e4SLinus Torvalds 2581da177e4SLinus Torvalds if (!info && ctrl) { 259f4777569SMarcel Holtmann int len = min_t(uint, CAPI_MANUFACTURER_LEN, 2601da177e4SLinus Torvalds skb->data[CAPI_MSG_BASELEN + 14]); 261f4777569SMarcel Holtmann 262f4777569SMarcel Holtmann memset(ctrl->manu, 0, CAPI_MANUFACTURER_LEN); 263f4777569SMarcel Holtmann strncpy(ctrl->manu, 264f4777569SMarcel Holtmann skb->data + CAPI_MSG_BASELEN + 15, len); 2651da177e4SLinus Torvalds } 2661da177e4SLinus Torvalds 2671da177e4SLinus Torvalds break; 2681da177e4SLinus Torvalds 2691da177e4SLinus Torvalds case CAPI_FUNCTION_GET_VERSION: 270f4777569SMarcel Holtmann if (skb->len < CAPI_MSG_BASELEN + 32) 271f4777569SMarcel Holtmann break; 272f4777569SMarcel Holtmann 2731da177e4SLinus Torvalds controller = CAPIMSG_U32(skb->data, CAPI_MSG_BASELEN + 12); 2741da177e4SLinus Torvalds 2751da177e4SLinus Torvalds if (!info && ctrl) { 2761da177e4SLinus Torvalds ctrl->version.majorversion = CAPIMSG_U32(skb->data, CAPI_MSG_BASELEN + 16); 2771da177e4SLinus Torvalds ctrl->version.minorversion = CAPIMSG_U32(skb->data, CAPI_MSG_BASELEN + 20); 2781da177e4SLinus Torvalds ctrl->version.majormanuversion = CAPIMSG_U32(skb->data, CAPI_MSG_BASELEN + 24); 2791da177e4SLinus Torvalds ctrl->version.minormanuversion = CAPIMSG_U32(skb->data, CAPI_MSG_BASELEN + 28); 2801da177e4SLinus Torvalds } 2811da177e4SLinus Torvalds 2821da177e4SLinus Torvalds break; 2831da177e4SLinus Torvalds 2841da177e4SLinus Torvalds case CAPI_FUNCTION_GET_SERIAL_NUMBER: 285f4777569SMarcel Holtmann if (skb->len < CAPI_MSG_BASELEN + 17) 286f4777569SMarcel Holtmann break; 287f4777569SMarcel Holtmann 2881da177e4SLinus Torvalds controller = CAPIMSG_U32(skb->data, CAPI_MSG_BASELEN + 12); 2891da177e4SLinus Torvalds 2901da177e4SLinus Torvalds if (!info && ctrl) { 291f4777569SMarcel Holtmann int len = min_t(uint, CAPI_SERIAL_LEN, 292f4777569SMarcel Holtmann skb->data[CAPI_MSG_BASELEN + 16]); 293f4777569SMarcel Holtmann 2941da177e4SLinus Torvalds memset(ctrl->serial, 0, CAPI_SERIAL_LEN); 2951da177e4SLinus Torvalds strncpy(ctrl->serial, 296f4777569SMarcel Holtmann skb->data + CAPI_MSG_BASELEN + 17, len); 2971da177e4SLinus Torvalds } 2981da177e4SLinus Torvalds 2991da177e4SLinus Torvalds break; 3001da177e4SLinus Torvalds } 3011da177e4SLinus Torvalds 3021da177e4SLinus Torvalds break; 3031da177e4SLinus Torvalds 3041da177e4SLinus Torvalds case CAPI_IND: 305f4777569SMarcel Holtmann if (skb->len < CAPI_MSG_BASELEN + 6) 306f4777569SMarcel Holtmann break; 307f4777569SMarcel Holtmann 3081da177e4SLinus Torvalds func = CAPIMSG_U16(skb->data, CAPI_MSG_BASELEN + 3); 3091da177e4SLinus Torvalds 3101da177e4SLinus Torvalds if (func == CAPI_FUNCTION_LOOPBACK) { 311f4777569SMarcel Holtmann int len = min_t(uint, skb->len - CAPI_MSG_BASELEN - 6, 312f4777569SMarcel Holtmann skb->data[CAPI_MSG_BASELEN + 5]); 3131da177e4SLinus Torvalds appl = CAPIMSG_APPID(skb->data); 3141da177e4SLinus Torvalds msgnum = CAPIMSG_MSGID(skb->data); 3151da177e4SLinus Torvalds cmtp_send_interopmsg(session, CAPI_RESP, appl, msgnum, func, 316f4777569SMarcel Holtmann skb->data + CAPI_MSG_BASELEN + 6, len); 3171da177e4SLinus Torvalds } 3181da177e4SLinus Torvalds 3191da177e4SLinus Torvalds break; 3201da177e4SLinus Torvalds } 3211da177e4SLinus Torvalds 3221da177e4SLinus Torvalds kfree_skb(skb); 3231da177e4SLinus Torvalds } 3241da177e4SLinus Torvalds 3251da177e4SLinus Torvalds void cmtp_recv_capimsg(struct cmtp_session *session, struct sk_buff *skb) 3261da177e4SLinus Torvalds { 3271da177e4SLinus Torvalds struct capi_ctr *ctrl = &session->ctrl; 3281da177e4SLinus Torvalds struct cmtp_application *application; 3291da177e4SLinus Torvalds __u16 cmd, appl; 3301da177e4SLinus Torvalds __u32 contr; 3311da177e4SLinus Torvalds 3321da177e4SLinus Torvalds BT_DBG("session %p skb %p len %d", session, skb, skb->len); 3331da177e4SLinus Torvalds 334f4777569SMarcel Holtmann if (skb->len < CAPI_MSG_BASELEN) 335f4777569SMarcel Holtmann return; 336f4777569SMarcel Holtmann 3371da177e4SLinus Torvalds if (CAPIMSG_COMMAND(skb->data) == CAPI_INTEROPERABILITY) { 3381da177e4SLinus Torvalds cmtp_recv_interopmsg(session, skb); 3391da177e4SLinus Torvalds return; 3401da177e4SLinus Torvalds } 3411da177e4SLinus Torvalds 3421da177e4SLinus Torvalds if (session->flags & (1 << CMTP_LOOPBACK)) { 3431da177e4SLinus Torvalds kfree_skb(skb); 3441da177e4SLinus Torvalds return; 3451da177e4SLinus Torvalds } 3461da177e4SLinus Torvalds 3471da177e4SLinus Torvalds cmd = CAPICMD(CAPIMSG_COMMAND(skb->data), CAPIMSG_SUBCOMMAND(skb->data)); 3481da177e4SLinus Torvalds appl = CAPIMSG_APPID(skb->data); 3491da177e4SLinus Torvalds contr = CAPIMSG_CONTROL(skb->data); 3501da177e4SLinus Torvalds 3511da177e4SLinus Torvalds application = cmtp_application_get(session, CMTP_MAPPING, appl); 3521da177e4SLinus Torvalds if (application) { 3531da177e4SLinus Torvalds appl = application->appl; 3541da177e4SLinus Torvalds CAPIMSG_SETAPPID(skb->data, appl); 3551da177e4SLinus Torvalds } else { 3561da177e4SLinus Torvalds BT_ERR("Can't find application with id %d", appl); 3571da177e4SLinus Torvalds kfree_skb(skb); 3581da177e4SLinus Torvalds return; 3591da177e4SLinus Torvalds } 3601da177e4SLinus Torvalds 3611da177e4SLinus Torvalds if ((contr & 0x7f) == 0x01) { 3621da177e4SLinus Torvalds contr = (contr & 0xffffff80) | session->num; 3631da177e4SLinus Torvalds CAPIMSG_SETCONTROL(skb->data, contr); 3641da177e4SLinus Torvalds } 3651da177e4SLinus Torvalds 3661da177e4SLinus Torvalds if (!ctrl) { 3671da177e4SLinus Torvalds BT_ERR("Can't find controller %d for message", session->num); 3681da177e4SLinus Torvalds kfree_skb(skb); 3691da177e4SLinus Torvalds return; 3701da177e4SLinus Torvalds } 3711da177e4SLinus Torvalds 3721da177e4SLinus Torvalds capi_ctr_handle_message(ctrl, appl, skb); 3731da177e4SLinus Torvalds } 3741da177e4SLinus Torvalds 3751da177e4SLinus Torvalds static int cmtp_load_firmware(struct capi_ctr *ctrl, capiloaddata *data) 3761da177e4SLinus Torvalds { 3771da177e4SLinus Torvalds BT_DBG("ctrl %p data %p", ctrl, data); 3781da177e4SLinus Torvalds 3791da177e4SLinus Torvalds return 0; 3801da177e4SLinus Torvalds } 3811da177e4SLinus Torvalds 3821da177e4SLinus Torvalds static void cmtp_reset_ctr(struct capi_ctr *ctrl) 3831da177e4SLinus Torvalds { 3841da177e4SLinus Torvalds struct cmtp_session *session = ctrl->driverdata; 3851da177e4SLinus Torvalds 3861da177e4SLinus Torvalds BT_DBG("ctrl %p", ctrl); 3871da177e4SLinus Torvalds 3884e329972STilman Schmidt capi_ctr_down(ctrl); 3891da177e4SLinus Torvalds 390*fada4ac3SSzymon Janc kthread_stop(session->task); 3911da177e4SLinus Torvalds } 3921da177e4SLinus Torvalds 3931da177e4SLinus Torvalds static void cmtp_register_appl(struct capi_ctr *ctrl, __u16 appl, capi_register_params *rp) 3941da177e4SLinus Torvalds { 3951da177e4SLinus Torvalds DECLARE_WAITQUEUE(wait, current); 3961da177e4SLinus Torvalds struct cmtp_session *session = ctrl->driverdata; 3971da177e4SLinus Torvalds struct cmtp_application *application; 3981da177e4SLinus Torvalds unsigned long timeo = CMTP_INTEROP_TIMEOUT; 3991da177e4SLinus Torvalds unsigned char buf[8]; 4001da177e4SLinus Torvalds int err = 0, nconn, want = rp->level3cnt; 4011da177e4SLinus Torvalds 4021da177e4SLinus Torvalds BT_DBG("ctrl %p appl %d level3cnt %d datablkcnt %d datablklen %d", 4031da177e4SLinus Torvalds ctrl, appl, rp->level3cnt, rp->datablkcnt, rp->datablklen); 4041da177e4SLinus Torvalds 4051da177e4SLinus Torvalds application = cmtp_application_add(session, appl); 4061da177e4SLinus Torvalds if (!application) { 4071da177e4SLinus Torvalds BT_ERR("Can't allocate memory for new application"); 4081da177e4SLinus Torvalds return; 4091da177e4SLinus Torvalds } 4101da177e4SLinus Torvalds 4111da177e4SLinus Torvalds if (want < 0) 4121da177e4SLinus Torvalds nconn = ctrl->profile.nbchannel * -want; 4131da177e4SLinus Torvalds else 4141da177e4SLinus Torvalds nconn = want; 4151da177e4SLinus Torvalds 4161da177e4SLinus Torvalds if (nconn == 0) 4171da177e4SLinus Torvalds nconn = ctrl->profile.nbchannel; 4181da177e4SLinus Torvalds 4191da177e4SLinus Torvalds capimsg_setu16(buf, 0, nconn); 4201da177e4SLinus Torvalds capimsg_setu16(buf, 2, rp->datablkcnt); 4211da177e4SLinus Torvalds capimsg_setu16(buf, 4, rp->datablklen); 4221da177e4SLinus Torvalds 4231da177e4SLinus Torvalds application->state = BT_CONFIG; 4241da177e4SLinus Torvalds application->msgnum = cmtp_msgnum_get(session); 4251da177e4SLinus Torvalds 4261da177e4SLinus Torvalds cmtp_send_interopmsg(session, CAPI_REQ, 0x0000, application->msgnum, 4271da177e4SLinus Torvalds CAPI_FUNCTION_REGISTER, buf, 6); 4281da177e4SLinus Torvalds 4291da177e4SLinus Torvalds add_wait_queue(&session->wait, &wait); 4301da177e4SLinus Torvalds while (1) { 4311da177e4SLinus Torvalds set_current_state(TASK_INTERRUPTIBLE); 4321da177e4SLinus Torvalds 4331da177e4SLinus Torvalds if (!timeo) { 4341da177e4SLinus Torvalds err = -EAGAIN; 4351da177e4SLinus Torvalds break; 4361da177e4SLinus Torvalds } 4371da177e4SLinus Torvalds 4381da177e4SLinus Torvalds if (application->state == BT_CLOSED) { 4391da177e4SLinus Torvalds err = -application->err; 4401da177e4SLinus Torvalds break; 4411da177e4SLinus Torvalds } 4421da177e4SLinus Torvalds 4431da177e4SLinus Torvalds if (application->state == BT_CONNECTED) 4441da177e4SLinus Torvalds break; 4451da177e4SLinus Torvalds 4461da177e4SLinus Torvalds if (signal_pending(current)) { 4471da177e4SLinus Torvalds err = -EINTR; 4481da177e4SLinus Torvalds break; 4491da177e4SLinus Torvalds } 4501da177e4SLinus Torvalds 4511da177e4SLinus Torvalds timeo = schedule_timeout(timeo); 4521da177e4SLinus Torvalds } 4531da177e4SLinus Torvalds set_current_state(TASK_RUNNING); 4541da177e4SLinus Torvalds remove_wait_queue(&session->wait, &wait); 4551da177e4SLinus Torvalds 4561da177e4SLinus Torvalds if (err) { 4571da177e4SLinus Torvalds cmtp_application_del(session, application); 4581da177e4SLinus Torvalds return; 4591da177e4SLinus Torvalds } 4601da177e4SLinus Torvalds } 4611da177e4SLinus Torvalds 4621da177e4SLinus Torvalds static void cmtp_release_appl(struct capi_ctr *ctrl, __u16 appl) 4631da177e4SLinus Torvalds { 4641da177e4SLinus Torvalds struct cmtp_session *session = ctrl->driverdata; 4651da177e4SLinus Torvalds struct cmtp_application *application; 4661da177e4SLinus Torvalds 4671da177e4SLinus Torvalds BT_DBG("ctrl %p appl %d", ctrl, appl); 4681da177e4SLinus Torvalds 4691da177e4SLinus Torvalds application = cmtp_application_get(session, CMTP_APPLID, appl); 4701da177e4SLinus Torvalds if (!application) { 4711da177e4SLinus Torvalds BT_ERR("Can't find application"); 4721da177e4SLinus Torvalds return; 4731da177e4SLinus Torvalds } 4741da177e4SLinus Torvalds 4751da177e4SLinus Torvalds application->msgnum = cmtp_msgnum_get(session); 4761da177e4SLinus Torvalds 4771da177e4SLinus Torvalds cmtp_send_interopmsg(session, CAPI_REQ, application->mapping, application->msgnum, 4781da177e4SLinus Torvalds CAPI_FUNCTION_RELEASE, NULL, 0); 4791da177e4SLinus Torvalds 4801da177e4SLinus Torvalds wait_event_interruptible_timeout(session->wait, 4811da177e4SLinus Torvalds (application->state == BT_CLOSED), CMTP_INTEROP_TIMEOUT); 4821da177e4SLinus Torvalds 4831da177e4SLinus Torvalds cmtp_application_del(session, application); 4841da177e4SLinus Torvalds } 4851da177e4SLinus Torvalds 4861da177e4SLinus Torvalds static u16 cmtp_send_message(struct capi_ctr *ctrl, struct sk_buff *skb) 4871da177e4SLinus Torvalds { 4881da177e4SLinus Torvalds struct cmtp_session *session = ctrl->driverdata; 4891da177e4SLinus Torvalds struct cmtp_application *application; 4901da177e4SLinus Torvalds __u16 appl; 4911da177e4SLinus Torvalds __u32 contr; 4921da177e4SLinus Torvalds 4931da177e4SLinus Torvalds BT_DBG("ctrl %p skb %p", ctrl, skb); 4941da177e4SLinus Torvalds 4951da177e4SLinus Torvalds appl = CAPIMSG_APPID(skb->data); 4961da177e4SLinus Torvalds contr = CAPIMSG_CONTROL(skb->data); 4971da177e4SLinus Torvalds 4981da177e4SLinus Torvalds application = cmtp_application_get(session, CMTP_APPLID, appl); 4991da177e4SLinus Torvalds if ((!application) || (application->state != BT_CONNECTED)) { 5001da177e4SLinus Torvalds BT_ERR("Can't find application with id %d", appl); 5011da177e4SLinus Torvalds return CAPI_ILLAPPNR; 5021da177e4SLinus Torvalds } 5031da177e4SLinus Torvalds 5041da177e4SLinus Torvalds CAPIMSG_SETAPPID(skb->data, application->mapping); 5051da177e4SLinus Torvalds 5061da177e4SLinus Torvalds if ((contr & 0x7f) == session->num) { 5071da177e4SLinus Torvalds contr = (contr & 0xffffff80) | 0x01; 5081da177e4SLinus Torvalds CAPIMSG_SETCONTROL(skb->data, contr); 5091da177e4SLinus Torvalds } 5101da177e4SLinus Torvalds 5111da177e4SLinus Torvalds cmtp_send_capimsg(session, skb); 5121da177e4SLinus Torvalds 5131da177e4SLinus Torvalds return CAPI_NOERROR; 5141da177e4SLinus Torvalds } 5151da177e4SLinus Torvalds 5161da177e4SLinus Torvalds static char *cmtp_procinfo(struct capi_ctr *ctrl) 5171da177e4SLinus Torvalds { 5181da177e4SLinus Torvalds return "CAPI Message Transport Protocol"; 5191da177e4SLinus Torvalds } 5201da177e4SLinus Torvalds 5219a58a80aSAlexey Dobriyan static int cmtp_proc_show(struct seq_file *m, void *v) 5221da177e4SLinus Torvalds { 5239a58a80aSAlexey Dobriyan struct capi_ctr *ctrl = m->private; 5241da177e4SLinus Torvalds struct cmtp_session *session = ctrl->driverdata; 5251da177e4SLinus Torvalds struct cmtp_application *app; 5261da177e4SLinus Torvalds struct list_head *p, *n; 5271da177e4SLinus Torvalds 5289a58a80aSAlexey Dobriyan seq_printf(m, "%s\n\n", cmtp_procinfo(ctrl)); 5299a58a80aSAlexey Dobriyan seq_printf(m, "addr %s\n", session->name); 5309a58a80aSAlexey Dobriyan seq_printf(m, "ctrl %d\n", session->num); 5311da177e4SLinus Torvalds 5321da177e4SLinus Torvalds list_for_each_safe(p, n, &session->applications) { 5331da177e4SLinus Torvalds app = list_entry(p, struct cmtp_application, list); 5349a58a80aSAlexey Dobriyan seq_printf(m, "appl %d -> %d\n", app->appl, app->mapping); 5351da177e4SLinus Torvalds } 5361da177e4SLinus Torvalds 5371da177e4SLinus Torvalds return 0; 5381da177e4SLinus Torvalds } 5391da177e4SLinus Torvalds 5409a58a80aSAlexey Dobriyan static int cmtp_proc_open(struct inode *inode, struct file *file) 5419a58a80aSAlexey Dobriyan { 5429a58a80aSAlexey Dobriyan return single_open(file, cmtp_proc_show, PDE(inode)->data); 5439a58a80aSAlexey Dobriyan } 5449a58a80aSAlexey Dobriyan 5459a58a80aSAlexey Dobriyan static const struct file_operations cmtp_proc_fops = { 5469a58a80aSAlexey Dobriyan .owner = THIS_MODULE, 5479a58a80aSAlexey Dobriyan .open = cmtp_proc_open, 5489a58a80aSAlexey Dobriyan .read = seq_read, 5499a58a80aSAlexey Dobriyan .llseek = seq_lseek, 5509a58a80aSAlexey Dobriyan .release = single_release, 5519a58a80aSAlexey Dobriyan }; 5521da177e4SLinus Torvalds 5531da177e4SLinus Torvalds int cmtp_attach_device(struct cmtp_session *session) 5541da177e4SLinus Torvalds { 5551da177e4SLinus Torvalds unsigned char buf[4]; 5561da177e4SLinus Torvalds long ret; 5571da177e4SLinus Torvalds 5581da177e4SLinus Torvalds BT_DBG("session %p", session); 5591da177e4SLinus Torvalds 5601da177e4SLinus Torvalds capimsg_setu32(buf, 0, 0); 5611da177e4SLinus Torvalds 5621da177e4SLinus Torvalds cmtp_send_interopmsg(session, CAPI_REQ, 0xffff, CMTP_INITIAL_MSGNUM, 5631da177e4SLinus Torvalds CAPI_FUNCTION_GET_PROFILE, buf, 4); 5641da177e4SLinus Torvalds 5651da177e4SLinus Torvalds ret = wait_event_interruptible_timeout(session->wait, 5661da177e4SLinus Torvalds session->ncontroller, CMTP_INTEROP_TIMEOUT); 5671da177e4SLinus Torvalds 5681da177e4SLinus Torvalds BT_INFO("Found %d CAPI controller(s) on device %s", session->ncontroller, session->name); 5691da177e4SLinus Torvalds 5701da177e4SLinus Torvalds if (!ret) 5711da177e4SLinus Torvalds return -ETIMEDOUT; 5721da177e4SLinus Torvalds 5731da177e4SLinus Torvalds if (!session->ncontroller) 5741da177e4SLinus Torvalds return -ENODEV; 5751da177e4SLinus Torvalds 5761da177e4SLinus Torvalds if (session->ncontroller > 1) 5771da177e4SLinus Torvalds BT_INFO("Setting up only CAPI controller 1"); 5781da177e4SLinus Torvalds 5791da177e4SLinus Torvalds session->ctrl.owner = THIS_MODULE; 5801da177e4SLinus Torvalds session->ctrl.driverdata = session; 5811da177e4SLinus Torvalds strcpy(session->ctrl.name, session->name); 5821da177e4SLinus Torvalds 5831da177e4SLinus Torvalds session->ctrl.driver_name = "cmtp"; 5841da177e4SLinus Torvalds session->ctrl.load_firmware = cmtp_load_firmware; 5851da177e4SLinus Torvalds session->ctrl.reset_ctr = cmtp_reset_ctr; 5861da177e4SLinus Torvalds session->ctrl.register_appl = cmtp_register_appl; 5871da177e4SLinus Torvalds session->ctrl.release_appl = cmtp_release_appl; 5881da177e4SLinus Torvalds session->ctrl.send_message = cmtp_send_message; 5891da177e4SLinus Torvalds 5901da177e4SLinus Torvalds session->ctrl.procinfo = cmtp_procinfo; 5919a58a80aSAlexey Dobriyan session->ctrl.proc_fops = &cmtp_proc_fops; 5921da177e4SLinus Torvalds 5931da177e4SLinus Torvalds if (attach_capi_ctr(&session->ctrl) < 0) { 5941da177e4SLinus Torvalds BT_ERR("Can't attach new controller"); 5951da177e4SLinus Torvalds return -EBUSY; 5961da177e4SLinus Torvalds } 5971da177e4SLinus Torvalds 5981da177e4SLinus Torvalds session->num = session->ctrl.cnr; 5991da177e4SLinus Torvalds 6001da177e4SLinus Torvalds BT_DBG("session %p num %d", session, session->num); 6011da177e4SLinus Torvalds 6021da177e4SLinus Torvalds capimsg_setu32(buf, 0, 1); 6031da177e4SLinus Torvalds 6041da177e4SLinus Torvalds cmtp_send_interopmsg(session, CAPI_REQ, 0xffff, cmtp_msgnum_get(session), 6051da177e4SLinus Torvalds CAPI_FUNCTION_GET_MANUFACTURER, buf, 4); 6061da177e4SLinus Torvalds 6071da177e4SLinus Torvalds cmtp_send_interopmsg(session, CAPI_REQ, 0xffff, cmtp_msgnum_get(session), 6081da177e4SLinus Torvalds CAPI_FUNCTION_GET_VERSION, buf, 4); 6091da177e4SLinus Torvalds 6101da177e4SLinus Torvalds cmtp_send_interopmsg(session, CAPI_REQ, 0xffff, cmtp_msgnum_get(session), 6111da177e4SLinus Torvalds CAPI_FUNCTION_GET_SERIAL_NUMBER, buf, 4); 6121da177e4SLinus Torvalds 6131da177e4SLinus Torvalds cmtp_send_interopmsg(session, CAPI_REQ, 0xffff, cmtp_msgnum_get(session), 6141da177e4SLinus Torvalds CAPI_FUNCTION_GET_PROFILE, buf, 4); 6151da177e4SLinus Torvalds 6161da177e4SLinus Torvalds return 0; 6171da177e4SLinus Torvalds } 6181da177e4SLinus Torvalds 6191da177e4SLinus Torvalds void cmtp_detach_device(struct cmtp_session *session) 6201da177e4SLinus Torvalds { 6211da177e4SLinus Torvalds BT_DBG("session %p", session); 6221da177e4SLinus Torvalds 6231da177e4SLinus Torvalds detach_capi_ctr(&session->ctrl); 6241da177e4SLinus Torvalds } 625