xref: /linux/net/bluetooth/cmtp/capi.c (revision f4777569204cb59f2f04fbe9ef4e9a6918209104)
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>
241da177e4SLinus Torvalds 
251da177e4SLinus Torvalds #include <linux/types.h>
261da177e4SLinus Torvalds #include <linux/errno.h>
271da177e4SLinus Torvalds #include <linux/kernel.h>
281da177e4SLinus Torvalds #include <linux/sched.h>
291da177e4SLinus Torvalds #include <linux/slab.h>
301da177e4SLinus Torvalds #include <linux/poll.h>
311da177e4SLinus Torvalds #include <linux/fcntl.h>
321da177e4SLinus Torvalds #include <linux/skbuff.h>
331da177e4SLinus Torvalds #include <linux/socket.h>
341da177e4SLinus Torvalds #include <linux/ioctl.h>
351da177e4SLinus Torvalds #include <linux/file.h>
361da177e4SLinus Torvalds #include <linux/wait.h>
371da177e4SLinus Torvalds #include <net/sock.h>
381da177e4SLinus Torvalds 
391da177e4SLinus Torvalds #include <linux/isdn/capilli.h>
401da177e4SLinus Torvalds #include <linux/isdn/capicmd.h>
411da177e4SLinus Torvalds #include <linux/isdn/capiutil.h>
421da177e4SLinus Torvalds 
431da177e4SLinus Torvalds #include "cmtp.h"
441da177e4SLinus Torvalds 
451da177e4SLinus Torvalds #ifndef CONFIG_BT_CMTP_DEBUG
461da177e4SLinus Torvalds #undef  BT_DBG
471da177e4SLinus Torvalds #define BT_DBG(D...)
481da177e4SLinus Torvalds #endif
491da177e4SLinus Torvalds 
501da177e4SLinus Torvalds #define CAPI_INTEROPERABILITY		0x20
511da177e4SLinus Torvalds 
521da177e4SLinus Torvalds #define CAPI_INTEROPERABILITY_REQ	CAPICMD(CAPI_INTEROPERABILITY, CAPI_REQ)
531da177e4SLinus Torvalds #define CAPI_INTEROPERABILITY_CONF	CAPICMD(CAPI_INTEROPERABILITY, CAPI_CONF)
541da177e4SLinus Torvalds #define CAPI_INTEROPERABILITY_IND	CAPICMD(CAPI_INTEROPERABILITY, CAPI_IND)
551da177e4SLinus Torvalds #define CAPI_INTEROPERABILITY_RESP	CAPICMD(CAPI_INTEROPERABILITY, CAPI_RESP)
561da177e4SLinus Torvalds 
571da177e4SLinus Torvalds #define CAPI_INTEROPERABILITY_REQ_LEN	(CAPI_MSG_BASELEN + 2)
581da177e4SLinus Torvalds #define CAPI_INTEROPERABILITY_CONF_LEN	(CAPI_MSG_BASELEN + 4)
591da177e4SLinus Torvalds #define CAPI_INTEROPERABILITY_IND_LEN	(CAPI_MSG_BASELEN + 2)
601da177e4SLinus Torvalds #define CAPI_INTEROPERABILITY_RESP_LEN	(CAPI_MSG_BASELEN + 2)
611da177e4SLinus Torvalds 
621da177e4SLinus Torvalds #define CAPI_FUNCTION_REGISTER		0
631da177e4SLinus Torvalds #define CAPI_FUNCTION_RELEASE		1
641da177e4SLinus Torvalds #define CAPI_FUNCTION_GET_PROFILE	2
651da177e4SLinus Torvalds #define CAPI_FUNCTION_GET_MANUFACTURER	3
661da177e4SLinus Torvalds #define CAPI_FUNCTION_GET_VERSION	4
671da177e4SLinus Torvalds #define CAPI_FUNCTION_GET_SERIAL_NUMBER	5
681da177e4SLinus Torvalds #define CAPI_FUNCTION_MANUFACTURER	6
691da177e4SLinus Torvalds #define CAPI_FUNCTION_LOOPBACK		7
701da177e4SLinus Torvalds 
711da177e4SLinus Torvalds 
721da177e4SLinus Torvalds #define CMTP_MSGNUM	1
731da177e4SLinus Torvalds #define CMTP_APPLID	2
741da177e4SLinus Torvalds #define CMTP_MAPPING	3
751da177e4SLinus Torvalds 
761da177e4SLinus Torvalds static struct cmtp_application *cmtp_application_add(struct cmtp_session *session, __u16 appl)
771da177e4SLinus Torvalds {
7825ea6db0SMarcel Holtmann 	struct cmtp_application *app = kzalloc(sizeof(*app), GFP_KERNEL);
791da177e4SLinus Torvalds 
801da177e4SLinus Torvalds 	BT_DBG("session %p application %p appl %d", session, app, appl);
811da177e4SLinus Torvalds 
821da177e4SLinus Torvalds 	if (!app)
831da177e4SLinus Torvalds 		return NULL;
841da177e4SLinus Torvalds 
851da177e4SLinus Torvalds 	app->state = BT_OPEN;
861da177e4SLinus Torvalds 	app->appl = appl;
871da177e4SLinus Torvalds 
881da177e4SLinus Torvalds 	list_add_tail(&app->list, &session->applications);
891da177e4SLinus Torvalds 
901da177e4SLinus Torvalds 	return app;
911da177e4SLinus Torvalds }
921da177e4SLinus Torvalds 
931da177e4SLinus Torvalds static void cmtp_application_del(struct cmtp_session *session, struct cmtp_application *app)
941da177e4SLinus Torvalds {
951da177e4SLinus Torvalds 	BT_DBG("session %p application %p", session, app);
961da177e4SLinus Torvalds 
971da177e4SLinus Torvalds 	if (app) {
981da177e4SLinus Torvalds 		list_del(&app->list);
991da177e4SLinus Torvalds 		kfree(app);
1001da177e4SLinus Torvalds 	}
1011da177e4SLinus Torvalds }
1021da177e4SLinus Torvalds 
1031da177e4SLinus Torvalds static struct cmtp_application *cmtp_application_get(struct cmtp_session *session, int pattern, __u16 value)
1041da177e4SLinus Torvalds {
1051da177e4SLinus Torvalds 	struct cmtp_application *app;
1061da177e4SLinus Torvalds 	struct list_head *p, *n;
1071da177e4SLinus Torvalds 
1081da177e4SLinus Torvalds 	list_for_each_safe(p, n, &session->applications) {
1091da177e4SLinus Torvalds 		app = list_entry(p, struct cmtp_application, list);
1101da177e4SLinus Torvalds 		switch (pattern) {
1111da177e4SLinus Torvalds 		case CMTP_MSGNUM:
1121da177e4SLinus Torvalds 			if (app->msgnum == value)
1131da177e4SLinus Torvalds 				return app;
1141da177e4SLinus Torvalds 			break;
1151da177e4SLinus Torvalds 		case CMTP_APPLID:
1161da177e4SLinus Torvalds 			if (app->appl == value)
1171da177e4SLinus Torvalds 				return app;
1181da177e4SLinus Torvalds 			break;
1191da177e4SLinus Torvalds 		case CMTP_MAPPING:
1201da177e4SLinus Torvalds 			if (app->mapping == value)
1211da177e4SLinus Torvalds 				return app;
1221da177e4SLinus Torvalds 			break;
1231da177e4SLinus Torvalds 		}
1241da177e4SLinus Torvalds 	}
1251da177e4SLinus Torvalds 
1261da177e4SLinus Torvalds 	return NULL;
1271da177e4SLinus Torvalds }
1281da177e4SLinus Torvalds 
1291da177e4SLinus Torvalds static int cmtp_msgnum_get(struct cmtp_session *session)
1301da177e4SLinus Torvalds {
1311da177e4SLinus Torvalds 	session->msgnum++;
1321da177e4SLinus Torvalds 
1331da177e4SLinus Torvalds 	if ((session->msgnum & 0xff) > 200)
1341da177e4SLinus Torvalds 		session->msgnum = CMTP_INITIAL_MSGNUM + 1;
1351da177e4SLinus Torvalds 
1361da177e4SLinus Torvalds 	return session->msgnum;
1371da177e4SLinus Torvalds }
1381da177e4SLinus Torvalds 
1391da177e4SLinus Torvalds static void cmtp_send_capimsg(struct cmtp_session *session, struct sk_buff *skb)
1401da177e4SLinus Torvalds {
1411da177e4SLinus Torvalds 	struct cmtp_scb *scb = (void *) skb->cb;
1421da177e4SLinus Torvalds 
1431da177e4SLinus Torvalds 	BT_DBG("session %p skb %p len %d", session, skb, skb->len);
1441da177e4SLinus Torvalds 
1451da177e4SLinus Torvalds 	scb->id = -1;
1461da177e4SLinus Torvalds 	scb->data = (CAPIMSG_COMMAND(skb->data) == CAPI_DATA_B3);
1471da177e4SLinus Torvalds 
1481da177e4SLinus Torvalds 	skb_queue_tail(&session->transmit, skb);
1491da177e4SLinus Torvalds 
1501da177e4SLinus Torvalds 	cmtp_schedule(session);
1511da177e4SLinus Torvalds }
1521da177e4SLinus Torvalds 
1531da177e4SLinus Torvalds static void cmtp_send_interopmsg(struct cmtp_session *session,
1541da177e4SLinus Torvalds 					__u8 subcmd, __u16 appl, __u16 msgnum,
1551da177e4SLinus Torvalds 					__u16 function, unsigned char *buf, int len)
1561da177e4SLinus Torvalds {
1571da177e4SLinus Torvalds 	struct sk_buff *skb;
1581da177e4SLinus Torvalds 	unsigned char *s;
1591da177e4SLinus Torvalds 
1601da177e4SLinus Torvalds 	BT_DBG("session %p subcmd 0x%02x appl %d msgnum %d", session, subcmd, appl, msgnum);
1611da177e4SLinus Torvalds 
1621da177e4SLinus Torvalds 	if (!(skb = alloc_skb(CAPI_MSG_BASELEN + 6 + len, GFP_ATOMIC))) {
1631da177e4SLinus Torvalds 		BT_ERR("Can't allocate memory for interoperability packet");
1641da177e4SLinus Torvalds 		return;
1651da177e4SLinus Torvalds 	}
1661da177e4SLinus Torvalds 
1671da177e4SLinus Torvalds 	s = skb_put(skb, CAPI_MSG_BASELEN + 6 + len);
1681da177e4SLinus Torvalds 
1691da177e4SLinus Torvalds 	capimsg_setu16(s, 0, CAPI_MSG_BASELEN + 6 + len);
1701da177e4SLinus Torvalds 	capimsg_setu16(s, 2, appl);
1711da177e4SLinus Torvalds 	capimsg_setu8 (s, 4, CAPI_INTEROPERABILITY);
1721da177e4SLinus Torvalds 	capimsg_setu8 (s, 5, subcmd);
1731da177e4SLinus Torvalds 	capimsg_setu16(s, 6, msgnum);
1741da177e4SLinus Torvalds 
1751da177e4SLinus Torvalds 	/* Interoperability selector (Bluetooth Device Management) */
1761da177e4SLinus Torvalds 	capimsg_setu16(s, 8, 0x0001);
1771da177e4SLinus Torvalds 
1781da177e4SLinus Torvalds 	capimsg_setu8 (s, 10, 3 + len);
1791da177e4SLinus Torvalds 	capimsg_setu16(s, 11, function);
1801da177e4SLinus Torvalds 	capimsg_setu8 (s, 13, len);
1811da177e4SLinus Torvalds 
1821da177e4SLinus Torvalds 	if (len > 0)
1831da177e4SLinus Torvalds 		memcpy(s + 14, buf, len);
1841da177e4SLinus Torvalds 
1851da177e4SLinus Torvalds 	cmtp_send_capimsg(session, skb);
1861da177e4SLinus Torvalds }
1871da177e4SLinus Torvalds 
1881da177e4SLinus Torvalds static void cmtp_recv_interopmsg(struct cmtp_session *session, struct sk_buff *skb)
1891da177e4SLinus Torvalds {
1901da177e4SLinus Torvalds 	struct capi_ctr *ctrl = &session->ctrl;
1911da177e4SLinus Torvalds 	struct cmtp_application *application;
1921da177e4SLinus Torvalds 	__u16 appl, msgnum, func, info;
1931da177e4SLinus Torvalds 	__u32 controller;
1941da177e4SLinus Torvalds 
1951da177e4SLinus Torvalds 	BT_DBG("session %p skb %p len %d", session, skb, skb->len);
1961da177e4SLinus Torvalds 
1971da177e4SLinus Torvalds 	switch (CAPIMSG_SUBCOMMAND(skb->data)) {
1981da177e4SLinus Torvalds 	case CAPI_CONF:
199*f4777569SMarcel Holtmann 		if (skb->len < CAPI_MSG_BASELEN + 10)
200*f4777569SMarcel Holtmann 			break;
201*f4777569SMarcel Holtmann 
2021da177e4SLinus Torvalds 		func = CAPIMSG_U16(skb->data, CAPI_MSG_BASELEN + 5);
2031da177e4SLinus Torvalds 		info = CAPIMSG_U16(skb->data, CAPI_MSG_BASELEN + 8);
2041da177e4SLinus Torvalds 
2051da177e4SLinus Torvalds 		switch (func) {
2061da177e4SLinus Torvalds 		case CAPI_FUNCTION_REGISTER:
2071da177e4SLinus Torvalds 			msgnum = CAPIMSG_MSGID(skb->data);
2081da177e4SLinus Torvalds 
2091da177e4SLinus Torvalds 			application = cmtp_application_get(session, CMTP_MSGNUM, msgnum);
2101da177e4SLinus Torvalds 			if (application) {
2111da177e4SLinus Torvalds 				application->state = BT_CONNECTED;
2121da177e4SLinus Torvalds 				application->msgnum = 0;
2131da177e4SLinus Torvalds 				application->mapping = CAPIMSG_APPID(skb->data);
2141da177e4SLinus Torvalds 				wake_up_interruptible(&session->wait);
2151da177e4SLinus Torvalds 			}
2161da177e4SLinus Torvalds 
2171da177e4SLinus Torvalds 			break;
2181da177e4SLinus Torvalds 
2191da177e4SLinus Torvalds 		case CAPI_FUNCTION_RELEASE:
2201da177e4SLinus Torvalds 			appl = CAPIMSG_APPID(skb->data);
2211da177e4SLinus Torvalds 
2221da177e4SLinus Torvalds 			application = cmtp_application_get(session, CMTP_MAPPING, appl);
2231da177e4SLinus Torvalds 			if (application) {
2241da177e4SLinus Torvalds 				application->state = BT_CLOSED;
2251da177e4SLinus Torvalds 				application->msgnum = 0;
2261da177e4SLinus Torvalds 				wake_up_interruptible(&session->wait);
2271da177e4SLinus Torvalds 			}
2281da177e4SLinus Torvalds 
2291da177e4SLinus Torvalds 			break;
2301da177e4SLinus Torvalds 
2311da177e4SLinus Torvalds 		case CAPI_FUNCTION_GET_PROFILE:
232*f4777569SMarcel Holtmann 			if (skb->len < CAPI_MSG_BASELEN + 11 + sizeof(capi_profile))
233*f4777569SMarcel Holtmann 				break;
234*f4777569SMarcel Holtmann 
2351da177e4SLinus Torvalds 			controller = CAPIMSG_U16(skb->data, CAPI_MSG_BASELEN + 11);
2361da177e4SLinus Torvalds 			msgnum = CAPIMSG_MSGID(skb->data);
2371da177e4SLinus Torvalds 
2381da177e4SLinus Torvalds 			if (!info && (msgnum == CMTP_INITIAL_MSGNUM)) {
2391da177e4SLinus Torvalds 				session->ncontroller = controller;
2401da177e4SLinus Torvalds 				wake_up_interruptible(&session->wait);
2411da177e4SLinus Torvalds 				break;
2421da177e4SLinus Torvalds 			}
2431da177e4SLinus Torvalds 
2441da177e4SLinus Torvalds 			if (!info && ctrl) {
2451da177e4SLinus Torvalds 				memcpy(&ctrl->profile,
2461da177e4SLinus Torvalds 					skb->data + CAPI_MSG_BASELEN + 11,
2471da177e4SLinus Torvalds 					sizeof(capi_profile));
2481da177e4SLinus Torvalds 				session->state = BT_CONNECTED;
2491da177e4SLinus Torvalds 				capi_ctr_ready(ctrl);
2501da177e4SLinus Torvalds 			}
2511da177e4SLinus Torvalds 
2521da177e4SLinus Torvalds 			break;
2531da177e4SLinus Torvalds 
2541da177e4SLinus Torvalds 		case CAPI_FUNCTION_GET_MANUFACTURER:
255*f4777569SMarcel Holtmann 			if (skb->len < CAPI_MSG_BASELEN + 15)
256*f4777569SMarcel Holtmann 				break;
257*f4777569SMarcel Holtmann 
2581da177e4SLinus Torvalds 			controller = CAPIMSG_U32(skb->data, CAPI_MSG_BASELEN + 10);
2591da177e4SLinus Torvalds 
2601da177e4SLinus Torvalds 			if (!info && ctrl) {
261*f4777569SMarcel Holtmann 				int len = min_t(uint, CAPI_MANUFACTURER_LEN,
2621da177e4SLinus Torvalds 						skb->data[CAPI_MSG_BASELEN + 14]);
263*f4777569SMarcel Holtmann 
264*f4777569SMarcel Holtmann 				memset(ctrl->manu, 0, CAPI_MANUFACTURER_LEN);
265*f4777569SMarcel Holtmann 				strncpy(ctrl->manu,
266*f4777569SMarcel Holtmann 					skb->data + CAPI_MSG_BASELEN + 15, len);
2671da177e4SLinus Torvalds 			}
2681da177e4SLinus Torvalds 
2691da177e4SLinus Torvalds 			break;
2701da177e4SLinus Torvalds 
2711da177e4SLinus Torvalds 		case CAPI_FUNCTION_GET_VERSION:
272*f4777569SMarcel Holtmann 			if (skb->len < CAPI_MSG_BASELEN + 32)
273*f4777569SMarcel Holtmann 				break;
274*f4777569SMarcel Holtmann 
2751da177e4SLinus Torvalds 			controller = CAPIMSG_U32(skb->data, CAPI_MSG_BASELEN + 12);
2761da177e4SLinus Torvalds 
2771da177e4SLinus Torvalds 			if (!info && ctrl) {
2781da177e4SLinus Torvalds 				ctrl->version.majorversion = CAPIMSG_U32(skb->data, CAPI_MSG_BASELEN + 16);
2791da177e4SLinus Torvalds 				ctrl->version.minorversion = CAPIMSG_U32(skb->data, CAPI_MSG_BASELEN + 20);
2801da177e4SLinus Torvalds 				ctrl->version.majormanuversion = CAPIMSG_U32(skb->data, CAPI_MSG_BASELEN + 24);
2811da177e4SLinus Torvalds 				ctrl->version.minormanuversion = CAPIMSG_U32(skb->data, CAPI_MSG_BASELEN + 28);
2821da177e4SLinus Torvalds 			}
2831da177e4SLinus Torvalds 
2841da177e4SLinus Torvalds 			break;
2851da177e4SLinus Torvalds 
2861da177e4SLinus Torvalds 		case CAPI_FUNCTION_GET_SERIAL_NUMBER:
287*f4777569SMarcel Holtmann 			if (skb->len < CAPI_MSG_BASELEN + 17)
288*f4777569SMarcel Holtmann 				break;
289*f4777569SMarcel Holtmann 
2901da177e4SLinus Torvalds 			controller = CAPIMSG_U32(skb->data, CAPI_MSG_BASELEN + 12);
2911da177e4SLinus Torvalds 
2921da177e4SLinus Torvalds 			if (!info && ctrl) {
293*f4777569SMarcel Holtmann 				int len = min_t(uint, CAPI_SERIAL_LEN,
294*f4777569SMarcel Holtmann 						skb->data[CAPI_MSG_BASELEN + 16]);
295*f4777569SMarcel Holtmann 
2961da177e4SLinus Torvalds 				memset(ctrl->serial, 0, CAPI_SERIAL_LEN);
2971da177e4SLinus Torvalds 				strncpy(ctrl->serial,
298*f4777569SMarcel Holtmann 					skb->data + CAPI_MSG_BASELEN + 17, len);
2991da177e4SLinus Torvalds 			}
3001da177e4SLinus Torvalds 
3011da177e4SLinus Torvalds 			break;
3021da177e4SLinus Torvalds 		}
3031da177e4SLinus Torvalds 
3041da177e4SLinus Torvalds 		break;
3051da177e4SLinus Torvalds 
3061da177e4SLinus Torvalds 	case CAPI_IND:
307*f4777569SMarcel Holtmann 		if (skb->len < CAPI_MSG_BASELEN + 6)
308*f4777569SMarcel Holtmann 			break;
309*f4777569SMarcel Holtmann 
3101da177e4SLinus Torvalds 		func = CAPIMSG_U16(skb->data, CAPI_MSG_BASELEN + 3);
3111da177e4SLinus Torvalds 
3121da177e4SLinus Torvalds 		if (func == CAPI_FUNCTION_LOOPBACK) {
313*f4777569SMarcel Holtmann 			int len = min_t(uint, skb->len - CAPI_MSG_BASELEN - 6,
314*f4777569SMarcel Holtmann 						skb->data[CAPI_MSG_BASELEN + 5]);
3151da177e4SLinus Torvalds 			appl = CAPIMSG_APPID(skb->data);
3161da177e4SLinus Torvalds 			msgnum = CAPIMSG_MSGID(skb->data);
3171da177e4SLinus Torvalds 			cmtp_send_interopmsg(session, CAPI_RESP, appl, msgnum, func,
318*f4777569SMarcel Holtmann 						skb->data + CAPI_MSG_BASELEN + 6, len);
3191da177e4SLinus Torvalds 		}
3201da177e4SLinus Torvalds 
3211da177e4SLinus Torvalds 		break;
3221da177e4SLinus Torvalds 	}
3231da177e4SLinus Torvalds 
3241da177e4SLinus Torvalds 	kfree_skb(skb);
3251da177e4SLinus Torvalds }
3261da177e4SLinus Torvalds 
3271da177e4SLinus Torvalds void cmtp_recv_capimsg(struct cmtp_session *session, struct sk_buff *skb)
3281da177e4SLinus Torvalds {
3291da177e4SLinus Torvalds 	struct capi_ctr *ctrl = &session->ctrl;
3301da177e4SLinus Torvalds 	struct cmtp_application *application;
3311da177e4SLinus Torvalds 	__u16 cmd, appl;
3321da177e4SLinus Torvalds 	__u32 contr;
3331da177e4SLinus Torvalds 
3341da177e4SLinus Torvalds 	BT_DBG("session %p skb %p len %d", session, skb, skb->len);
3351da177e4SLinus Torvalds 
336*f4777569SMarcel Holtmann 	if (skb->len < CAPI_MSG_BASELEN)
337*f4777569SMarcel Holtmann 		return;
338*f4777569SMarcel Holtmann 
3391da177e4SLinus Torvalds 	if (CAPIMSG_COMMAND(skb->data) == CAPI_INTEROPERABILITY) {
3401da177e4SLinus Torvalds 		cmtp_recv_interopmsg(session, skb);
3411da177e4SLinus Torvalds 		return;
3421da177e4SLinus Torvalds 	}
3431da177e4SLinus Torvalds 
3441da177e4SLinus Torvalds 	if (session->flags & (1 << CMTP_LOOPBACK)) {
3451da177e4SLinus Torvalds 		kfree_skb(skb);
3461da177e4SLinus Torvalds 		return;
3471da177e4SLinus Torvalds 	}
3481da177e4SLinus Torvalds 
3491da177e4SLinus Torvalds 	cmd = CAPICMD(CAPIMSG_COMMAND(skb->data), CAPIMSG_SUBCOMMAND(skb->data));
3501da177e4SLinus Torvalds 	appl = CAPIMSG_APPID(skb->data);
3511da177e4SLinus Torvalds 	contr = CAPIMSG_CONTROL(skb->data);
3521da177e4SLinus Torvalds 
3531da177e4SLinus Torvalds 	application = cmtp_application_get(session, CMTP_MAPPING, appl);
3541da177e4SLinus Torvalds 	if (application) {
3551da177e4SLinus Torvalds 		appl = application->appl;
3561da177e4SLinus Torvalds 		CAPIMSG_SETAPPID(skb->data, appl);
3571da177e4SLinus Torvalds 	} else {
3581da177e4SLinus Torvalds 		BT_ERR("Can't find application with id %d", appl);
3591da177e4SLinus Torvalds 		kfree_skb(skb);
3601da177e4SLinus Torvalds 		return;
3611da177e4SLinus Torvalds 	}
3621da177e4SLinus Torvalds 
3631da177e4SLinus Torvalds 	if ((contr & 0x7f) == 0x01) {
3641da177e4SLinus Torvalds 		contr = (contr & 0xffffff80) | session->num;
3651da177e4SLinus Torvalds 		CAPIMSG_SETCONTROL(skb->data, contr);
3661da177e4SLinus Torvalds 	}
3671da177e4SLinus Torvalds 
3681da177e4SLinus Torvalds 	if (!ctrl) {
3691da177e4SLinus Torvalds 		BT_ERR("Can't find controller %d for message", session->num);
3701da177e4SLinus Torvalds 		kfree_skb(skb);
3711da177e4SLinus Torvalds 		return;
3721da177e4SLinus Torvalds 	}
3731da177e4SLinus Torvalds 
3741da177e4SLinus Torvalds 	capi_ctr_handle_message(ctrl, appl, skb);
3751da177e4SLinus Torvalds }
3761da177e4SLinus Torvalds 
3771da177e4SLinus Torvalds static int cmtp_load_firmware(struct capi_ctr *ctrl, capiloaddata *data)
3781da177e4SLinus Torvalds {
3791da177e4SLinus Torvalds 	BT_DBG("ctrl %p data %p", ctrl, data);
3801da177e4SLinus Torvalds 
3811da177e4SLinus Torvalds 	return 0;
3821da177e4SLinus Torvalds }
3831da177e4SLinus Torvalds 
3841da177e4SLinus Torvalds static void cmtp_reset_ctr(struct capi_ctr *ctrl)
3851da177e4SLinus Torvalds {
3861da177e4SLinus Torvalds 	struct cmtp_session *session = ctrl->driverdata;
3871da177e4SLinus Torvalds 
3881da177e4SLinus Torvalds 	BT_DBG("ctrl %p", ctrl);
3891da177e4SLinus Torvalds 
3901da177e4SLinus Torvalds 	capi_ctr_reseted(ctrl);
3911da177e4SLinus Torvalds 
3921da177e4SLinus Torvalds 	atomic_inc(&session->terminate);
3931da177e4SLinus Torvalds 	cmtp_schedule(session);
3941da177e4SLinus Torvalds }
3951da177e4SLinus Torvalds 
3961da177e4SLinus Torvalds static void cmtp_register_appl(struct capi_ctr *ctrl, __u16 appl, capi_register_params *rp)
3971da177e4SLinus Torvalds {
3981da177e4SLinus Torvalds 	DECLARE_WAITQUEUE(wait, current);
3991da177e4SLinus Torvalds 	struct cmtp_session *session = ctrl->driverdata;
4001da177e4SLinus Torvalds 	struct cmtp_application *application;
4011da177e4SLinus Torvalds 	unsigned long timeo = CMTP_INTEROP_TIMEOUT;
4021da177e4SLinus Torvalds 	unsigned char buf[8];
4031da177e4SLinus Torvalds 	int err = 0, nconn, want = rp->level3cnt;
4041da177e4SLinus Torvalds 
4051da177e4SLinus Torvalds 	BT_DBG("ctrl %p appl %d level3cnt %d datablkcnt %d datablklen %d",
4061da177e4SLinus Torvalds 		ctrl, appl, rp->level3cnt, rp->datablkcnt, rp->datablklen);
4071da177e4SLinus Torvalds 
4081da177e4SLinus Torvalds 	application = cmtp_application_add(session, appl);
4091da177e4SLinus Torvalds 	if (!application) {
4101da177e4SLinus Torvalds 		BT_ERR("Can't allocate memory for new application");
4111da177e4SLinus Torvalds 		return;
4121da177e4SLinus Torvalds 	}
4131da177e4SLinus Torvalds 
4141da177e4SLinus Torvalds 	if (want < 0)
4151da177e4SLinus Torvalds 		nconn = ctrl->profile.nbchannel * -want;
4161da177e4SLinus Torvalds 	else
4171da177e4SLinus Torvalds 		nconn = want;
4181da177e4SLinus Torvalds 
4191da177e4SLinus Torvalds 	if (nconn == 0)
4201da177e4SLinus Torvalds 		nconn = ctrl->profile.nbchannel;
4211da177e4SLinus Torvalds 
4221da177e4SLinus Torvalds 	capimsg_setu16(buf, 0, nconn);
4231da177e4SLinus Torvalds 	capimsg_setu16(buf, 2, rp->datablkcnt);
4241da177e4SLinus Torvalds 	capimsg_setu16(buf, 4, rp->datablklen);
4251da177e4SLinus Torvalds 
4261da177e4SLinus Torvalds 	application->state = BT_CONFIG;
4271da177e4SLinus Torvalds 	application->msgnum = cmtp_msgnum_get(session);
4281da177e4SLinus Torvalds 
4291da177e4SLinus Torvalds 	cmtp_send_interopmsg(session, CAPI_REQ, 0x0000, application->msgnum,
4301da177e4SLinus Torvalds 				CAPI_FUNCTION_REGISTER, buf, 6);
4311da177e4SLinus Torvalds 
4321da177e4SLinus Torvalds 	add_wait_queue(&session->wait, &wait);
4331da177e4SLinus Torvalds 	while (1) {
4341da177e4SLinus Torvalds 		set_current_state(TASK_INTERRUPTIBLE);
4351da177e4SLinus Torvalds 
4361da177e4SLinus Torvalds 		if (!timeo) {
4371da177e4SLinus Torvalds 			err = -EAGAIN;
4381da177e4SLinus Torvalds 			break;
4391da177e4SLinus Torvalds 		}
4401da177e4SLinus Torvalds 
4411da177e4SLinus Torvalds 		if (application->state == BT_CLOSED) {
4421da177e4SLinus Torvalds 			err = -application->err;
4431da177e4SLinus Torvalds 			break;
4441da177e4SLinus Torvalds 		}
4451da177e4SLinus Torvalds 
4461da177e4SLinus Torvalds 		if (application->state == BT_CONNECTED)
4471da177e4SLinus Torvalds 			break;
4481da177e4SLinus Torvalds 
4491da177e4SLinus Torvalds 		if (signal_pending(current)) {
4501da177e4SLinus Torvalds 			err = -EINTR;
4511da177e4SLinus Torvalds 			break;
4521da177e4SLinus Torvalds 		}
4531da177e4SLinus Torvalds 
4541da177e4SLinus Torvalds 		timeo = schedule_timeout(timeo);
4551da177e4SLinus Torvalds 	}
4561da177e4SLinus Torvalds 	set_current_state(TASK_RUNNING);
4571da177e4SLinus Torvalds 	remove_wait_queue(&session->wait, &wait);
4581da177e4SLinus Torvalds 
4591da177e4SLinus Torvalds 	if (err) {
4601da177e4SLinus Torvalds 		cmtp_application_del(session, application);
4611da177e4SLinus Torvalds 		return;
4621da177e4SLinus Torvalds 	}
4631da177e4SLinus Torvalds }
4641da177e4SLinus Torvalds 
4651da177e4SLinus Torvalds static void cmtp_release_appl(struct capi_ctr *ctrl, __u16 appl)
4661da177e4SLinus Torvalds {
4671da177e4SLinus Torvalds 	struct cmtp_session *session = ctrl->driverdata;
4681da177e4SLinus Torvalds 	struct cmtp_application *application;
4691da177e4SLinus Torvalds 
4701da177e4SLinus Torvalds 	BT_DBG("ctrl %p appl %d", ctrl, appl);
4711da177e4SLinus Torvalds 
4721da177e4SLinus Torvalds 	application = cmtp_application_get(session, CMTP_APPLID, appl);
4731da177e4SLinus Torvalds 	if (!application) {
4741da177e4SLinus Torvalds 		BT_ERR("Can't find application");
4751da177e4SLinus Torvalds 		return;
4761da177e4SLinus Torvalds 	}
4771da177e4SLinus Torvalds 
4781da177e4SLinus Torvalds 	application->msgnum = cmtp_msgnum_get(session);
4791da177e4SLinus Torvalds 
4801da177e4SLinus Torvalds 	cmtp_send_interopmsg(session, CAPI_REQ, application->mapping, application->msgnum,
4811da177e4SLinus Torvalds 				CAPI_FUNCTION_RELEASE, NULL, 0);
4821da177e4SLinus Torvalds 
4831da177e4SLinus Torvalds 	wait_event_interruptible_timeout(session->wait,
4841da177e4SLinus Torvalds 			(application->state == BT_CLOSED), CMTP_INTEROP_TIMEOUT);
4851da177e4SLinus Torvalds 
4861da177e4SLinus Torvalds 	cmtp_application_del(session, application);
4871da177e4SLinus Torvalds }
4881da177e4SLinus Torvalds 
4891da177e4SLinus Torvalds static u16 cmtp_send_message(struct capi_ctr *ctrl, struct sk_buff *skb)
4901da177e4SLinus Torvalds {
4911da177e4SLinus Torvalds 	struct cmtp_session *session = ctrl->driverdata;
4921da177e4SLinus Torvalds 	struct cmtp_application *application;
4931da177e4SLinus Torvalds 	__u16 appl;
4941da177e4SLinus Torvalds 	__u32 contr;
4951da177e4SLinus Torvalds 
4961da177e4SLinus Torvalds 	BT_DBG("ctrl %p skb %p", ctrl, skb);
4971da177e4SLinus Torvalds 
4981da177e4SLinus Torvalds 	appl = CAPIMSG_APPID(skb->data);
4991da177e4SLinus Torvalds 	contr = CAPIMSG_CONTROL(skb->data);
5001da177e4SLinus Torvalds 
5011da177e4SLinus Torvalds 	application = cmtp_application_get(session, CMTP_APPLID, appl);
5021da177e4SLinus Torvalds 	if ((!application) || (application->state != BT_CONNECTED)) {
5031da177e4SLinus Torvalds 		BT_ERR("Can't find application with id %d", appl);
5041da177e4SLinus Torvalds 		return CAPI_ILLAPPNR;
5051da177e4SLinus Torvalds 	}
5061da177e4SLinus Torvalds 
5071da177e4SLinus Torvalds 	CAPIMSG_SETAPPID(skb->data, application->mapping);
5081da177e4SLinus Torvalds 
5091da177e4SLinus Torvalds 	if ((contr & 0x7f) == session->num) {
5101da177e4SLinus Torvalds 		contr = (contr & 0xffffff80) | 0x01;
5111da177e4SLinus Torvalds 		CAPIMSG_SETCONTROL(skb->data, contr);
5121da177e4SLinus Torvalds 	}
5131da177e4SLinus Torvalds 
5141da177e4SLinus Torvalds 	cmtp_send_capimsg(session, skb);
5151da177e4SLinus Torvalds 
5161da177e4SLinus Torvalds 	return CAPI_NOERROR;
5171da177e4SLinus Torvalds }
5181da177e4SLinus Torvalds 
5191da177e4SLinus Torvalds static char *cmtp_procinfo(struct capi_ctr *ctrl)
5201da177e4SLinus Torvalds {
5211da177e4SLinus Torvalds 	return "CAPI Message Transport Protocol";
5221da177e4SLinus Torvalds }
5231da177e4SLinus Torvalds 
5241da177e4SLinus Torvalds static int cmtp_ctr_read_proc(char *page, char **start, off_t off, int count, int *eof, struct capi_ctr *ctrl)
5251da177e4SLinus Torvalds {
5261da177e4SLinus Torvalds 	struct cmtp_session *session = ctrl->driverdata;
5271da177e4SLinus Torvalds 	struct cmtp_application *app;
5281da177e4SLinus Torvalds 	struct list_head *p, *n;
5291da177e4SLinus Torvalds 	int len = 0;
5301da177e4SLinus Torvalds 
5311da177e4SLinus Torvalds 	len += sprintf(page + len, "%s\n\n", cmtp_procinfo(ctrl));
5321da177e4SLinus Torvalds 	len += sprintf(page + len, "addr %s\n", session->name);
5331da177e4SLinus Torvalds 	len += sprintf(page + len, "ctrl %d\n", session->num);
5341da177e4SLinus Torvalds 
5351da177e4SLinus Torvalds 	list_for_each_safe(p, n, &session->applications) {
5361da177e4SLinus Torvalds 		app = list_entry(p, struct cmtp_application, list);
5371da177e4SLinus Torvalds 		len += sprintf(page + len, "appl %d -> %d\n", app->appl, app->mapping);
5381da177e4SLinus Torvalds 	}
5391da177e4SLinus Torvalds 
5401da177e4SLinus Torvalds 	if (off + count >= len)
5411da177e4SLinus Torvalds 		*eof = 1;
5421da177e4SLinus Torvalds 
5431da177e4SLinus Torvalds 	if (len < off)
5441da177e4SLinus Torvalds 		return 0;
5451da177e4SLinus Torvalds 
5461da177e4SLinus Torvalds 	*start = page + off;
5471da177e4SLinus Torvalds 
5481da177e4SLinus Torvalds 	return ((count < len - off) ? count : len - off);
5491da177e4SLinus Torvalds }
5501da177e4SLinus Torvalds 
5511da177e4SLinus Torvalds 
5521da177e4SLinus Torvalds int cmtp_attach_device(struct cmtp_session *session)
5531da177e4SLinus Torvalds {
5541da177e4SLinus Torvalds 	unsigned char buf[4];
5551da177e4SLinus Torvalds 	long ret;
5561da177e4SLinus Torvalds 
5571da177e4SLinus Torvalds 	BT_DBG("session %p", session);
5581da177e4SLinus Torvalds 
5591da177e4SLinus Torvalds 	capimsg_setu32(buf, 0, 0);
5601da177e4SLinus Torvalds 
5611da177e4SLinus Torvalds 	cmtp_send_interopmsg(session, CAPI_REQ, 0xffff, CMTP_INITIAL_MSGNUM,
5621da177e4SLinus Torvalds 				CAPI_FUNCTION_GET_PROFILE, buf, 4);
5631da177e4SLinus Torvalds 
5641da177e4SLinus Torvalds 	ret = wait_event_interruptible_timeout(session->wait,
5651da177e4SLinus Torvalds 			session->ncontroller, CMTP_INTEROP_TIMEOUT);
5661da177e4SLinus Torvalds 
5671da177e4SLinus Torvalds 	BT_INFO("Found %d CAPI controller(s) on device %s", session->ncontroller, session->name);
5681da177e4SLinus Torvalds 
5691da177e4SLinus Torvalds 	if (!ret)
5701da177e4SLinus Torvalds 		return -ETIMEDOUT;
5711da177e4SLinus Torvalds 
5721da177e4SLinus Torvalds 	if (!session->ncontroller)
5731da177e4SLinus Torvalds 		return -ENODEV;
5741da177e4SLinus Torvalds 
5751da177e4SLinus Torvalds 	if (session->ncontroller > 1)
5761da177e4SLinus Torvalds 		BT_INFO("Setting up only CAPI controller 1");
5771da177e4SLinus Torvalds 
5781da177e4SLinus Torvalds 	session->ctrl.owner      = THIS_MODULE;
5791da177e4SLinus Torvalds 	session->ctrl.driverdata = session;
5801da177e4SLinus Torvalds 	strcpy(session->ctrl.name, session->name);
5811da177e4SLinus Torvalds 
5821da177e4SLinus Torvalds 	session->ctrl.driver_name   = "cmtp";
5831da177e4SLinus Torvalds 	session->ctrl.load_firmware = cmtp_load_firmware;
5841da177e4SLinus Torvalds 	session->ctrl.reset_ctr     = cmtp_reset_ctr;
5851da177e4SLinus Torvalds 	session->ctrl.register_appl = cmtp_register_appl;
5861da177e4SLinus Torvalds 	session->ctrl.release_appl  = cmtp_release_appl;
5871da177e4SLinus Torvalds 	session->ctrl.send_message  = cmtp_send_message;
5881da177e4SLinus Torvalds 
5891da177e4SLinus Torvalds 	session->ctrl.procinfo      = cmtp_procinfo;
5901da177e4SLinus Torvalds 	session->ctrl.ctr_read_proc = cmtp_ctr_read_proc;
5911da177e4SLinus Torvalds 
5921da177e4SLinus Torvalds 	if (attach_capi_ctr(&session->ctrl) < 0) {
5931da177e4SLinus Torvalds 		BT_ERR("Can't attach new controller");
5941da177e4SLinus Torvalds 		return -EBUSY;
5951da177e4SLinus Torvalds 	}
5961da177e4SLinus Torvalds 
5971da177e4SLinus Torvalds 	session->num = session->ctrl.cnr;
5981da177e4SLinus Torvalds 
5991da177e4SLinus Torvalds 	BT_DBG("session %p num %d", session, session->num);
6001da177e4SLinus Torvalds 
6011da177e4SLinus Torvalds 	capimsg_setu32(buf, 0, 1);
6021da177e4SLinus Torvalds 
6031da177e4SLinus Torvalds 	cmtp_send_interopmsg(session, CAPI_REQ, 0xffff, cmtp_msgnum_get(session),
6041da177e4SLinus Torvalds 				CAPI_FUNCTION_GET_MANUFACTURER, buf, 4);
6051da177e4SLinus Torvalds 
6061da177e4SLinus Torvalds 	cmtp_send_interopmsg(session, CAPI_REQ, 0xffff, cmtp_msgnum_get(session),
6071da177e4SLinus Torvalds 				CAPI_FUNCTION_GET_VERSION, buf, 4);
6081da177e4SLinus Torvalds 
6091da177e4SLinus Torvalds 	cmtp_send_interopmsg(session, CAPI_REQ, 0xffff, cmtp_msgnum_get(session),
6101da177e4SLinus Torvalds 				CAPI_FUNCTION_GET_SERIAL_NUMBER, buf, 4);
6111da177e4SLinus Torvalds 
6121da177e4SLinus Torvalds 	cmtp_send_interopmsg(session, CAPI_REQ, 0xffff, cmtp_msgnum_get(session),
6131da177e4SLinus Torvalds 				CAPI_FUNCTION_GET_PROFILE, buf, 4);
6141da177e4SLinus Torvalds 
6151da177e4SLinus Torvalds 	return 0;
6161da177e4SLinus Torvalds }
6171da177e4SLinus Torvalds 
6181da177e4SLinus Torvalds void cmtp_detach_device(struct cmtp_session *session)
6191da177e4SLinus Torvalds {
6201da177e4SLinus Torvalds 	BT_DBG("session %p", session);
6211da177e4SLinus Torvalds 
6221da177e4SLinus Torvalds 	detach_capi_ctr(&session->ctrl);
6231da177e4SLinus Torvalds }
624