xref: /linux/net/bluetooth/cmtp/capi.c (revision a1f1c24368ec9b1d2bbc77075d2ba340ea9d0a83)
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 
232ad8f54bSSyam Sidhardhan #include <linux/export.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>
29174cd4b1SIngo Molnar #include <linux/sched/signal.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>
38fada4ac3SSzymon 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 
77b442a853SKai Ye 	BT_DBG("session %p application %p appl %u", 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 
1047eb7404fSGeliang Tang 	list_for_each_entry(app, &session->applications, 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 
138b442a853SKai Ye 	BT_DBG("session %p skb %p len %u", 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 
145fada4ac3SSzymon Janc 	wake_up_interruptible(sk_sleep(session->sock->sk));
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 
155b442a853SKai Ye 	BT_DBG("session %p subcmd 0x%02x appl %u msgnum %u", session, subcmd, appl, msgnum);
1561da177e4SLinus Torvalds 
1575a08ecceSAndrei Emeltchenko 	skb = alloc_skb(CAPI_MSG_BASELEN + 6 + len, GFP_ATOMIC);
1585a08ecceSAndrei Emeltchenko 	if (!skb) {
1591da177e4SLinus Torvalds 		BT_ERR("Can't allocate memory for interoperability packet");
1601da177e4SLinus Torvalds 		return;
1611da177e4SLinus Torvalds 	}
1621da177e4SLinus Torvalds 
1631da177e4SLinus Torvalds 	s = skb_put(skb, CAPI_MSG_BASELEN + 6 + len);
1641da177e4SLinus Torvalds 
1651da177e4SLinus Torvalds 	capimsg_setu16(s, 0, CAPI_MSG_BASELEN + 6 + len);
1661da177e4SLinus Torvalds 	capimsg_setu16(s, 2, appl);
1671da177e4SLinus Torvalds 	capimsg_setu8 (s, 4, CAPI_INTEROPERABILITY);
1681da177e4SLinus Torvalds 	capimsg_setu8 (s, 5, subcmd);
1691da177e4SLinus Torvalds 	capimsg_setu16(s, 6, msgnum);
1701da177e4SLinus Torvalds 
1711da177e4SLinus Torvalds 	/* Interoperability selector (Bluetooth Device Management) */
1721da177e4SLinus Torvalds 	capimsg_setu16(s, 8, 0x0001);
1731da177e4SLinus Torvalds 
1741da177e4SLinus Torvalds 	capimsg_setu8 (s, 10, 3 + len);
1751da177e4SLinus Torvalds 	capimsg_setu16(s, 11, function);
1761da177e4SLinus Torvalds 	capimsg_setu8 (s, 13, len);
1771da177e4SLinus Torvalds 
1781da177e4SLinus Torvalds 	if (len > 0)
1791da177e4SLinus Torvalds 		memcpy(s + 14, buf, len);
1801da177e4SLinus Torvalds 
1811da177e4SLinus Torvalds 	cmtp_send_capimsg(session, skb);
1821da177e4SLinus Torvalds }
1831da177e4SLinus Torvalds 
1841da177e4SLinus Torvalds static void cmtp_recv_interopmsg(struct cmtp_session *session, struct sk_buff *skb)
1851da177e4SLinus Torvalds {
1861da177e4SLinus Torvalds 	struct capi_ctr *ctrl = &session->ctrl;
1871da177e4SLinus Torvalds 	struct cmtp_application *application;
1881da177e4SLinus Torvalds 	__u16 appl, msgnum, func, info;
1891da177e4SLinus Torvalds 	__u32 controller;
1901da177e4SLinus Torvalds 
191b442a853SKai Ye 	BT_DBG("session %p skb %p len %u", session, skb, skb->len);
1921da177e4SLinus Torvalds 
1931da177e4SLinus Torvalds 	switch (CAPIMSG_SUBCOMMAND(skb->data)) {
1941da177e4SLinus Torvalds 	case CAPI_CONF:
195f4777569SMarcel Holtmann 		if (skb->len < CAPI_MSG_BASELEN + 10)
196f4777569SMarcel Holtmann 			break;
197f4777569SMarcel Holtmann 
1981da177e4SLinus Torvalds 		func = CAPIMSG_U16(skb->data, CAPI_MSG_BASELEN + 5);
1991da177e4SLinus Torvalds 		info = CAPIMSG_U16(skb->data, CAPI_MSG_BASELEN + 8);
2001da177e4SLinus Torvalds 
2011da177e4SLinus Torvalds 		switch (func) {
2021da177e4SLinus Torvalds 		case CAPI_FUNCTION_REGISTER:
2031da177e4SLinus Torvalds 			msgnum = CAPIMSG_MSGID(skb->data);
2041da177e4SLinus Torvalds 
2051da177e4SLinus Torvalds 			application = cmtp_application_get(session, CMTP_MSGNUM, msgnum);
2061da177e4SLinus Torvalds 			if (application) {
2071da177e4SLinus Torvalds 				application->state = BT_CONNECTED;
2081da177e4SLinus Torvalds 				application->msgnum = 0;
2091da177e4SLinus Torvalds 				application->mapping = CAPIMSG_APPID(skb->data);
2101da177e4SLinus Torvalds 				wake_up_interruptible(&session->wait);
2111da177e4SLinus Torvalds 			}
2121da177e4SLinus Torvalds 
2131da177e4SLinus Torvalds 			break;
2141da177e4SLinus Torvalds 
2151da177e4SLinus Torvalds 		case CAPI_FUNCTION_RELEASE:
2161da177e4SLinus Torvalds 			appl = CAPIMSG_APPID(skb->data);
2171da177e4SLinus Torvalds 
2181da177e4SLinus Torvalds 			application = cmtp_application_get(session, CMTP_MAPPING, appl);
2191da177e4SLinus Torvalds 			if (application) {
2201da177e4SLinus Torvalds 				application->state = BT_CLOSED;
2211da177e4SLinus Torvalds 				application->msgnum = 0;
2221da177e4SLinus Torvalds 				wake_up_interruptible(&session->wait);
2231da177e4SLinus Torvalds 			}
2241da177e4SLinus Torvalds 
2251da177e4SLinus Torvalds 			break;
2261da177e4SLinus Torvalds 
2271da177e4SLinus Torvalds 		case CAPI_FUNCTION_GET_PROFILE:
228f4777569SMarcel Holtmann 			if (skb->len < CAPI_MSG_BASELEN + 11 + sizeof(capi_profile))
229f4777569SMarcel Holtmann 				break;
230f4777569SMarcel Holtmann 
2311da177e4SLinus Torvalds 			controller = CAPIMSG_U16(skb->data, CAPI_MSG_BASELEN + 11);
2321da177e4SLinus Torvalds 			msgnum = CAPIMSG_MSGID(skb->data);
2331da177e4SLinus Torvalds 
2341da177e4SLinus Torvalds 			if (!info && (msgnum == CMTP_INITIAL_MSGNUM)) {
2351da177e4SLinus Torvalds 				session->ncontroller = controller;
2361da177e4SLinus Torvalds 				wake_up_interruptible(&session->wait);
2371da177e4SLinus Torvalds 				break;
2381da177e4SLinus Torvalds 			}
2391da177e4SLinus Torvalds 
2401da177e4SLinus Torvalds 			if (!info && ctrl) {
2411da177e4SLinus Torvalds 				memcpy(&ctrl->profile,
2421da177e4SLinus Torvalds 					skb->data + CAPI_MSG_BASELEN + 11,
2431da177e4SLinus Torvalds 					sizeof(capi_profile));
2441da177e4SLinus Torvalds 				session->state = BT_CONNECTED;
2451da177e4SLinus Torvalds 				capi_ctr_ready(ctrl);
2461da177e4SLinus Torvalds 			}
2471da177e4SLinus Torvalds 
2481da177e4SLinus Torvalds 			break;
2491da177e4SLinus Torvalds 
2501da177e4SLinus Torvalds 		case CAPI_FUNCTION_GET_MANUFACTURER:
251*a1f1c243SJustin Stitt 			if (!info && ctrl && skb->len > CAPI_MSG_BASELEN + 14)
252*a1f1c243SJustin Stitt 				strscpy_pad(ctrl->manu,
253*a1f1c243SJustin Stitt 					    skb->data + CAPI_MSG_BASELEN + 15,
2541da177e4SLinus Torvalds 					    skb->data[CAPI_MSG_BASELEN + 14]);
2551da177e4SLinus Torvalds 			break;
2561da177e4SLinus Torvalds 
2571da177e4SLinus Torvalds 		case CAPI_FUNCTION_GET_VERSION:
258f4777569SMarcel Holtmann 			if (skb->len < CAPI_MSG_BASELEN + 32)
259f4777569SMarcel Holtmann 				break;
260f4777569SMarcel Holtmann 
2611da177e4SLinus Torvalds 			if (!info && ctrl) {
2621da177e4SLinus Torvalds 				ctrl->version.majorversion = CAPIMSG_U32(skb->data, CAPI_MSG_BASELEN + 16);
2631da177e4SLinus Torvalds 				ctrl->version.minorversion = CAPIMSG_U32(skb->data, CAPI_MSG_BASELEN + 20);
2641da177e4SLinus Torvalds 				ctrl->version.majormanuversion = CAPIMSG_U32(skb->data, CAPI_MSG_BASELEN + 24);
2651da177e4SLinus Torvalds 				ctrl->version.minormanuversion = CAPIMSG_U32(skb->data, CAPI_MSG_BASELEN + 28);
2661da177e4SLinus Torvalds 			}
2671da177e4SLinus Torvalds 
2681da177e4SLinus Torvalds 			break;
2691da177e4SLinus Torvalds 
2701da177e4SLinus Torvalds 		case CAPI_FUNCTION_GET_SERIAL_NUMBER:
271*a1f1c243SJustin Stitt 			if (!info && ctrl && skb->len > CAPI_MSG_BASELEN + 16)
272*a1f1c243SJustin Stitt 				strscpy_pad(ctrl->serial,
273*a1f1c243SJustin Stitt 					    skb->data + CAPI_MSG_BASELEN + 17,
274f4777569SMarcel Holtmann 					    skb->data[CAPI_MSG_BASELEN + 16]);
2751da177e4SLinus Torvalds 			break;
2761da177e4SLinus Torvalds 		}
2771da177e4SLinus Torvalds 
2781da177e4SLinus Torvalds 		break;
2791da177e4SLinus Torvalds 
2801da177e4SLinus Torvalds 	case CAPI_IND:
281f4777569SMarcel Holtmann 		if (skb->len < CAPI_MSG_BASELEN + 6)
282f4777569SMarcel Holtmann 			break;
283f4777569SMarcel Holtmann 
2841da177e4SLinus Torvalds 		func = CAPIMSG_U16(skb->data, CAPI_MSG_BASELEN + 3);
2851da177e4SLinus Torvalds 
2861da177e4SLinus Torvalds 		if (func == CAPI_FUNCTION_LOOPBACK) {
287f4777569SMarcel Holtmann 			int len = min_t(uint, skb->len - CAPI_MSG_BASELEN - 6,
288f4777569SMarcel Holtmann 						skb->data[CAPI_MSG_BASELEN + 5]);
2891da177e4SLinus Torvalds 			appl = CAPIMSG_APPID(skb->data);
2901da177e4SLinus Torvalds 			msgnum = CAPIMSG_MSGID(skb->data);
2911da177e4SLinus Torvalds 			cmtp_send_interopmsg(session, CAPI_RESP, appl, msgnum, func,
292f4777569SMarcel Holtmann 						skb->data + CAPI_MSG_BASELEN + 6, len);
2931da177e4SLinus Torvalds 		}
2941da177e4SLinus Torvalds 
2951da177e4SLinus Torvalds 		break;
2961da177e4SLinus Torvalds 	}
2971da177e4SLinus Torvalds 
2981da177e4SLinus Torvalds 	kfree_skb(skb);
2991da177e4SLinus Torvalds }
3001da177e4SLinus Torvalds 
3011da177e4SLinus Torvalds void cmtp_recv_capimsg(struct cmtp_session *session, struct sk_buff *skb)
3021da177e4SLinus Torvalds {
3031da177e4SLinus Torvalds 	struct capi_ctr *ctrl = &session->ctrl;
3041da177e4SLinus Torvalds 	struct cmtp_application *application;
305d29d04ceSDavid Miller 	__u16 appl;
3061da177e4SLinus Torvalds 	__u32 contr;
3071da177e4SLinus Torvalds 
308b442a853SKai Ye 	BT_DBG("session %p skb %p len %u", session, skb, skb->len);
3091da177e4SLinus Torvalds 
310f4777569SMarcel Holtmann 	if (skb->len < CAPI_MSG_BASELEN)
311f4777569SMarcel Holtmann 		return;
312f4777569SMarcel Holtmann 
3131da177e4SLinus Torvalds 	if (CAPIMSG_COMMAND(skb->data) == CAPI_INTEROPERABILITY) {
3141da177e4SLinus Torvalds 		cmtp_recv_interopmsg(session, skb);
3151da177e4SLinus Torvalds 		return;
3161da177e4SLinus Torvalds 	}
3171da177e4SLinus Torvalds 
318b2ddeb11SMarcel Holtmann 	if (session->flags & BIT(CMTP_LOOPBACK)) {
3191da177e4SLinus Torvalds 		kfree_skb(skb);
3201da177e4SLinus Torvalds 		return;
3211da177e4SLinus Torvalds 	}
3221da177e4SLinus Torvalds 
3231da177e4SLinus Torvalds 	appl = CAPIMSG_APPID(skb->data);
3241da177e4SLinus Torvalds 	contr = CAPIMSG_CONTROL(skb->data);
3251da177e4SLinus Torvalds 
3261da177e4SLinus Torvalds 	application = cmtp_application_get(session, CMTP_MAPPING, appl);
3271da177e4SLinus Torvalds 	if (application) {
3281da177e4SLinus Torvalds 		appl = application->appl;
3291da177e4SLinus Torvalds 		CAPIMSG_SETAPPID(skb->data, appl);
3301da177e4SLinus Torvalds 	} else {
331b442a853SKai Ye 		BT_ERR("Can't find application with id %u", appl);
3321da177e4SLinus Torvalds 		kfree_skb(skb);
3331da177e4SLinus Torvalds 		return;
3341da177e4SLinus Torvalds 	}
3351da177e4SLinus Torvalds 
3361da177e4SLinus Torvalds 	if ((contr & 0x7f) == 0x01) {
3371da177e4SLinus Torvalds 		contr = (contr & 0xffffff80) | session->num;
3381da177e4SLinus Torvalds 		CAPIMSG_SETCONTROL(skb->data, contr);
3391da177e4SLinus Torvalds 	}
3401da177e4SLinus Torvalds 
3411da177e4SLinus Torvalds 	capi_ctr_handle_message(ctrl, appl, skb);
3421da177e4SLinus Torvalds }
3431da177e4SLinus Torvalds 
3441da177e4SLinus Torvalds static int cmtp_load_firmware(struct capi_ctr *ctrl, capiloaddata *data)
3451da177e4SLinus Torvalds {
3461da177e4SLinus Torvalds 	BT_DBG("ctrl %p data %p", ctrl, data);
3471da177e4SLinus Torvalds 
3481da177e4SLinus Torvalds 	return 0;
3491da177e4SLinus Torvalds }
3501da177e4SLinus Torvalds 
3511da177e4SLinus Torvalds static void cmtp_reset_ctr(struct capi_ctr *ctrl)
3521da177e4SLinus Torvalds {
3531da177e4SLinus Torvalds 	struct cmtp_session *session = ctrl->driverdata;
3541da177e4SLinus Torvalds 
3551da177e4SLinus Torvalds 	BT_DBG("ctrl %p", ctrl);
3561da177e4SLinus Torvalds 
3574e329972STilman Schmidt 	capi_ctr_down(ctrl);
3581da177e4SLinus Torvalds 
3597176522cSPeter Hurley 	atomic_inc(&session->terminate);
3607176522cSPeter Hurley 	wake_up_process(session->task);
3611da177e4SLinus Torvalds }
3621da177e4SLinus Torvalds 
3631da177e4SLinus Torvalds static void cmtp_register_appl(struct capi_ctr *ctrl, __u16 appl, capi_register_params *rp)
3641da177e4SLinus Torvalds {
3651da177e4SLinus Torvalds 	DECLARE_WAITQUEUE(wait, current);
3661da177e4SLinus Torvalds 	struct cmtp_session *session = ctrl->driverdata;
3671da177e4SLinus Torvalds 	struct cmtp_application *application;
3681da177e4SLinus Torvalds 	unsigned long timeo = CMTP_INTEROP_TIMEOUT;
3691da177e4SLinus Torvalds 	unsigned char buf[8];
3701da177e4SLinus Torvalds 	int err = 0, nconn, want = rp->level3cnt;
3711da177e4SLinus Torvalds 
372b442a853SKai Ye 	BT_DBG("ctrl %p appl %u level3cnt %u datablkcnt %u datablklen %u",
3731da177e4SLinus Torvalds 	       ctrl, appl, rp->level3cnt, rp->datablkcnt, rp->datablklen);
3741da177e4SLinus Torvalds 
3751da177e4SLinus Torvalds 	application = cmtp_application_add(session, appl);
3761da177e4SLinus Torvalds 	if (!application) {
3771da177e4SLinus Torvalds 		BT_ERR("Can't allocate memory for new application");
3781da177e4SLinus Torvalds 		return;
3791da177e4SLinus Torvalds 	}
3801da177e4SLinus Torvalds 
3811da177e4SLinus Torvalds 	if (want < 0)
3821da177e4SLinus Torvalds 		nconn = ctrl->profile.nbchannel * -want;
3831da177e4SLinus Torvalds 	else
3841da177e4SLinus Torvalds 		nconn = want;
3851da177e4SLinus Torvalds 
3861da177e4SLinus Torvalds 	if (nconn == 0)
3871da177e4SLinus Torvalds 		nconn = ctrl->profile.nbchannel;
3881da177e4SLinus Torvalds 
3891da177e4SLinus Torvalds 	capimsg_setu16(buf, 0, nconn);
3901da177e4SLinus Torvalds 	capimsg_setu16(buf, 2, rp->datablkcnt);
3911da177e4SLinus Torvalds 	capimsg_setu16(buf, 4, rp->datablklen);
3921da177e4SLinus Torvalds 
3931da177e4SLinus Torvalds 	application->state = BT_CONFIG;
3941da177e4SLinus Torvalds 	application->msgnum = cmtp_msgnum_get(session);
3951da177e4SLinus Torvalds 
3961da177e4SLinus Torvalds 	cmtp_send_interopmsg(session, CAPI_REQ, 0x0000, application->msgnum,
3971da177e4SLinus Torvalds 				CAPI_FUNCTION_REGISTER, buf, 6);
3981da177e4SLinus Torvalds 
3991da177e4SLinus Torvalds 	add_wait_queue(&session->wait, &wait);
4001da177e4SLinus Torvalds 	while (1) {
4011da177e4SLinus Torvalds 		set_current_state(TASK_INTERRUPTIBLE);
4021da177e4SLinus Torvalds 
4031da177e4SLinus Torvalds 		if (!timeo) {
4041da177e4SLinus Torvalds 			err = -EAGAIN;
4051da177e4SLinus Torvalds 			break;
4061da177e4SLinus Torvalds 		}
4071da177e4SLinus Torvalds 
4081da177e4SLinus Torvalds 		if (application->state == BT_CLOSED) {
4091da177e4SLinus Torvalds 			err = -application->err;
4101da177e4SLinus Torvalds 			break;
4111da177e4SLinus Torvalds 		}
4121da177e4SLinus Torvalds 
4131da177e4SLinus Torvalds 		if (application->state == BT_CONNECTED)
4141da177e4SLinus Torvalds 			break;
4151da177e4SLinus Torvalds 
4161da177e4SLinus Torvalds 		if (signal_pending(current)) {
4171da177e4SLinus Torvalds 			err = -EINTR;
4181da177e4SLinus Torvalds 			break;
4191da177e4SLinus Torvalds 		}
4201da177e4SLinus Torvalds 
4211da177e4SLinus Torvalds 		timeo = schedule_timeout(timeo);
4221da177e4SLinus Torvalds 	}
4231da177e4SLinus Torvalds 	set_current_state(TASK_RUNNING);
4241da177e4SLinus Torvalds 	remove_wait_queue(&session->wait, &wait);
4251da177e4SLinus Torvalds 
4261da177e4SLinus Torvalds 	if (err) {
4271da177e4SLinus Torvalds 		cmtp_application_del(session, application);
4281da177e4SLinus Torvalds 		return;
4291da177e4SLinus Torvalds 	}
4301da177e4SLinus Torvalds }
4311da177e4SLinus Torvalds 
4321da177e4SLinus Torvalds static void cmtp_release_appl(struct capi_ctr *ctrl, __u16 appl)
4331da177e4SLinus Torvalds {
4341da177e4SLinus Torvalds 	struct cmtp_session *session = ctrl->driverdata;
4351da177e4SLinus Torvalds 	struct cmtp_application *application;
4361da177e4SLinus Torvalds 
437b442a853SKai Ye 	BT_DBG("ctrl %p appl %u", ctrl, appl);
4381da177e4SLinus Torvalds 
4391da177e4SLinus Torvalds 	application = cmtp_application_get(session, CMTP_APPLID, appl);
4401da177e4SLinus Torvalds 	if (!application) {
4411da177e4SLinus Torvalds 		BT_ERR("Can't find application");
4421da177e4SLinus Torvalds 		return;
4431da177e4SLinus Torvalds 	}
4441da177e4SLinus Torvalds 
4451da177e4SLinus Torvalds 	application->msgnum = cmtp_msgnum_get(session);
4461da177e4SLinus Torvalds 
4471da177e4SLinus Torvalds 	cmtp_send_interopmsg(session, CAPI_REQ, application->mapping, application->msgnum,
4481da177e4SLinus Torvalds 				CAPI_FUNCTION_RELEASE, NULL, 0);
4491da177e4SLinus Torvalds 
4501da177e4SLinus Torvalds 	wait_event_interruptible_timeout(session->wait,
4511da177e4SLinus Torvalds 			(application->state == BT_CLOSED), CMTP_INTEROP_TIMEOUT);
4521da177e4SLinus Torvalds 
4531da177e4SLinus Torvalds 	cmtp_application_del(session, application);
4541da177e4SLinus Torvalds }
4551da177e4SLinus Torvalds 
4561da177e4SLinus Torvalds static u16 cmtp_send_message(struct capi_ctr *ctrl, struct sk_buff *skb)
4571da177e4SLinus Torvalds {
4581da177e4SLinus Torvalds 	struct cmtp_session *session = ctrl->driverdata;
4591da177e4SLinus Torvalds 	struct cmtp_application *application;
4601da177e4SLinus Torvalds 	__u16 appl;
4611da177e4SLinus Torvalds 	__u32 contr;
4621da177e4SLinus Torvalds 
4631da177e4SLinus Torvalds 	BT_DBG("ctrl %p skb %p", ctrl, skb);
4641da177e4SLinus Torvalds 
4651da177e4SLinus Torvalds 	appl = CAPIMSG_APPID(skb->data);
4661da177e4SLinus Torvalds 	contr = CAPIMSG_CONTROL(skb->data);
4671da177e4SLinus Torvalds 
4681da177e4SLinus Torvalds 	application = cmtp_application_get(session, CMTP_APPLID, appl);
4691da177e4SLinus Torvalds 	if ((!application) || (application->state != BT_CONNECTED)) {
470b442a853SKai Ye 		BT_ERR("Can't find application with id %u", appl);
4711da177e4SLinus Torvalds 		return CAPI_ILLAPPNR;
4721da177e4SLinus Torvalds 	}
4731da177e4SLinus Torvalds 
4741da177e4SLinus Torvalds 	CAPIMSG_SETAPPID(skb->data, application->mapping);
4751da177e4SLinus Torvalds 
4761da177e4SLinus Torvalds 	if ((contr & 0x7f) == session->num) {
4771da177e4SLinus Torvalds 		contr = (contr & 0xffffff80) | 0x01;
4781da177e4SLinus Torvalds 		CAPIMSG_SETCONTROL(skb->data, contr);
4791da177e4SLinus Torvalds 	}
4801da177e4SLinus Torvalds 
4811da177e4SLinus Torvalds 	cmtp_send_capimsg(session, skb);
4821da177e4SLinus Torvalds 
4831da177e4SLinus Torvalds 	return CAPI_NOERROR;
4841da177e4SLinus Torvalds }
4851da177e4SLinus Torvalds 
4861da177e4SLinus Torvalds static char *cmtp_procinfo(struct capi_ctr *ctrl)
4871da177e4SLinus Torvalds {
4881da177e4SLinus Torvalds 	return "CAPI Message Transport Protocol";
4891da177e4SLinus Torvalds }
4901da177e4SLinus Torvalds 
4919a58a80aSAlexey Dobriyan static int cmtp_proc_show(struct seq_file *m, void *v)
4921da177e4SLinus Torvalds {
4939a58a80aSAlexey Dobriyan 	struct capi_ctr *ctrl = m->private;
4941da177e4SLinus Torvalds 	struct cmtp_session *session = ctrl->driverdata;
4951da177e4SLinus Torvalds 	struct cmtp_application *app;
4961da177e4SLinus Torvalds 
4979a58a80aSAlexey Dobriyan 	seq_printf(m, "%s\n\n", cmtp_procinfo(ctrl));
4989a58a80aSAlexey Dobriyan 	seq_printf(m, "addr %s\n", session->name);
4999a58a80aSAlexey Dobriyan 	seq_printf(m, "ctrl %d\n", session->num);
5001da177e4SLinus Torvalds 
5017eb7404fSGeliang Tang 	list_for_each_entry(app, &session->applications, list) {
502b442a853SKai Ye 		seq_printf(m, "appl %u -> %u\n", app->appl, app->mapping);
5031da177e4SLinus Torvalds 	}
5041da177e4SLinus Torvalds 
5051da177e4SLinus Torvalds 	return 0;
5061da177e4SLinus Torvalds }
5071da177e4SLinus Torvalds 
5081da177e4SLinus Torvalds int cmtp_attach_device(struct cmtp_session *session)
5091da177e4SLinus Torvalds {
5101da177e4SLinus Torvalds 	unsigned char buf[4];
5111da177e4SLinus Torvalds 	long ret;
5121da177e4SLinus Torvalds 
5131da177e4SLinus Torvalds 	BT_DBG("session %p", session);
5141da177e4SLinus Torvalds 
5151da177e4SLinus Torvalds 	capimsg_setu32(buf, 0, 0);
5161da177e4SLinus Torvalds 
5171da177e4SLinus Torvalds 	cmtp_send_interopmsg(session, CAPI_REQ, 0xffff, CMTP_INITIAL_MSGNUM,
5181da177e4SLinus Torvalds 				CAPI_FUNCTION_GET_PROFILE, buf, 4);
5191da177e4SLinus Torvalds 
5201da177e4SLinus Torvalds 	ret = wait_event_interruptible_timeout(session->wait,
5211da177e4SLinus Torvalds 			session->ncontroller, CMTP_INTEROP_TIMEOUT);
5221da177e4SLinus Torvalds 
5231da177e4SLinus Torvalds 	BT_INFO("Found %d CAPI controller(s) on device %s", session->ncontroller, session->name);
5241da177e4SLinus Torvalds 
5251da177e4SLinus Torvalds 	if (!ret)
5261da177e4SLinus Torvalds 		return -ETIMEDOUT;
5271da177e4SLinus Torvalds 
5281da177e4SLinus Torvalds 	if (!session->ncontroller)
5291da177e4SLinus Torvalds 		return -ENODEV;
5301da177e4SLinus Torvalds 
5311da177e4SLinus Torvalds 	if (session->ncontroller > 1)
5321da177e4SLinus Torvalds 		BT_INFO("Setting up only CAPI controller 1");
5331da177e4SLinus Torvalds 
5341da177e4SLinus Torvalds 	session->ctrl.owner      = THIS_MODULE;
5351da177e4SLinus Torvalds 	session->ctrl.driverdata = session;
5361da177e4SLinus Torvalds 	strcpy(session->ctrl.name, session->name);
5371da177e4SLinus Torvalds 
5381da177e4SLinus Torvalds 	session->ctrl.driver_name   = "cmtp";
5391da177e4SLinus Torvalds 	session->ctrl.load_firmware = cmtp_load_firmware;
5401da177e4SLinus Torvalds 	session->ctrl.reset_ctr     = cmtp_reset_ctr;
5411da177e4SLinus Torvalds 	session->ctrl.register_appl = cmtp_register_appl;
5421da177e4SLinus Torvalds 	session->ctrl.release_appl  = cmtp_release_appl;
5431da177e4SLinus Torvalds 	session->ctrl.send_message  = cmtp_send_message;
5441da177e4SLinus Torvalds 
5451da177e4SLinus Torvalds 	session->ctrl.procinfo      = cmtp_procinfo;
5462cd1f0ddSChristoph Hellwig 	session->ctrl.proc_show     = cmtp_proc_show;
5471da177e4SLinus Torvalds 
5481da177e4SLinus Torvalds 	if (attach_capi_ctr(&session->ctrl) < 0) {
5491da177e4SLinus Torvalds 		BT_ERR("Can't attach new controller");
5501da177e4SLinus Torvalds 		return -EBUSY;
5511da177e4SLinus Torvalds 	}
5521da177e4SLinus Torvalds 
5531da177e4SLinus Torvalds 	session->num = session->ctrl.cnr;
5541da177e4SLinus Torvalds 
5551da177e4SLinus Torvalds 	BT_DBG("session %p num %d", session, session->num);
5561da177e4SLinus Torvalds 
5571da177e4SLinus Torvalds 	capimsg_setu32(buf, 0, 1);
5581da177e4SLinus Torvalds 
5591da177e4SLinus Torvalds 	cmtp_send_interopmsg(session, CAPI_REQ, 0xffff, cmtp_msgnum_get(session),
5601da177e4SLinus Torvalds 				CAPI_FUNCTION_GET_MANUFACTURER, buf, 4);
5611da177e4SLinus Torvalds 
5621da177e4SLinus Torvalds 	cmtp_send_interopmsg(session, CAPI_REQ, 0xffff, cmtp_msgnum_get(session),
5631da177e4SLinus Torvalds 				CAPI_FUNCTION_GET_VERSION, buf, 4);
5641da177e4SLinus Torvalds 
5651da177e4SLinus Torvalds 	cmtp_send_interopmsg(session, CAPI_REQ, 0xffff, cmtp_msgnum_get(session),
5661da177e4SLinus Torvalds 				CAPI_FUNCTION_GET_SERIAL_NUMBER, buf, 4);
5671da177e4SLinus Torvalds 
5681da177e4SLinus Torvalds 	cmtp_send_interopmsg(session, CAPI_REQ, 0xffff, cmtp_msgnum_get(session),
5691da177e4SLinus Torvalds 				CAPI_FUNCTION_GET_PROFILE, buf, 4);
5701da177e4SLinus Torvalds 
5711da177e4SLinus Torvalds 	return 0;
5721da177e4SLinus Torvalds }
5731da177e4SLinus Torvalds 
5741da177e4SLinus Torvalds void cmtp_detach_device(struct cmtp_session *session)
5751da177e4SLinus Torvalds {
5761da177e4SLinus Torvalds 	BT_DBG("session %p", session);
5771da177e4SLinus Torvalds 
5781da177e4SLinus Torvalds 	detach_capi_ctr(&session->ctrl);
5791da177e4SLinus Torvalds }
580