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