400-bluetooth-hci_h4p.patch 49 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946
  1. ---
  2. drivers/bluetooth/Kconfig | 10
  3. drivers/bluetooth/Makefile | 1
  4. drivers/bluetooth/hci_h4p/Makefile | 7
  5. drivers/bluetooth/hci_h4p/core.c | 1043 ++++++++++++++++++++++++++++++++++++
  6. drivers/bluetooth/hci_h4p/fw-csr.c | 149 +++++
  7. drivers/bluetooth/hci_h4p/fw-ti.c | 90 +++
  8. drivers/bluetooth/hci_h4p/fw.c | 155 +++++
  9. drivers/bluetooth/hci_h4p/hci_h4p.h | 183 ++++++
  10. drivers/bluetooth/hci_h4p/sysfs.c | 84 ++
  11. drivers/bluetooth/hci_h4p/uart.c | 169 +++++
  12. 10 files changed, 1891 insertions(+)
  13. --- /dev/null
  14. +++ b/drivers/bluetooth/hci_h4p/core.c
  15. @@ -0,0 +1,1043 @@
  16. +/*
  17. + * This file is part of hci_h4p bluetooth driver
  18. + *
  19. + * Copyright (C) 2005, 2006 Nokia Corporation.
  20. + *
  21. + * Contact: Ville Tervo <[email protected]>
  22. + *
  23. + * This program is free software; you can redistribute it and/or
  24. + * modify it under the terms of the GNU General Public License
  25. + * version 2 as published by the Free Software Foundation.
  26. + *
  27. + * This program is distributed in the hope that it will be useful, but
  28. + * WITHOUT ANY WARRANTY; without even the implied warranty of
  29. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  30. + * General Public License for more details.
  31. + *
  32. + * You should have received a copy of the GNU General Public License
  33. + * along with this program; if not, write to the Free Software
  34. + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  35. + * 02110-1301 USA
  36. + *
  37. + */
  38. +
  39. +#include <linux/module.h>
  40. +
  41. +#include <linux/kernel.h>
  42. +#include <linux/init.h>
  43. +#include <linux/errno.h>
  44. +#include <linux/delay.h>
  45. +#include <linux/spinlock.h>
  46. +#include <linux/serial_reg.h>
  47. +#include <linux/skbuff.h>
  48. +#include <linux/timer.h>
  49. +#include <linux/device.h>
  50. +#include <linux/platform_device.h>
  51. +#include <linux/clk.h>
  52. +#include <linux/gpio.h>
  53. +
  54. +#include <mach/hardware.h>
  55. +#include <mach/board.h>
  56. +#include <mach/irqs.h>
  57. +#include <plat/serial.h>
  58. +
  59. +#include <net/bluetooth/bluetooth.h>
  60. +#include <net/bluetooth/hci_core.h>
  61. +#include <net/bluetooth/hci.h>
  62. +
  63. +#include "hci_h4p.h"
  64. +
  65. +#define PM_TIMEOUT 200
  66. +
  67. +struct omap_bluetooth_config {
  68. + u8 chip_type;
  69. + u8 bt_wakeup_gpio;
  70. + u8 host_wakeup_gpio;
  71. + u8 reset_gpio;
  72. + u8 bt_uart;
  73. + u8 bd_addr[6];
  74. + u8 bt_sysclk;
  75. +};
  76. +
  77. +/* This should be used in function that cannot release clocks */
  78. +static void hci_h4p_set_clk(struct hci_h4p_info *info, int *clock, int enable)
  79. +{
  80. + unsigned long flags;
  81. +
  82. + spin_lock_irqsave(&info->clocks_lock, flags);
  83. + if (enable && !*clock) {
  84. + NBT_DBG_POWER("Enabling %p\n", clock);
  85. + clk_enable(info->uart_fclk);
  86. +#ifdef CONFIG_ARCH_OMAP2
  87. + if (cpu_is_omap24xx()) {
  88. + clk_enable(info->uart_iclk);
  89. + //omap2_block_sleep();
  90. + }
  91. +#endif
  92. + }
  93. + if (!enable && *clock) {
  94. + NBT_DBG_POWER("Disabling %p\n", clock);
  95. + clk_disable(info->uart_fclk);
  96. +#ifdef CONFIG_ARCH_OMAP2
  97. + if (cpu_is_omap24xx()) {
  98. + clk_disable(info->uart_iclk);
  99. + //omap2_allow_sleep();
  100. + }
  101. +#endif
  102. + }
  103. +
  104. + *clock = enable;
  105. + spin_unlock_irqrestore(&info->clocks_lock, flags);
  106. +}
  107. +
  108. +/* Power management functions */
  109. +static void hci_h4p_disable_tx(struct hci_h4p_info *info)
  110. +{
  111. + NBT_DBG_POWER("\n");
  112. +
  113. + if (!info->pm_enabled)
  114. + return;
  115. +
  116. + mod_timer(&info->tx_pm_timer, jiffies + msecs_to_jiffies(PM_TIMEOUT));
  117. +}
  118. +
  119. +static void hci_h4p_enable_tx(struct hci_h4p_info *info)
  120. +{
  121. + NBT_DBG_POWER("\n");
  122. +
  123. + if (!info->pm_enabled)
  124. + return;
  125. +
  126. + del_timer_sync(&info->tx_pm_timer);
  127. + if (info->tx_pm_enabled) {
  128. + info->tx_pm_enabled = 0;
  129. + hci_h4p_set_clk(info, &info->tx_clocks_en, 1);
  130. + gpio_set_value(info->bt_wakeup_gpio, 1);
  131. + }
  132. +}
  133. +
  134. +static void hci_h4p_tx_pm_timer(unsigned long data)
  135. +{
  136. + struct hci_h4p_info *info;
  137. +
  138. + NBT_DBG_POWER("\n");
  139. +
  140. + info = (struct hci_h4p_info *)data;
  141. +
  142. + if (hci_h4p_inb(info, UART_LSR) & UART_LSR_TEMT) {
  143. + gpio_set_value(info->bt_wakeup_gpio, 0);
  144. + hci_h4p_set_clk(info, &info->tx_clocks_en, 0);
  145. + info->tx_pm_enabled = 1;
  146. + }
  147. + else {
  148. + mod_timer(&info->tx_pm_timer, jiffies + msecs_to_jiffies(PM_TIMEOUT));
  149. + }
  150. +}
  151. +
  152. +static void hci_h4p_disable_rx(struct hci_h4p_info *info)
  153. +{
  154. + if (!info->pm_enabled)
  155. + return;
  156. +
  157. + mod_timer(&info->rx_pm_timer, jiffies + msecs_to_jiffies(PM_TIMEOUT));
  158. +}
  159. +
  160. +static void hci_h4p_enable_rx(struct hci_h4p_info *info)
  161. +{
  162. + unsigned long flags;
  163. +
  164. + if (!info->pm_enabled)
  165. + return;
  166. +
  167. + del_timer_sync(&info->rx_pm_timer);
  168. + spin_lock_irqsave(&info->lock, flags);
  169. + if (info->rx_pm_enabled) {
  170. + hci_h4p_set_clk(info, &info->rx_clocks_en, 1);
  171. + hci_h4p_outb(info, UART_IER, hci_h4p_inb(info, UART_IER) | UART_IER_RDI);
  172. + __hci_h4p_set_auto_ctsrts(info, 1, UART_EFR_RTS);
  173. + info->rx_pm_enabled = 0;
  174. + }
  175. + spin_unlock_irqrestore(&info->lock, flags);
  176. +}
  177. +
  178. +static void hci_h4p_rx_pm_timer(unsigned long data)
  179. +{
  180. + unsigned long flags;
  181. + struct hci_h4p_info *info = (struct hci_h4p_info *)data;
  182. +
  183. + spin_lock_irqsave(&info->lock, flags);
  184. + if (!(hci_h4p_inb(info, UART_LSR) & UART_LSR_DR)) {
  185. + __hci_h4p_set_auto_ctsrts(info, 0, UART_EFR_RTS);
  186. + hci_h4p_set_rts(info, 0);
  187. + hci_h4p_outb(info, UART_IER, hci_h4p_inb(info, UART_IER) & ~UART_IER_RDI);
  188. + hci_h4p_set_clk(info, &info->rx_clocks_en, 0);
  189. + info->rx_pm_enabled = 1;
  190. + }
  191. + else {
  192. + mod_timer(&info->rx_pm_timer, jiffies + msecs_to_jiffies(PM_TIMEOUT));
  193. + }
  194. + spin_unlock_irqrestore(&info->lock, flags);
  195. +}
  196. +
  197. +/* Negotiation functions */
  198. +int hci_h4p_send_alive_packet(struct hci_h4p_info *info)
  199. +{
  200. + NBT_DBG("Sending alive packet\n");
  201. +
  202. + if (!info->alive_cmd_skb)
  203. + return -EINVAL;
  204. +
  205. + /* Keep reference to buffer so we can reuse it */
  206. + info->alive_cmd_skb = skb_get(info->alive_cmd_skb);
  207. +
  208. + skb_queue_tail(&info->txq, info->alive_cmd_skb);
  209. + tasklet_schedule(&info->tx_task);
  210. +
  211. + NBT_DBG("Alive packet sent\n");
  212. +
  213. + return 0;
  214. +}
  215. +
  216. +static void hci_h4p_alive_packet(struct hci_h4p_info *info, struct sk_buff *skb)
  217. +{
  218. + NBT_DBG("Received alive packet\n");
  219. + if (skb->data[1] == 0xCC) {
  220. + complete(&info->init_completion);
  221. + }
  222. +
  223. + kfree_skb(skb);
  224. +}
  225. +
  226. +static int hci_h4p_send_negotiation(struct hci_h4p_info *info, struct sk_buff *skb)
  227. +{
  228. + NBT_DBG("Sending negotiation..\n");
  229. +
  230. + hci_h4p_change_speed(info, INIT_SPEED);
  231. +
  232. + info->init_error = 0;
  233. + init_completion(&info->init_completion);
  234. + skb_queue_tail(&info->txq, skb);
  235. + tasklet_schedule(&info->tx_task);
  236. +
  237. + if (!wait_for_completion_interruptible_timeout(&info->init_completion,
  238. + msecs_to_jiffies(1000)))
  239. + return -ETIMEDOUT;
  240. +
  241. + NBT_DBG("Negotiation sent\n");
  242. + return info->init_error;
  243. +}
  244. +
  245. +static void hci_h4p_negotiation_packet(struct hci_h4p_info *info,
  246. + struct sk_buff *skb)
  247. +{
  248. + int err = 0;
  249. +
  250. + if (skb->data[1] == 0x20) {
  251. + /* Change to operational settings */
  252. + hci_h4p_set_rts(info, 0);
  253. +
  254. + err = hci_h4p_wait_for_cts(info, 0, 100);
  255. + if (err < 0)
  256. + goto neg_ret;
  257. +
  258. + hci_h4p_change_speed(info, MAX_BAUD_RATE);
  259. +
  260. + err = hci_h4p_wait_for_cts(info, 1, 100);
  261. + if (err < 0)
  262. + goto neg_ret;
  263. +
  264. + hci_h4p_set_auto_ctsrts(info, 1, UART_EFR_CTS | UART_EFR_RTS);
  265. +
  266. + err = hci_h4p_send_alive_packet(info);
  267. + if (err < 0)
  268. + goto neg_ret;
  269. + } else {
  270. + dev_err(info->dev, "Could not negotiate hci_h4p settings\n");
  271. + err = -EINVAL;
  272. + goto neg_ret;
  273. + }
  274. +
  275. + kfree_skb(skb);
  276. + return;
  277. +
  278. +neg_ret:
  279. + info->init_error = err;
  280. + complete(&info->init_completion);
  281. + kfree_skb(skb);
  282. +}
  283. +
  284. +/* H4 packet handling functions */
  285. +static int hci_h4p_get_hdr_len(struct hci_h4p_info *info, u8 pkt_type)
  286. +{
  287. + long retval;
  288. +
  289. + switch (pkt_type) {
  290. + case H4_EVT_PKT:
  291. + retval = HCI_EVENT_HDR_SIZE;
  292. + break;
  293. + case H4_ACL_PKT:
  294. + retval = HCI_ACL_HDR_SIZE;
  295. + break;
  296. + case H4_SCO_PKT:
  297. + retval = HCI_SCO_HDR_SIZE;
  298. + break;
  299. + case H4_NEG_PKT:
  300. + retval = 11;
  301. + break;
  302. + case H4_ALIVE_PKT:
  303. + retval = 3;
  304. + break;
  305. + default:
  306. + dev_err(info->dev, "Unknown H4 packet type 0x%.2x\n", pkt_type);
  307. + retval = -1;
  308. + break;
  309. + }
  310. +
  311. + return retval;
  312. +}
  313. +
  314. +static unsigned int hci_h4p_get_data_len(struct hci_h4p_info *info,
  315. + struct sk_buff *skb)
  316. +{
  317. + long retval = -1;
  318. + struct hci_event_hdr *evt_hdr;
  319. + struct hci_acl_hdr *acl_hdr;
  320. + struct hci_sco_hdr *sco_hdr;
  321. +
  322. + switch (bt_cb(skb)->pkt_type) {
  323. + case H4_EVT_PKT:
  324. + evt_hdr = (struct hci_event_hdr *)skb->data;
  325. + retval = evt_hdr->plen;
  326. + break;
  327. + case H4_ACL_PKT:
  328. + acl_hdr = (struct hci_acl_hdr *)skb->data;
  329. + retval = le16_to_cpu(acl_hdr->dlen);
  330. + break;
  331. + case H4_SCO_PKT:
  332. + sco_hdr = (struct hci_sco_hdr *)skb->data;
  333. + retval = sco_hdr->dlen;
  334. + break;
  335. + case H4_NEG_PKT:
  336. + retval = 0;
  337. + break;
  338. + case H4_ALIVE_PKT:
  339. + retval = 0;
  340. + break;
  341. + }
  342. +
  343. + return retval;
  344. +}
  345. +
  346. +static inline void hci_h4p_recv_frame(struct hci_h4p_info *info,
  347. + struct sk_buff *skb)
  348. +{
  349. +
  350. + if (unlikely(!test_bit(HCI_RUNNING, &info->hdev->flags))) {
  351. + NBT_DBG("fw_event\n");
  352. + hci_h4p_parse_fw_event(info, skb);
  353. + } else {
  354. + hci_recv_frame(skb);
  355. + NBT_DBG("Frame sent to upper layer\n");
  356. + }
  357. +}
  358. +
  359. +static void hci_h4p_rx_tasklet(unsigned long data)
  360. +{
  361. + u8 byte;
  362. + unsigned long flags;
  363. + struct hci_h4p_info *info = (struct hci_h4p_info *)data;
  364. +
  365. + NBT_DBG("tasklet woke up\n");
  366. + NBT_DBG_TRANSFER("rx_tasklet woke up\ndata ");
  367. +
  368. + while (hci_h4p_inb(info, UART_LSR) & UART_LSR_DR) {
  369. + byte = hci_h4p_inb(info, UART_RX);
  370. + if (info->garbage_bytes) {
  371. + info->garbage_bytes--;
  372. + continue;
  373. + }
  374. + if (info->rx_skb == NULL) {
  375. + info->rx_skb = bt_skb_alloc(HCI_MAX_FRAME_SIZE, GFP_ATOMIC | GFP_DMA);
  376. + if (!info->rx_skb) {
  377. + dev_err(info->dev, "Can't allocate memory for new packet\n");
  378. + goto finish_task;
  379. + }
  380. + info->rx_state = WAIT_FOR_PKT_TYPE;
  381. + info->rx_skb->dev = (void *)info->hdev;
  382. + }
  383. + info->hdev->stat.byte_rx++;
  384. + NBT_DBG_TRANSFER_NF("0x%.2x ", byte);
  385. + switch (info->rx_state) {
  386. + case WAIT_FOR_PKT_TYPE:
  387. + bt_cb(info->rx_skb)->pkt_type = byte;
  388. + info->rx_count = hci_h4p_get_hdr_len(info, byte);
  389. + if (info->rx_count < 0) {
  390. + info->hdev->stat.err_rx++;
  391. + kfree_skb(info->rx_skb);
  392. + info->rx_skb = NULL;
  393. + } else {
  394. + info->rx_state = WAIT_FOR_HEADER;
  395. + }
  396. + break;
  397. + case WAIT_FOR_HEADER:
  398. + info->rx_count--;
  399. + *skb_put(info->rx_skb, 1) = byte;
  400. + if (info->rx_count == 0) {
  401. + info->rx_count = hci_h4p_get_data_len(info, info->rx_skb);
  402. + if (info->rx_count > skb_tailroom(info->rx_skb)) {
  403. + dev_err(info->dev, "Frame is %ld bytes too long.\n",
  404. + info->rx_count - skb_tailroom(info->rx_skb));
  405. + kfree_skb(info->rx_skb);
  406. + info->rx_skb = NULL;
  407. + info->garbage_bytes = info->rx_count - skb_tailroom(info->rx_skb);
  408. + break;
  409. + }
  410. + info->rx_state = WAIT_FOR_DATA;
  411. +
  412. + if (bt_cb(info->rx_skb)->pkt_type == H4_NEG_PKT) {
  413. + hci_h4p_negotiation_packet(info, info->rx_skb);
  414. + info->rx_skb = NULL;
  415. + info->rx_state = WAIT_FOR_PKT_TYPE;
  416. + goto finish_task;
  417. + }
  418. + if (bt_cb(info->rx_skb)->pkt_type == H4_ALIVE_PKT) {
  419. + hci_h4p_alive_packet(info, info->rx_skb);
  420. + info->rx_skb = NULL;
  421. + info->rx_state = WAIT_FOR_PKT_TYPE;
  422. + goto finish_task;
  423. + }
  424. + }
  425. + break;
  426. + case WAIT_FOR_DATA:
  427. + info->rx_count--;
  428. + *skb_put(info->rx_skb, 1) = byte;
  429. + if (info->rx_count == 0) {
  430. + /* H4+ devices should allways send word aligned packets */
  431. + if (!(info->rx_skb->len % 2)) {
  432. + info->garbage_bytes++;
  433. + }
  434. + hci_h4p_recv_frame(info, info->rx_skb);
  435. + info->rx_skb = NULL;
  436. + }
  437. + break;
  438. + default:
  439. + WARN_ON(1);
  440. + break;
  441. + }
  442. + }
  443. +
  444. +finish_task:
  445. + spin_lock_irqsave(&info->lock, flags);
  446. + hci_h4p_outb(info, UART_IER, hci_h4p_inb(info, UART_IER) | UART_IER_RDI);
  447. + spin_unlock_irqrestore(&info->lock, flags);
  448. +
  449. + NBT_DBG_TRANSFER_NF("\n");
  450. + NBT_DBG("rx_ended\n");
  451. +}
  452. +
  453. +static void hci_h4p_tx_tasklet(unsigned long data)
  454. +{
  455. + unsigned int sent = 0;
  456. + unsigned long flags;
  457. + struct sk_buff *skb;
  458. + struct hci_h4p_info *info = (struct hci_h4p_info *)data;
  459. +
  460. + NBT_DBG("tasklet woke up\n");
  461. + NBT_DBG_TRANSFER("tx_tasklet woke up\n data ");
  462. +
  463. + skb = skb_dequeue(&info->txq);
  464. + if (!skb) {
  465. + /* No data in buffer */
  466. + NBT_DBG("skb ready\n");
  467. + hci_h4p_disable_tx(info);
  468. + return;
  469. + }
  470. +
  471. + /* Copy data to tx fifo */
  472. + while (!(hci_h4p_inb(info, UART_OMAP_SSR) & UART_OMAP_SSR_TXFULL) &&
  473. + (sent < skb->len)) {
  474. + NBT_DBG_TRANSFER_NF("0x%.2x ", skb->data[sent]);
  475. + hci_h4p_outb(info, UART_TX, skb->data[sent]);
  476. + sent++;
  477. + }
  478. +
  479. + info->hdev->stat.byte_tx += sent;
  480. + NBT_DBG_TRANSFER_NF("\n");
  481. + if (skb->len == sent) {
  482. + kfree_skb(skb);
  483. + } else {
  484. + skb_pull(skb, sent);
  485. + skb_queue_head(&info->txq, skb);
  486. + }
  487. +
  488. + spin_lock_irqsave(&info->lock, flags);
  489. + hci_h4p_outb(info, UART_IER, hci_h4p_inb(info, UART_IER) | UART_IER_THRI);
  490. + spin_unlock_irqrestore(&info->lock, flags);
  491. +}
  492. +
  493. +static irqreturn_t hci_h4p_interrupt(int irq, void *data)
  494. +{
  495. + struct hci_h4p_info *info = (struct hci_h4p_info *)data;
  496. + u8 iir, msr;
  497. + int ret;
  498. + unsigned long flags;
  499. +
  500. + ret = IRQ_NONE;
  501. +
  502. + iir = hci_h4p_inb(info, UART_IIR);
  503. + if (iir & UART_IIR_NO_INT) {
  504. + dev_err(info->dev, "Interrupt but no reason irq 0x%.2x\n", iir);
  505. + return IRQ_HANDLED;
  506. + }
  507. +
  508. + NBT_DBG("In interrupt handler iir 0x%.2x\n", iir);
  509. +
  510. + iir &= UART_IIR_ID;
  511. +
  512. + if (iir == UART_IIR_MSI) {
  513. + msr = hci_h4p_inb(info, UART_MSR);
  514. + ret = IRQ_HANDLED;
  515. + }
  516. + if (iir == UART_IIR_RLSI) {
  517. + hci_h4p_inb(info, UART_RX);
  518. + hci_h4p_inb(info, UART_LSR);
  519. + ret = IRQ_HANDLED;
  520. + }
  521. +
  522. + if (iir == UART_IIR_RDI) {
  523. + spin_lock_irqsave(&info->lock, flags);
  524. + hci_h4p_outb(info, UART_IER, hci_h4p_inb(info, UART_IER) & ~UART_IER_RDI);
  525. + spin_unlock_irqrestore(&info->lock, flags);
  526. + tasklet_schedule(&info->rx_task);
  527. + ret = IRQ_HANDLED;
  528. + }
  529. +
  530. + if (iir == UART_IIR_THRI) {
  531. + spin_lock_irqsave(&info->lock, flags);
  532. + hci_h4p_outb(info, UART_IER, hci_h4p_inb(info, UART_IER) & ~UART_IER_THRI);
  533. + spin_unlock_irqrestore(&info->lock, flags);
  534. + tasklet_schedule(&info->tx_task);
  535. + ret = IRQ_HANDLED;
  536. + }
  537. +
  538. + return ret;
  539. +}
  540. +
  541. +static irqreturn_t hci_h4p_wakeup_interrupt(int irq, void *dev_inst)
  542. +{
  543. + struct hci_h4p_info *info = dev_inst;
  544. + int should_wakeup;
  545. + struct hci_dev *hdev;
  546. +
  547. + if (!info->hdev)
  548. + return IRQ_HANDLED;
  549. +
  550. + hdev = info->hdev;
  551. +
  552. + if (!test_bit(HCI_RUNNING, &hdev->flags))
  553. + return IRQ_HANDLED;
  554. +
  555. + should_wakeup = gpio_get_value(info->host_wakeup_gpio);
  556. + NBT_DBG_POWER("gpio interrupt %d\n", should_wakeup);
  557. + if (should_wakeup) {
  558. + hci_h4p_enable_rx(info);
  559. + } else {
  560. + hci_h4p_disable_rx(info);
  561. + }
  562. +
  563. + return IRQ_HANDLED;
  564. +}
  565. +
  566. +static int hci_h4p_reset(struct hci_h4p_info *info)
  567. +{
  568. + int err;
  569. +
  570. + hci_h4p_init_uart(info);
  571. + hci_h4p_set_rts(info, 0);
  572. +
  573. + gpio_set_value(info->reset_gpio, 0);
  574. + msleep(100);
  575. + gpio_set_value(info->bt_wakeup_gpio, 1);
  576. + gpio_set_value(info->reset_gpio, 1);
  577. + msleep(100);
  578. +
  579. + err = hci_h4p_wait_for_cts(info, 1, 10);
  580. + if (err < 0) {
  581. + dev_err(info->dev, "No cts from bt chip\n");
  582. + return err;
  583. + }
  584. +
  585. + hci_h4p_set_rts(info, 1);
  586. +
  587. + return 0;
  588. +}
  589. +
  590. +/* hci callback functions */
  591. +static int hci_h4p_hci_flush(struct hci_dev *hdev)
  592. +{
  593. + struct hci_h4p_info *info;
  594. + info = hdev->driver_data;
  595. +
  596. + skb_queue_purge(&info->txq);
  597. +
  598. + return 0;
  599. +}
  600. +
  601. +static int hci_h4p_hci_open(struct hci_dev *hdev)
  602. +{
  603. + struct hci_h4p_info *info;
  604. + int err;
  605. + struct sk_buff *neg_cmd_skb;
  606. + struct sk_buff_head fw_queue;
  607. +
  608. + info = hdev->driver_data;
  609. +
  610. + if (test_bit(HCI_RUNNING, &hdev->flags))
  611. + return 0;
  612. +
  613. + skb_queue_head_init(&fw_queue);
  614. + err = hci_h4p_read_fw(info, &fw_queue);
  615. + if (err < 0) {
  616. + dev_err(info->dev, "Cannot read firmware\n");
  617. + return err;
  618. + }
  619. + neg_cmd_skb = skb_dequeue(&fw_queue);
  620. + if (!neg_cmd_skb) {
  621. + err = -EPROTO;
  622. + goto err_clean;
  623. + }
  624. + info->alive_cmd_skb = skb_dequeue(&fw_queue);
  625. + if (!info->alive_cmd_skb) {
  626. + err = -EPROTO;
  627. + goto err_clean;
  628. + }
  629. +
  630. + hci_h4p_set_clk(info, &info->tx_clocks_en, 1);
  631. + hci_h4p_set_clk(info, &info->rx_clocks_en, 1);
  632. +
  633. + tasklet_enable(&info->tx_task);
  634. + tasklet_enable(&info->rx_task);
  635. + info->rx_state = WAIT_FOR_PKT_TYPE;
  636. + info->rx_count = 0;
  637. + info->garbage_bytes = 0;
  638. + info->rx_skb = NULL;
  639. + info->pm_enabled = 0;
  640. + init_completion(&info->fw_completion);
  641. +
  642. + err = hci_h4p_reset(info);
  643. + if (err < 0)
  644. + goto err_clean;
  645. +
  646. + err = hci_h4p_send_negotiation(info, neg_cmd_skb);
  647. + neg_cmd_skb = NULL;
  648. + if (err < 0)
  649. + goto err_clean;
  650. +
  651. + err = hci_h4p_send_fw(info, &fw_queue);
  652. + if (err < 0) {
  653. + dev_err(info->dev, "Sending firmware failed.\n");
  654. + goto err_clean;
  655. + }
  656. +
  657. + kfree_skb(info->alive_cmd_skb);
  658. + info->alive_cmd_skb = NULL;
  659. + info->pm_enabled = 1;
  660. + info->tx_pm_enabled = 1;
  661. + info->rx_pm_enabled = 0;
  662. + set_bit(HCI_RUNNING, &hdev->flags);
  663. +
  664. + NBT_DBG("hci up and running\n");
  665. + return 0;
  666. +
  667. +err_clean:
  668. + hci_h4p_hci_flush(hdev);
  669. + tasklet_disable(&info->tx_task);
  670. + tasklet_disable(&info->rx_task);
  671. + hci_h4p_reset_uart(info);
  672. + hci_h4p_set_clk(info, &info->tx_clocks_en, 0);
  673. + hci_h4p_set_clk(info, &info->rx_clocks_en, 0);
  674. + gpio_set_value(info->reset_gpio, 0);
  675. + gpio_set_value(info->bt_wakeup_gpio, 0);
  676. + skb_queue_purge(&fw_queue);
  677. + kfree_skb(neg_cmd_skb);
  678. + neg_cmd_skb = NULL;
  679. + kfree_skb(info->alive_cmd_skb);
  680. + info->alive_cmd_skb = NULL;
  681. + kfree_skb(info->rx_skb);
  682. +
  683. + return err;
  684. +}
  685. +
  686. +static int hci_h4p_hci_close(struct hci_dev *hdev)
  687. +{
  688. + struct hci_h4p_info *info = hdev->driver_data;
  689. +
  690. + if (!test_and_clear_bit(HCI_RUNNING, &hdev->flags))
  691. + return 0;
  692. +
  693. + hci_h4p_hci_flush(hdev);
  694. + del_timer_sync(&info->tx_pm_timer);
  695. + del_timer_sync(&info->rx_pm_timer);
  696. + tasklet_disable(&info->tx_task);
  697. + tasklet_disable(&info->rx_task);
  698. + hci_h4p_set_clk(info, &info->tx_clocks_en, 1);
  699. + hci_h4p_set_clk(info, &info->rx_clocks_en, 1);
  700. + hci_h4p_reset_uart(info);
  701. + hci_h4p_set_clk(info, &info->tx_clocks_en, 0);
  702. + hci_h4p_set_clk(info, &info->rx_clocks_en, 0);
  703. + gpio_set_value(info->reset_gpio, 0);
  704. + gpio_set_value(info->bt_wakeup_gpio, 0);
  705. + kfree_skb(info->rx_skb);
  706. +
  707. + return 0;
  708. +}
  709. +
  710. +static void hci_h4p_hci_destruct(struct hci_dev *hdev)
  711. +{
  712. +}
  713. +
  714. +static int hci_h4p_hci_send_frame(struct sk_buff *skb)
  715. +{
  716. + struct hci_h4p_info *info;
  717. + struct hci_dev *hdev = (struct hci_dev *)skb->dev;
  718. + int err = 0;
  719. +
  720. + if (!hdev) {
  721. + printk(KERN_WARNING "hci_h4p: Frame for unknown device\n");
  722. + return -ENODEV;
  723. + }
  724. +
  725. + NBT_DBG("dev %p, skb %p\n", hdev, skb);
  726. +
  727. + info = hdev->driver_data;
  728. +
  729. + if (!test_bit(HCI_RUNNING, &hdev->flags)) {
  730. + dev_warn(info->dev, "Frame for non-running device\n");
  731. + return -EIO;
  732. + }
  733. +
  734. + switch (bt_cb(skb)->pkt_type) {
  735. + case HCI_COMMAND_PKT:
  736. + hdev->stat.cmd_tx++;
  737. + break;
  738. + case HCI_ACLDATA_PKT:
  739. + hdev->stat.acl_tx++;
  740. + break;
  741. + case HCI_SCODATA_PKT:
  742. + hdev->stat.sco_tx++;
  743. + break;
  744. + }
  745. +
  746. + /* Push frame type to skb */
  747. + *skb_push(skb, 1) = (bt_cb(skb)->pkt_type);
  748. + /* We should allways send word aligned data to h4+ devices */
  749. + if (skb->len % 2) {
  750. + err = skb_pad(skb, 1);
  751. + }
  752. + if (err)
  753. + return err;
  754. +
  755. + hci_h4p_enable_tx(info);
  756. + skb_queue_tail(&info->txq, skb);
  757. + tasklet_schedule(&info->tx_task);
  758. +
  759. + return 0;
  760. +}
  761. +
  762. +static int hci_h4p_hci_ioctl(struct hci_dev *hdev, unsigned int cmd, unsigned long arg)
  763. +{
  764. + return -ENOIOCTLCMD;
  765. +}
  766. +
  767. +static int hci_h4p_register_hdev(struct hci_h4p_info *info)
  768. +{
  769. + struct hci_dev *hdev;
  770. +
  771. + /* Initialize and register HCI device */
  772. +
  773. + hdev = hci_alloc_dev();
  774. + if (!hdev) {
  775. + dev_err(info->dev, "Can't allocate memory for device\n");
  776. + return -ENOMEM;
  777. + }
  778. + info->hdev = hdev;
  779. +
  780. + hdev->dev_type = HCI_UART;
  781. + hdev->driver_data = info;
  782. +
  783. + hdev->open = hci_h4p_hci_open;
  784. + hdev->close = hci_h4p_hci_close;
  785. + hdev->flush = hci_h4p_hci_flush;
  786. + hdev->send = hci_h4p_hci_send_frame;
  787. + hdev->destruct = hci_h4p_hci_destruct;
  788. + hdev->ioctl = hci_h4p_hci_ioctl;
  789. +
  790. + hdev->owner = THIS_MODULE;
  791. +
  792. + if (hci_register_dev(hdev) < 0) {
  793. + dev_err(info->dev, "hci_h4p: Can't register HCI device %s.\n", hdev->name);
  794. + return -ENODEV;
  795. + }
  796. +
  797. + return 0;
  798. +}
  799. +
  800. +static int hci_h4p_probe(struct platform_device *pdev)
  801. +{
  802. + struct omap_bluetooth_config *bt_config;
  803. + struct hci_h4p_info *info;
  804. + int irq, err;
  805. +
  806. + dev_info(&pdev->dev, "Registering HCI H4P device\n");
  807. + info = kzalloc(sizeof(struct hci_h4p_info), GFP_KERNEL);
  808. + if (!info)
  809. + return -ENOMEM;
  810. +
  811. + info->dev = &pdev->dev;
  812. + info->pm_enabled = 0;
  813. + info->tx_pm_enabled = 0;
  814. + info->rx_pm_enabled = 0;
  815. + info->garbage_bytes = 0;
  816. + info->tx_clocks_en = 0;
  817. + info->rx_clocks_en = 0;
  818. + tasklet_init(&info->tx_task, hci_h4p_tx_tasklet, (unsigned long)info);
  819. + tasklet_init(&info->rx_task, hci_h4p_rx_tasklet, (unsigned long)info);
  820. + /* hci_h4p_hci_open assumes that tasklet is disabled in startup */
  821. + tasklet_disable(&info->tx_task);
  822. + tasklet_disable(&info->rx_task);
  823. + spin_lock_init(&info->lock);
  824. + spin_lock_init(&info->clocks_lock);
  825. + skb_queue_head_init(&info->txq);
  826. + init_timer(&info->tx_pm_timer);
  827. + info->tx_pm_timer.function = hci_h4p_tx_pm_timer;
  828. + info->tx_pm_timer.data = (unsigned long)info;
  829. + init_timer(&info->rx_pm_timer);
  830. + info->rx_pm_timer.function = hci_h4p_rx_pm_timer;
  831. + info->rx_pm_timer.data = (unsigned long)info;
  832. +
  833. + if (pdev->dev.platform_data == NULL) {
  834. + dev_err(&pdev->dev, "Could not get Bluetooth config data\n");
  835. + return -ENODATA;
  836. + }
  837. +
  838. + bt_config = pdev->dev.platform_data;
  839. + info->chip_type = bt_config->chip_type;
  840. + info->bt_wakeup_gpio = bt_config->bt_wakeup_gpio;
  841. + info->host_wakeup_gpio = bt_config->host_wakeup_gpio;
  842. + info->reset_gpio = bt_config->reset_gpio;
  843. + info->bt_sysclk = bt_config->bt_sysclk;
  844. +
  845. + NBT_DBG("RESET gpio: %d\n", info->reset_gpio);
  846. + NBT_DBG("BTWU gpio: %d\n", info->bt_wakeup_gpio);
  847. + NBT_DBG("HOSTWU gpio: %d\n", info->host_wakeup_gpio);
  848. + NBT_DBG("Uart: %d\n", bt_config->bt_uart);
  849. + NBT_DBG("sysclk: %d\n", info->bt_sysclk);
  850. +
  851. + err = gpio_request(info->reset_gpio, "BT reset");
  852. + if (err < 0) {
  853. + dev_err(&pdev->dev, "Cannot get GPIO line %d\n",
  854. + info->reset_gpio);
  855. + kfree(info);
  856. + goto cleanup;
  857. + }
  858. +
  859. + err = gpio_request(info->bt_wakeup_gpio, "BT wakeup");
  860. + if (err < 0)
  861. + {
  862. + dev_err(info->dev, "Cannot get GPIO line 0x%d",
  863. + info->bt_wakeup_gpio);
  864. + gpio_free(info->reset_gpio);
  865. + kfree(info);
  866. + goto cleanup;
  867. + }
  868. +
  869. + err = gpio_request(info->host_wakeup_gpio, "BT host wakeup");
  870. + if (err < 0)
  871. + {
  872. + dev_err(info->dev, "Cannot get GPIO line %d",
  873. + info->host_wakeup_gpio);
  874. + gpio_free(info->reset_gpio);
  875. + gpio_free(info->bt_wakeup_gpio);
  876. + kfree(info);
  877. + goto cleanup;
  878. + }
  879. +
  880. + gpio_direction_output(info->reset_gpio, 0);
  881. + gpio_direction_output(info->bt_wakeup_gpio, 0);
  882. + gpio_direction_input(info->host_wakeup_gpio);
  883. +
  884. +//FIXME
  885. +#if defined(CONFIG_ARCH_OMAP1)
  886. +# define OMAP_UART1_BASE OMAP1_UART1_BASE
  887. +# define OMAP_UART2_BASE OMAP1_UART2_BASE
  888. +# define OMAP_UART3_BASE OMAP1_UART3_BASE
  889. +#elif defined(CONFIG_ARCH_OMAP2)
  890. +# define OMAP_UART1_BASE OMAP2_UART1_BASE
  891. +# define OMAP_UART2_BASE OMAP2_UART2_BASE
  892. +# define OMAP_UART3_BASE OMAP2_UART3_BASE
  893. +#elif defined(CONFIG_ARCH_OMAP3)
  894. +# define OMAP_UART1_BASE OMAP3_UART1_BASE
  895. +# define OMAP_UART2_BASE OMAP3_UART2_BASE
  896. +# define OMAP_UART3_BASE OMAP3_UART3_BASE
  897. +#elif defined(CONFIG_ARCH_OMAP4)
  898. +# define OMAP_UART1_BASE OMAP4_UART1_BASE
  899. +# define OMAP_UART2_BASE OMAP4_UART2_BASE
  900. +# define OMAP_UART3_BASE OMAP4_UART3_BASE
  901. +#else
  902. +# error
  903. +#endif
  904. + switch (bt_config->bt_uart) {
  905. + case 1:
  906. + if (cpu_is_omap16xx()) {
  907. + irq = INT_UART1;
  908. + info->uart_fclk = clk_get(NULL, "uart1_ck");
  909. + } else if (cpu_is_omap24xx()) {
  910. + irq = INT_24XX_UART1_IRQ;
  911. + info->uart_iclk = clk_get(NULL, "uart1_ick");
  912. + info->uart_fclk = clk_get(NULL, "uart1_fck");
  913. + }
  914. + /* FIXME: Use platform_get_resource for the port */
  915. + info->uart_base = ioremap(OMAP_UART1_BASE, 0x16);
  916. + if (!info->uart_base)
  917. + goto cleanup;
  918. + break;
  919. + case 2:
  920. + if (cpu_is_omap16xx()) {
  921. + irq = INT_UART2;
  922. + info->uart_fclk = clk_get(NULL, "uart2_ck");
  923. + } else {
  924. + irq = INT_24XX_UART2_IRQ;
  925. + info->uart_iclk = clk_get(NULL, "uart2_ick");
  926. + info->uart_fclk = clk_get(NULL, "uart2_fck");
  927. + }
  928. + /* FIXME: Use platform_get_resource for the port */
  929. + info->uart_base = ioremap(OMAP_UART2_BASE, 0x16);
  930. + if (!info->uart_base)
  931. + goto cleanup;
  932. + break;
  933. + case 3:
  934. + if (cpu_is_omap16xx()) {
  935. + irq = INT_UART3;
  936. + info->uart_fclk = clk_get(NULL, "uart3_ck");
  937. + } else {
  938. + irq = INT_24XX_UART3_IRQ;
  939. + info->uart_iclk = clk_get(NULL, "uart3_ick");
  940. + info->uart_fclk = clk_get(NULL, "uart3_fck");
  941. + }
  942. + /* FIXME: Use platform_get_resource for the port */
  943. + info->uart_base = ioremap(OMAP_UART3_BASE, 0x16);
  944. + if (!info->uart_base)
  945. + goto cleanup;
  946. + break;
  947. + default:
  948. + dev_err(info->dev, "No uart defined\n");
  949. + goto cleanup;
  950. + }
  951. +
  952. + info->irq = irq;
  953. + err = request_irq(irq, hci_h4p_interrupt, 0, "hci_h4p", (void *)info);
  954. + if (err < 0) {
  955. + dev_err(info->dev, "hci_h4p: unable to get IRQ %d\n", irq);
  956. + goto cleanup;
  957. + }
  958. +
  959. + err = request_irq(gpio_to_irq(info->host_wakeup_gpio),
  960. + hci_h4p_wakeup_interrupt,
  961. + IRQF_TRIGGER_FALLING | IRQF_TRIGGER_RISING,
  962. + "hci_h4p_wkup", (void *)info);
  963. + if (err < 0) {
  964. + dev_err(info->dev, "hci_h4p: unable to get wakeup IRQ %d\n",
  965. + gpio_to_irq(info->host_wakeup_gpio));
  966. + free_irq(irq, (void *)info);
  967. + goto cleanup;
  968. + }
  969. +
  970. + hci_h4p_set_clk(info, &info->tx_clocks_en, 1);
  971. + hci_h4p_set_auto_ctsrts(info, 0, UART_EFR_CTS | UART_EFR_RTS);
  972. + err = hci_h4p_init_uart(info);
  973. + if (err < 0)
  974. + goto cleanup_irq;
  975. + err = hci_h4p_reset(info);
  976. + if (err < 0)
  977. + goto cleanup_irq;
  978. + err = hci_h4p_wait_for_cts(info, 1, 10);
  979. + if (err < 0)
  980. + goto cleanup_irq;
  981. + hci_h4p_set_clk(info, &info->tx_clocks_en, 0);
  982. +
  983. + platform_set_drvdata(pdev, info);
  984. + err = hci_h4p_sysfs_create_files(info->dev);
  985. + if (err < 0)
  986. + goto cleanup_irq;
  987. +
  988. + if (hci_h4p_register_hdev(info) < 0) {
  989. + dev_err(info->dev, "failed to register hci_h4p hci device\n");
  990. + goto cleanup_irq;
  991. + }
  992. + gpio_set_value(info->reset_gpio, 0);
  993. +
  994. + return 0;
  995. +
  996. +cleanup_irq:
  997. + free_irq(irq, (void *)info);
  998. + free_irq(gpio_to_irq(info->host_wakeup_gpio), (void *)info);
  999. +cleanup:
  1000. + gpio_set_value(info->reset_gpio, 0);
  1001. + gpio_free(info->reset_gpio);
  1002. + gpio_free(info->bt_wakeup_gpio);
  1003. + gpio_free(info->host_wakeup_gpio);
  1004. + kfree(info);
  1005. +
  1006. + return err;
  1007. +
  1008. +}
  1009. +
  1010. +static int hci_h4p_remove(struct platform_device *dev)
  1011. +{
  1012. + struct hci_h4p_info *info;
  1013. +
  1014. + info = platform_get_drvdata(dev);
  1015. +
  1016. + hci_h4p_hci_close(info->hdev);
  1017. + free_irq(gpio_to_irq(info->host_wakeup_gpio), (void *) info);
  1018. + hci_free_dev(info->hdev);
  1019. + gpio_free(info->reset_gpio);
  1020. + gpio_free(info->bt_wakeup_gpio);
  1021. + gpio_free(info->host_wakeup_gpio);
  1022. + free_irq(info->irq, (void *) info);
  1023. + kfree(info);
  1024. +
  1025. + return 0;
  1026. +}
  1027. +
  1028. +static struct platform_driver hci_h4p_driver = {
  1029. + .probe = hci_h4p_probe,
  1030. + .remove = hci_h4p_remove,
  1031. + .driver = {
  1032. + .name = "hci_h4p",
  1033. + },
  1034. +};
  1035. +
  1036. +static int __init hci_h4p_init(void)
  1037. +{
  1038. + int err = 0;
  1039. +
  1040. + /* Register the driver with LDM */
  1041. + err = platform_driver_register(&hci_h4p_driver);
  1042. + if (err < 0)
  1043. + printk(KERN_WARNING "failed to register hci_h4p driver\n");
  1044. +
  1045. + return err;
  1046. +}
  1047. +
  1048. +static void __exit hci_h4p_exit(void)
  1049. +{
  1050. + platform_driver_unregister(&hci_h4p_driver);
  1051. +}
  1052. +
  1053. +module_init(hci_h4p_init);
  1054. +module_exit(hci_h4p_exit);
  1055. +
  1056. +MODULE_DESCRIPTION("h4 driver with nokia extensions");
  1057. +MODULE_LICENSE("GPL");
  1058. +MODULE_AUTHOR("Ville Tervo");
  1059. --- /dev/null
  1060. +++ b/drivers/bluetooth/hci_h4p/fw.c
  1061. @@ -0,0 +1,155 @@
  1062. +/*
  1063. + * This file is part of hci_h4p bluetooth driver
  1064. + *
  1065. + * Copyright (C) 2005, 2006 Nokia Corporation.
  1066. + *
  1067. + * Contact: Ville Tervo <[email protected]>
  1068. + *
  1069. + * This program is free software; you can redistribute it and/or
  1070. + * modify it under the terms of the GNU General Public License
  1071. + * version 2 as published by the Free Software Foundation.
  1072. + *
  1073. + * This program is distributed in the hope that it will be useful, but
  1074. + * WITHOUT ANY WARRANTY; without even the implied warranty of
  1075. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  1076. + * General Public License for more details.
  1077. + *
  1078. + * You should have received a copy of the GNU General Public License
  1079. + * along with this program; if not, write to the Free Software
  1080. + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  1081. + * 02110-1301 USA
  1082. + *
  1083. + */
  1084. +
  1085. +#include <linux/skbuff.h>
  1086. +#include <linux/firmware.h>
  1087. +#include <linux/clk.h>
  1088. +
  1089. +#include <net/bluetooth/bluetooth.h>
  1090. +
  1091. +#include "hci_h4p.h"
  1092. +
  1093. +#define BT_CHIP_TI 2
  1094. +#define BT_CHIP_CSR 1
  1095. +
  1096. +static int fw_pos;
  1097. +
  1098. +/* Firmware handling */
  1099. +static int hci_h4p_open_firmware(struct hci_h4p_info *info,
  1100. + const struct firmware **fw_entry)
  1101. +{
  1102. + int err;
  1103. +
  1104. + fw_pos = 0;
  1105. + NBT_DBG_FW("Opening %d firmware\n", info->chip_type);
  1106. + switch (info->chip_type) {
  1107. + case BT_CHIP_TI:
  1108. + err = request_firmware(fw_entry, "brf6150fw.bin", info->dev);
  1109. + break;
  1110. + case BT_CHIP_CSR:
  1111. + err = request_firmware(fw_entry, "bc4fw.bin", info->dev);
  1112. + break;
  1113. + default:
  1114. + dev_err(info->dev, "Invalid chip type\n");
  1115. + *fw_entry = NULL;
  1116. + err = -EINVAL;
  1117. + }
  1118. +
  1119. + return err;
  1120. +}
  1121. +
  1122. +static void hci_h4p_close_firmware(const struct firmware *fw_entry)
  1123. +{
  1124. + release_firmware(fw_entry);
  1125. +}
  1126. +
  1127. +/* Read fw. Return length of the command. If no more commands in
  1128. + * fw 0 is returned. In error case return value is negative.
  1129. + */
  1130. +static int hci_h4p_read_fw_cmd(struct hci_h4p_info *info, struct sk_buff **skb,
  1131. + const struct firmware *fw_entry, int how)
  1132. +{
  1133. + unsigned int cmd_len;
  1134. +
  1135. + if (fw_pos >= fw_entry->size) {
  1136. + return 0;
  1137. + }
  1138. +
  1139. + cmd_len = fw_entry->data[fw_pos++];
  1140. + if (!cmd_len)
  1141. + return 0;
  1142. +
  1143. + if (fw_pos + cmd_len > fw_entry->size) {
  1144. + dev_err(info->dev, "Corrupted firmware image\n");
  1145. + return -EMSGSIZE;
  1146. + }
  1147. +
  1148. + *skb = bt_skb_alloc(cmd_len, how);
  1149. + if (!*skb) {
  1150. + dev_err(info->dev, "Cannot reserve memory for buffer\n");
  1151. + return -ENOMEM;
  1152. + }
  1153. + memcpy(skb_put(*skb, cmd_len), &fw_entry->data[fw_pos], cmd_len);
  1154. +
  1155. + fw_pos += cmd_len;
  1156. +
  1157. + return (*skb)->len;
  1158. +}
  1159. +
  1160. +int hci_h4p_read_fw(struct hci_h4p_info *info, struct sk_buff_head *fw_queue)
  1161. +{
  1162. + const struct firmware *fw_entry = NULL;
  1163. + struct sk_buff *skb = NULL;
  1164. + int err;
  1165. +
  1166. + err = hci_h4p_open_firmware(info, &fw_entry);
  1167. + if (err < 0 || !fw_entry)
  1168. + goto err_clean;
  1169. +
  1170. + while ((err = hci_h4p_read_fw_cmd(info, &skb, fw_entry, GFP_KERNEL))) {
  1171. + if (err < 0 || !skb)
  1172. + goto err_clean;
  1173. +
  1174. + skb_queue_tail(fw_queue, skb);
  1175. + }
  1176. +
  1177. +err_clean:
  1178. + hci_h4p_close_firmware(fw_entry);
  1179. + return err;
  1180. +}
  1181. +
  1182. +int hci_h4p_send_fw(struct hci_h4p_info *info, struct sk_buff_head *fw_queue)
  1183. +{
  1184. + int err;
  1185. +
  1186. + switch(info->chip_type) {
  1187. + case BT_CHIP_CSR:
  1188. + err = hci_h4p_bc4_send_fw(info, fw_queue);
  1189. + break;
  1190. + case BT_CHIP_TI:
  1191. + err = hci_h4p_brf6150_send_fw(info, fw_queue);
  1192. + break;
  1193. + default:
  1194. + dev_err(info->dev, "Don't know how to send firmware\n");
  1195. + err = -EINVAL;
  1196. + }
  1197. +
  1198. + return err;
  1199. +}
  1200. +
  1201. +void hci_h4p_parse_fw_event(struct hci_h4p_info *info, struct sk_buff *skb)
  1202. +{
  1203. + switch (info->chip_type) {
  1204. + case BT_CHIP_CSR:
  1205. + hci_h4p_bc4_parse_fw_event(info, skb);
  1206. + break;
  1207. + case BT_CHIP_TI:
  1208. + hci_h4p_brf6150_parse_fw_event(info, skb);
  1209. + break;
  1210. + default:
  1211. + dev_err(info->dev, "Don't know how to parse fw event\n");
  1212. + info->fw_error = -EINVAL;
  1213. + }
  1214. +
  1215. + return;
  1216. +}
  1217. --- /dev/null
  1218. +++ b/drivers/bluetooth/hci_h4p/fw-csr.c
  1219. @@ -0,0 +1,149 @@
  1220. +/*
  1221. + * This file is part of hci_h4p bluetooth driver
  1222. + *
  1223. + * Copyright (C) 2005, 2006 Nokia Corporation.
  1224. + *
  1225. + * Contact: Ville Tervo <[email protected]>
  1226. + *
  1227. + * This program is free software; you can redistribute it and/or
  1228. + * modify it under the terms of the GNU General Public License
  1229. + * version 2 as published by the Free Software Foundation.
  1230. + *
  1231. + * This program is distributed in the hope that it will be useful, but
  1232. + * WITHOUT ANY WARRANTY; without even the implied warranty of
  1233. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  1234. + * General Public License for more details.
  1235. + *
  1236. + * You should have received a copy of the GNU General Public License
  1237. + * along with this program; if not, write to the Free Software
  1238. + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  1239. + * 02110-1301 USA
  1240. + *
  1241. + */
  1242. +
  1243. +#include <linux/skbuff.h>
  1244. +#include <linux/delay.h>
  1245. +#include <linux/serial_reg.h>
  1246. +
  1247. +#include "hci_h4p.h"
  1248. +
  1249. +void hci_h4p_bc4_parse_fw_event(struct hci_h4p_info *info, struct sk_buff *skb)
  1250. +{
  1251. + /* Check if this is fw packet */
  1252. + if (skb->data[0] != 0xff) {
  1253. + hci_recv_frame(skb);
  1254. + return;
  1255. + }
  1256. +
  1257. + if (skb->data[11] || skb->data[12]) {
  1258. + dev_err(info->dev, "Firmware sending command failed\n");
  1259. + info->fw_error = -EPROTO;
  1260. + }
  1261. +
  1262. + kfree_skb(skb);
  1263. + complete(&info->fw_completion);
  1264. +}
  1265. +
  1266. +int hci_h4p_bc4_send_fw(struct hci_h4p_info *info,
  1267. + struct sk_buff_head *fw_queue)
  1268. +{
  1269. + struct sk_buff *skb;
  1270. + unsigned int offset;
  1271. + int retries, count, i;
  1272. +
  1273. + info->fw_error = 0;
  1274. +
  1275. + NBT_DBG_FW("Sending firmware\n");
  1276. + skb = skb_dequeue(fw_queue);
  1277. +
  1278. + if (!skb)
  1279. + return -ENOMSG;
  1280. +
  1281. + info->bdaddr[0] = 0x00;
  1282. + info->bdaddr[1] = 0x1D;
  1283. + info->bdaddr[2] = 0x6E;
  1284. + info->bdaddr[3] = 0xD4;
  1285. + info->bdaddr[4] = 0xF0;
  1286. + info->bdaddr[5] = 0x37;
  1287. +
  1288. + /* Check if this is bd_address packet */
  1289. + if (skb->data[15] == 0x01 && skb->data[16] == 0x00) {
  1290. + dev_info(info->dev, "bd_address packet found\n");
  1291. + offset = 21;
  1292. + skb->data[offset + 1] = 0x00;
  1293. + skb->data[offset + 5] = 0x00;
  1294. + skb->data[offset + 7] = info->bdaddr[0];
  1295. + skb->data[offset + 6] = info->bdaddr[1];
  1296. + skb->data[offset + 4] = info->bdaddr[2];
  1297. + skb->data[offset + 0] = info->bdaddr[3];
  1298. + skb->data[offset + 3] = info->bdaddr[4];
  1299. + skb->data[offset + 2] = info->bdaddr[5];
  1300. + }
  1301. +
  1302. + for (i = 0; i < 6; i++) {
  1303. + if (info->bdaddr[i] != 0x00)
  1304. + break;
  1305. + }
  1306. +
  1307. + /* if (i > 5) {
  1308. + dev_info(info->dev, "Valid bluetooth address not found.\n");
  1309. + kfree_skb(skb);
  1310. + return -ENODEV;
  1311. + } */
  1312. +
  1313. + for (count = 1; ; count++) {
  1314. + NBT_DBG_FW("Sending firmware command %d\n", count);
  1315. + init_completion(&info->fw_completion);
  1316. + skb_queue_tail(&info->txq, skb);
  1317. + tasklet_schedule(&info->tx_task);
  1318. +
  1319. + skb = skb_dequeue(fw_queue);
  1320. + if (!skb)
  1321. + break;
  1322. +
  1323. + if (!wait_for_completion_timeout(&info->fw_completion,
  1324. + msecs_to_jiffies(1000))) {
  1325. + dev_err(info->dev, "No reply to fw command\n");
  1326. + return -ETIMEDOUT;
  1327. + }
  1328. +
  1329. + if (info->fw_error) {
  1330. + dev_err(info->dev, "FW error\n");
  1331. + return -EPROTO;
  1332. + }
  1333. + };
  1334. +
  1335. + /* Wait for chip warm reset */
  1336. + retries = 100;
  1337. + while ((!skb_queue_empty(&info->txq) ||
  1338. + !(hci_h4p_inb(info, UART_LSR) & UART_LSR_TEMT)) &&
  1339. + retries--) {
  1340. + msleep(10);
  1341. + }
  1342. + if (!retries) {
  1343. + dev_err(info->dev, "Transmitter not empty\n");
  1344. + return -ETIMEDOUT;
  1345. + }
  1346. +
  1347. + hci_h4p_change_speed(info, BC4_MAX_BAUD_RATE);
  1348. +
  1349. + if (hci_h4p_wait_for_cts(info, 1, 100)) {
  1350. + dev_err(info->dev, "cts didn't go down after final speed change\n");
  1351. + return -ETIMEDOUT;
  1352. + }
  1353. +
  1354. + retries = 100;
  1355. + do {
  1356. + init_completion(&info->init_completion);
  1357. + hci_h4p_send_alive_packet(info);
  1358. + retries--;
  1359. + } while (!wait_for_completion_timeout(&info->init_completion, 100) &&
  1360. + retries > 0);
  1361. +
  1362. + if (!retries) {
  1363. + dev_err(info->dev, "No alive reply after speed change\n");
  1364. + return -ETIMEDOUT;
  1365. + }
  1366. +
  1367. + return 0;
  1368. +}
  1369. --- /dev/null
  1370. +++ b/drivers/bluetooth/hci_h4p/fw-ti.c
  1371. @@ -0,0 +1,90 @@
  1372. +/*
  1373. + * This file is part of hci_h4p bluetooth driver
  1374. + *
  1375. + * Copyright (C) 2005, 2006 Nokia Corporation.
  1376. + *
  1377. + * Contact: Ville Tervo <[email protected]>
  1378. + *
  1379. + * This program is free software; you can redistribute it and/or
  1380. + * modify it under the terms of the GNU General Public License
  1381. + * version 2 as published by the Free Software Foundation.
  1382. + *
  1383. + * This program is distributed in the hope that it will be useful, but
  1384. + * WITHOUT ANY WARRANTY; without even the implied warranty of
  1385. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  1386. + * General Public License for more details.
  1387. + *
  1388. + * You should have received a copy of the GNU General Public License
  1389. + * along with this program; if not, write to the Free Software
  1390. + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  1391. + * 02110-1301 USA
  1392. + *
  1393. + */
  1394. +
  1395. +#include <linux/skbuff.h>
  1396. +
  1397. +#include "hci_h4p.h"
  1398. +
  1399. +void hci_h4p_brf6150_parse_fw_event(struct hci_h4p_info *info,
  1400. + struct sk_buff *skb)
  1401. +{
  1402. + struct hci_fw_event *ev;
  1403. + int err = 0;
  1404. +
  1405. + if (bt_cb(skb)->pkt_type != H4_EVT_PKT) {
  1406. + dev_err(info->dev, "Got non event fw packet.\n");
  1407. + err = -EPROTO;
  1408. + goto ret;
  1409. + }
  1410. +
  1411. + ev = (struct hci_fw_event *)skb->data;
  1412. + if (ev->hev.evt != HCI_EV_CMD_COMPLETE) {
  1413. + dev_err(info->dev, "Got non cmd complete fw event\n");
  1414. + err = -EPROTO;
  1415. + goto ret;
  1416. + }
  1417. +
  1418. + if (ev->status != 0) {
  1419. + dev_err(info->dev, "Got error status from fw command\n");
  1420. + err = -EPROTO;
  1421. + goto ret;
  1422. + }
  1423. +
  1424. +ret:
  1425. + info->fw_error = err;
  1426. + complete(&info->fw_completion);
  1427. +}
  1428. +
  1429. +int hci_h4p_brf6150_send_fw(struct hci_h4p_info *info, struct sk_buff_head *fw_queue)
  1430. +{
  1431. + struct sk_buff *skb;
  1432. + int err = 0;
  1433. +
  1434. + info->fw_error = 0;
  1435. +
  1436. + while ((skb = skb_dequeue(fw_queue)) != NULL) {
  1437. + /* We should allways send word aligned data to h4+ devices */
  1438. + if (skb->len % 2) {
  1439. + err = skb_pad(skb, 1);
  1440. + }
  1441. + if (err)
  1442. + return err;
  1443. +
  1444. + init_completion(&info->fw_completion);
  1445. + skb_queue_tail(&info->txq, skb);
  1446. + tasklet_schedule(&info->tx_task);
  1447. +
  1448. + if (!wait_for_completion_timeout(&info->fw_completion, HZ)) {
  1449. + dev_err(info->dev, "Timeout while sending brf6150 fw\n");
  1450. + return -ETIMEDOUT;
  1451. + }
  1452. +
  1453. + if (info->fw_error) {
  1454. + dev_err(info->dev, "There was fw_error while sending bfr6150 fw\n");
  1455. + return -EPROTO;
  1456. + }
  1457. + }
  1458. + NBT_DBG_FW("Firmware sent\n");
  1459. +
  1460. + return 0;
  1461. +}
  1462. --- /dev/null
  1463. +++ b/drivers/bluetooth/hci_h4p/hci_h4p.h
  1464. @@ -0,0 +1,183 @@
  1465. +/*
  1466. + * This file is part of hci_h4p bluetooth driver
  1467. + *
  1468. + * Copyright (C) 2005, 2006 Nokia Corporation.
  1469. + *
  1470. + * Contact: Ville Tervo <[email protected]>
  1471. + *
  1472. + * This program is free software; you can redistribute it and/or
  1473. + * modify it under the terms of the GNU General Public License
  1474. + * version 2 as published by the Free Software Foundation.
  1475. + *
  1476. + * This program is distributed in the hope that it will be useful, but
  1477. + * WITHOUT ANY WARRANTY; without even the implied warranty of
  1478. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  1479. + * General Public License for more details.
  1480. + *
  1481. + * You should have received a copy of the GNU General Public License
  1482. + * along with this program; if not, write to the Free Software
  1483. + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  1484. + * 02110-1301 USA
  1485. + *
  1486. + */
  1487. +
  1488. +#include <mach/board.h>
  1489. +
  1490. +#include <net/bluetooth/bluetooth.h>
  1491. +#include <net/bluetooth/hci_core.h>
  1492. +#include <net/bluetooth/hci.h>
  1493. +
  1494. +#ifndef __DRIVERS_BLUETOOTH_HCI_H4P_H
  1495. +#define __DRIVERS_BLUETOOTH_HCI_H4P_H
  1496. +
  1497. +#define UART_SYSC_OMAP_RESET 0x03
  1498. +#define UART_SYSS_RESETDONE 0x01
  1499. +#define UART_OMAP_SCR_EMPTY_THR 0x08
  1500. +#define UART_OMAP_SCR_WAKEUP 0x10
  1501. +#define UART_OMAP_SSR_WAKEUP 0x02
  1502. +#define UART_OMAP_SSR_TXFULL 0x01
  1503. +
  1504. +#if 0
  1505. +#define NBT_DBG(fmt, arg...) printk("%s: " fmt "" , __FUNCTION__ , ## arg)
  1506. +#else
  1507. +#define NBT_DBG(...)
  1508. +#endif
  1509. +
  1510. +#if 0
  1511. +#define NBT_DBG_FW(fmt, arg...) printk("%s: " fmt "" , __FUNCTION__ , ## arg)
  1512. +#else
  1513. +#define NBT_DBG_FW(...)
  1514. +#endif
  1515. +
  1516. +#if 0
  1517. +#define NBT_DBG_POWER(fmt, arg...) printk("%s: " fmt "" , __FUNCTION__ , ## arg)
  1518. +#else
  1519. +#define NBT_DBG_POWER(...)
  1520. +#endif
  1521. +
  1522. +#if 0
  1523. +#define NBT_DBG_TRANSFER(fmt, arg...) printk("%s: " fmt "" , __FUNCTION__ , ## arg)
  1524. +#else
  1525. +#define NBT_DBG_TRANSFER(...)
  1526. +#endif
  1527. +
  1528. +#if 0
  1529. +#define NBT_DBG_TRANSFER_NF(fmt, arg...) printk(fmt "" , ## arg)
  1530. +#else
  1531. +#define NBT_DBG_TRANSFER_NF(...)
  1532. +#endif
  1533. +
  1534. +#if 0
  1535. +#define NBT_DBG_DMA(fmt, arg...) printk("%s: " fmt "" , __FUNCTION__ , ## arg)
  1536. +#else
  1537. +#define NBT_DBG_DMA(...)
  1538. +#endif
  1539. +
  1540. +struct hci_h4p_info {
  1541. + struct hci_dev *hdev;
  1542. + spinlock_t lock;
  1543. +
  1544. + void __iomem *uart_base;
  1545. + unsigned long uart_phys_base;
  1546. + int irq;
  1547. + struct device *dev;
  1548. + u8 bdaddr[6];
  1549. + u8 chip_type;
  1550. + u8 bt_wakeup_gpio;
  1551. + u8 host_wakeup_gpio;
  1552. + u8 reset_gpio;
  1553. + u8 bt_sysclk;
  1554. +
  1555. +
  1556. + struct sk_buff_head fw_queue;
  1557. + struct sk_buff *alive_cmd_skb;
  1558. + struct completion init_completion;
  1559. + struct completion fw_completion;
  1560. + int fw_error;
  1561. + int init_error;
  1562. +
  1563. + struct sk_buff_head txq;
  1564. + struct tasklet_struct tx_task;
  1565. +
  1566. + struct sk_buff *rx_skb;
  1567. + long rx_count;
  1568. + unsigned long rx_state;
  1569. + unsigned long garbage_bytes;
  1570. + struct tasklet_struct rx_task;
  1571. +
  1572. + int pm_enabled;
  1573. + int tx_pm_enabled;
  1574. + int rx_pm_enabled;
  1575. + struct timer_list tx_pm_timer;
  1576. + struct timer_list rx_pm_timer;
  1577. +
  1578. + int tx_clocks_en;
  1579. + int rx_clocks_en;
  1580. + spinlock_t clocks_lock;
  1581. + struct clk *uart_iclk;
  1582. + struct clk *uart_fclk;
  1583. +};
  1584. +
  1585. +#define MAX_BAUD_RATE 921600
  1586. +#define BC4_MAX_BAUD_RATE 3692300
  1587. +#define UART_CLOCK 48000000
  1588. +#define BT_INIT_DIVIDER 320
  1589. +#define BT_BAUDRATE_DIVIDER 384000000
  1590. +#define BT_SYSCLK_DIV 1000
  1591. +#define INIT_SPEED 120000
  1592. +
  1593. +#define H4_TYPE_SIZE 1
  1594. +
  1595. +/* H4+ packet types */
  1596. +#define H4_CMD_PKT 0x01
  1597. +#define H4_ACL_PKT 0x02
  1598. +#define H4_SCO_PKT 0x03
  1599. +#define H4_EVT_PKT 0x04
  1600. +#define H4_NEG_PKT 0x06
  1601. +#define H4_ALIVE_PKT 0x07
  1602. +
  1603. +/* TX states */
  1604. +#define WAIT_FOR_PKT_TYPE 1
  1605. +#define WAIT_FOR_HEADER 2
  1606. +#define WAIT_FOR_DATA 3
  1607. +
  1608. +struct hci_fw_event {
  1609. + struct hci_event_hdr hev;
  1610. + struct hci_ev_cmd_complete cmd;
  1611. + u8 status;
  1612. +} __attribute__ ((packed));
  1613. +
  1614. +struct hci_bc4_set_bdaddr {
  1615. + u8 type;
  1616. + struct hci_command_hdr cmd_hdr;
  1617. +} __attribute__ ((packed));
  1618. +
  1619. +int hci_h4p_send_alive_packet(struct hci_h4p_info *info);
  1620. +
  1621. +void hci_h4p_bc4_parse_fw_event(struct hci_h4p_info *info,
  1622. + struct sk_buff *skb);
  1623. +int hci_h4p_bc4_send_fw(struct hci_h4p_info *info,
  1624. + struct sk_buff_head *fw_queue);
  1625. +
  1626. +void hci_h4p_brf6150_parse_fw_event(struct hci_h4p_info *info,
  1627. + struct sk_buff *skb);
  1628. +int hci_h4p_brf6150_send_fw(struct hci_h4p_info *info,
  1629. + struct sk_buff_head *fw_queue);
  1630. +
  1631. +int hci_h4p_read_fw(struct hci_h4p_info *info, struct sk_buff_head *fw_queue);
  1632. +int hci_h4p_send_fw(struct hci_h4p_info *info, struct sk_buff_head *fw_queue);
  1633. +void hci_h4p_parse_fw_event(struct hci_h4p_info *info, struct sk_buff *skb);
  1634. +
  1635. +int hci_h4p_sysfs_create_files(struct device *dev);
  1636. +
  1637. +void hci_h4p_outb(struct hci_h4p_info *info, unsigned int offset, u8 val);
  1638. +u8 hci_h4p_inb(struct hci_h4p_info *info, unsigned int offset);
  1639. +void hci_h4p_set_rts(struct hci_h4p_info *info, int active);
  1640. +int hci_h4p_wait_for_cts(struct hci_h4p_info *info, int active, int timeout_ms);
  1641. +void __hci_h4p_set_auto_ctsrts(struct hci_h4p_info *info, int on, u8 which);
  1642. +void hci_h4p_set_auto_ctsrts(struct hci_h4p_info *info, int on, u8 which);
  1643. +void hci_h4p_change_speed(struct hci_h4p_info *info, unsigned long speed);
  1644. +int hci_h4p_reset_uart(struct hci_h4p_info *info);
  1645. +int hci_h4p_init_uart(struct hci_h4p_info *info);
  1646. +
  1647. +#endif /* __DRIVERS_BLUETOOTH_HCI_H4P_H */
  1648. --- /dev/null
  1649. +++ b/drivers/bluetooth/hci_h4p/Makefile
  1650. @@ -0,0 +1,7 @@
  1651. +#
  1652. +# Makefile for the Linux Bluetooth HCI device drivers.
  1653. +#
  1654. +
  1655. +obj-$(CONFIG_BT_HCIH4P) += hci_h4p.o
  1656. +
  1657. +hci_h4p-objs := core.o fw.o uart.o sysfs.o fw-ti.o fw-csr.o
  1658. --- /dev/null
  1659. +++ b/drivers/bluetooth/hci_h4p/sysfs.c
  1660. @@ -0,0 +1,84 @@
  1661. +/*
  1662. + * This file is part of hci_h4p bluetooth driver
  1663. + *
  1664. + * Copyright (C) 2005, 2006 Nokia Corporation.
  1665. + *
  1666. + * Contact: Ville Tervo <[email protected]>
  1667. + *
  1668. + * This program is free software; you can redistribute it and/or
  1669. + * modify it under the terms of the GNU General Public License
  1670. + * version 2 as published by the Free Software Foundation.
  1671. + *
  1672. + * This program is distributed in the hope that it will be useful, but
  1673. + * WITHOUT ANY WARRANTY; without even the implied warranty of
  1674. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  1675. + * General Public License for more details.
  1676. + *
  1677. + * You should have received a copy of the GNU General Public License
  1678. + * along with this program; if not, write to the Free Software
  1679. + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  1680. + * 02110-1301 USA
  1681. + *
  1682. + */
  1683. +
  1684. +#include <linux/kernel.h>
  1685. +#include <linux/init.h>
  1686. +#include <linux/device.h>
  1687. +#include <linux/platform_device.h>
  1688. +
  1689. +#include "hci_h4p.h"
  1690. +
  1691. +#ifdef CONFIG_SYSFS
  1692. +
  1693. +static ssize_t hci_h4p_store_bdaddr(struct device *dev, struct device_attribute *attr,
  1694. + const char *buf, size_t count)
  1695. +{
  1696. + struct hci_h4p_info *info = (struct hci_h4p_info*)dev_get_drvdata(dev);
  1697. + unsigned int bdaddr[6];
  1698. + int ret, i;
  1699. +
  1700. + dev_info(info->dev, "HCI_H4P_STORE_BDADDR called\n");
  1701. +
  1702. + ret = sscanf(buf, "%2x:%2x:%2x:%2x:%2x:%2x\n",
  1703. + &bdaddr[0], &bdaddr[1], &bdaddr[2],
  1704. + &bdaddr[3], &bdaddr[4], &bdaddr[5]);
  1705. +
  1706. + if (ret != 6) {
  1707. + dev_info(info->dev, "bdaddr isn't found\n");
  1708. + return -EINVAL;
  1709. + }
  1710. +
  1711. + //for (i = 0; i < 6; i++)
  1712. + //info->bdaddr[i] = bdaddr[i] & 0xff;
  1713. +
  1714. + info->bdaddr[0] = 0x00;
  1715. + info->bdaddr[1] = 0x1D;
  1716. + info->bdaddr[2] = 0x6E;
  1717. + info->bdaddr[3] = 0xD4;
  1718. + info->bdaddr[4] = 0xF0;
  1719. + info->bdaddr[5] = 0x37;
  1720. +
  1721. + return count;
  1722. +}
  1723. +
  1724. +static ssize_t hci_h4p_show_bdaddr(struct device *dev, struct device_attribute *attr,
  1725. + char *buf)
  1726. +{
  1727. + struct hci_h4p_info *info = (struct hci_h4p_info*)dev_get_drvdata(dev);
  1728. +
  1729. + return sprintf(buf, "%.2x:%.2x:%.2x:%.2x:%.2x:%.2x\n",
  1730. + info->bdaddr[0],
  1731. + info->bdaddr[1],
  1732. + info->bdaddr[2],
  1733. + info->bdaddr[3],
  1734. + info->bdaddr[4],
  1735. + info->bdaddr[5]);
  1736. +}
  1737. +
  1738. +static DEVICE_ATTR(bdaddr, S_IRUGO | S_IWUSR, hci_h4p_show_bdaddr, hci_h4p_store_bdaddr);
  1739. +int hci_h4p_sysfs_create_files(struct device *dev)
  1740. +{
  1741. + return device_create_file(dev, &dev_attr_bdaddr);
  1742. +}
  1743. +
  1744. +#endif
  1745. --- /dev/null
  1746. +++ b/drivers/bluetooth/hci_h4p/uart.c
  1747. @@ -0,0 +1,169 @@
  1748. +/*
  1749. + * This file is part of hci_h4p bluetooth driver
  1750. + *
  1751. + * Copyright (C) 2005, 2006 Nokia Corporation.
  1752. + *
  1753. + * Contact: Ville Tervo <[email protected]>
  1754. + *
  1755. + * This program is free software; you can redistribute it and/or
  1756. + * modify it under the terms of the GNU General Public License
  1757. + * version 2 as published by the Free Software Foundation.
  1758. + *
  1759. + * This program is distributed in the hope that it will be useful, but
  1760. + * WITHOUT ANY WARRANTY; without even the implied warranty of
  1761. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  1762. + * General Public License for more details.
  1763. + *
  1764. + * You should have received a copy of the GNU General Public License
  1765. + * along with this program; if not, write to the Free Software
  1766. + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  1767. + * 02110-1301 USA
  1768. + *
  1769. + */
  1770. +
  1771. +#include <linux/serial_reg.h>
  1772. +#include <linux/delay.h>
  1773. +#include <linux/clk.h>
  1774. +
  1775. +#include <asm/io.h>
  1776. +
  1777. +#include "hci_h4p.h"
  1778. +
  1779. +inline void hci_h4p_outb(struct hci_h4p_info *info, unsigned int offset, u8 val)
  1780. +{
  1781. + offset <<= 2;
  1782. + __raw_writeb(val, info->uart_base + offset);
  1783. + //outb(val, info->uart_base + (offset << 2));
  1784. +}
  1785. +
  1786. +inline u8 hci_h4p_inb(struct hci_h4p_info *info, unsigned int offset)
  1787. +{
  1788. + offset <<= 2;
  1789. + return (u8)__raw_readb(info->uart_base + offset);
  1790. + //return (unsigned int)__raw_readb(up->membase + offset);
  1791. + //return inb(info->uart_base + (offset << 2));
  1792. +}
  1793. +
  1794. +void hci_h4p_set_rts(struct hci_h4p_info *info, int active)
  1795. +{
  1796. + u8 b;
  1797. +
  1798. + b = hci_h4p_inb(info, UART_MCR);
  1799. + if (active)
  1800. + b |= UART_MCR_RTS;
  1801. + else
  1802. + b &= ~UART_MCR_RTS;
  1803. + hci_h4p_outb(info, UART_MCR, b);
  1804. +}
  1805. +
  1806. +int hci_h4p_wait_for_cts(struct hci_h4p_info *info, int active,
  1807. + int timeout_ms)
  1808. +{
  1809. + int okay;
  1810. + unsigned long timeout;
  1811. +
  1812. + okay = 0;
  1813. + timeout = jiffies + msecs_to_jiffies(timeout_ms);
  1814. + for (;;) {
  1815. + int state;
  1816. +
  1817. + state = hci_h4p_inb(info, UART_MSR) & UART_MSR_CTS;
  1818. + if (active) {
  1819. + if (state)
  1820. + return 0;
  1821. + } else {
  1822. + if (!state)
  1823. + return 0;
  1824. + }
  1825. + if (time_after(jiffies, timeout))
  1826. + return -ETIMEDOUT;
  1827. + }
  1828. +}
  1829. +
  1830. +void __hci_h4p_set_auto_ctsrts(struct hci_h4p_info *info, int on, u8 which)
  1831. +{
  1832. + u8 lcr, b;
  1833. +
  1834. + lcr = hci_h4p_inb(info, UART_LCR);
  1835. + hci_h4p_outb(info, UART_LCR, 0xbf);
  1836. + b = hci_h4p_inb(info, UART_EFR);
  1837. + if (on)
  1838. + b |= which;
  1839. + else
  1840. + b &= ~which;
  1841. + hci_h4p_outb(info, UART_EFR, b);
  1842. + hci_h4p_outb(info, UART_LCR, lcr);
  1843. +}
  1844. +
  1845. +void hci_h4p_set_auto_ctsrts(struct hci_h4p_info *info, int on, u8 which)
  1846. +{
  1847. + unsigned long flags;
  1848. +
  1849. + spin_lock_irqsave(&info->lock, flags);
  1850. + __hci_h4p_set_auto_ctsrts(info, on, which);
  1851. + spin_unlock_irqrestore(&info->lock, flags);
  1852. +}
  1853. +
  1854. +void hci_h4p_change_speed(struct hci_h4p_info *info, unsigned long speed)
  1855. +{
  1856. + unsigned int divisor;
  1857. + u8 lcr, mdr1;
  1858. +
  1859. + NBT_DBG("Setting speed %lu\n", speed);
  1860. +
  1861. + if (speed >= 460800) {
  1862. + divisor = UART_CLOCK / 13 / speed;
  1863. + mdr1 = 3;
  1864. + } else {
  1865. + divisor = UART_CLOCK / 16 / speed;
  1866. + mdr1 = 0;
  1867. + }
  1868. +
  1869. + hci_h4p_outb(info, UART_OMAP_MDR1, 7); /* Make sure UART mode is disabled */
  1870. + lcr = hci_h4p_inb(info, UART_LCR);
  1871. + hci_h4p_outb(info, UART_LCR, UART_LCR_DLAB); /* Set DLAB */
  1872. + hci_h4p_outb(info, UART_DLL, divisor & 0xff); /* Set speed */
  1873. + hci_h4p_outb(info, UART_DLM, divisor >> 8);
  1874. + hci_h4p_outb(info, UART_LCR, lcr);
  1875. + hci_h4p_outb(info, UART_OMAP_MDR1, mdr1); /* Make sure UART mode is enabled */
  1876. +}
  1877. +
  1878. +int hci_h4p_reset_uart(struct hci_h4p_info *info)
  1879. +{
  1880. + int count = 0;
  1881. +
  1882. + /* Reset the UART */
  1883. + hci_h4p_outb(info, UART_OMAP_SYSC, UART_SYSC_OMAP_RESET);
  1884. + while (!(hci_h4p_inb(info, UART_OMAP_SYSS) & UART_SYSS_RESETDONE)) {
  1885. + if (count++ > 20000) {
  1886. + dev_err(info->dev, "hci_h4p: UART reset timeout\n");
  1887. + return -ENODEV;
  1888. + }
  1889. + udelay(1);
  1890. + }
  1891. +
  1892. + return 0;
  1893. +}
  1894. +
  1895. +int hci_h4p_init_uart(struct hci_h4p_info *info)
  1896. +{
  1897. + int err;
  1898. +
  1899. + err = hci_h4p_reset_uart(info);
  1900. + if (err < 0)
  1901. + return err;
  1902. +
  1903. + /* Enable and setup FIFO */
  1904. + hci_h4p_outb(info, UART_LCR, UART_LCR_WLEN8);
  1905. + hci_h4p_outb(info, UART_OMAP_MDR1, 0x00); /* Make sure UART mode is enabled */
  1906. + hci_h4p_outb(info, UART_OMAP_SCR, 0x80);
  1907. + hci_h4p_outb(info, UART_EFR, UART_EFR_ECB);
  1908. + hci_h4p_outb(info, UART_MCR, UART_MCR_TCRTLR);
  1909. + hci_h4p_outb(info, UART_TI752_TLR, 0x1f);
  1910. + hci_h4p_outb(info, UART_TI752_TCR, 0xef);
  1911. + hci_h4p_outb(info, UART_FCR, UART_FCR_ENABLE_FIFO | UART_FCR_CLEAR_RCVR |
  1912. + UART_FCR_CLEAR_XMIT | UART_FCR_R_TRIG_00);
  1913. + hci_h4p_outb(info, UART_IER, UART_IER_RDI);
  1914. +
  1915. + return 0;
  1916. +}
  1917. --- a/drivers/bluetooth/Kconfig
  1918. +++ b/drivers/bluetooth/Kconfig
  1919. @@ -173,6 +173,16 @@ config BT_HCIBTUART
  1920. Say Y here to compile support for HCI UART devices into the
  1921. kernel or say M to compile it as module (btuart_cs).
  1922. +config BT_HCIH4P
  1923. + tristate "HCI driver with H4 Nokia extensions"
  1924. + depends on BT && ARCH_OMAP
  1925. + help
  1926. + Bluetooth HCI driver with H4 extensions. This driver provides
  1927. + support for H4+ Bluetooth chip with vendor-specific H4 extensions.
  1928. +
  1929. + Say Y here to compile support for h4 extended devices into the kernel
  1930. + or say M to compile it as module (hci_h4p).
  1931. +
  1932. config BT_HCIVHCI
  1933. tristate "HCI VHCI (Virtual HCI device) driver"
  1934. help
  1935. --- a/drivers/bluetooth/Makefile
  1936. +++ b/drivers/bluetooth/Makefile
  1937. @@ -11,6 +11,7 @@ obj-$(CONFIG_BT_HCIDTL1) += dtl1_cs.o
  1938. obj-$(CONFIG_BT_HCIBT3C) += bt3c_cs.o
  1939. obj-$(CONFIG_BT_HCIBLUECARD) += bluecard_cs.o
  1940. obj-$(CONFIG_BT_HCIBTUART) += btuart_cs.o
  1941. +obj-$(CONFIG_BT_HCIH4P) += hci_h4p/
  1942. obj-$(CONFIG_BT_HCIBTUSB) += btusb.o
  1943. obj-$(CONFIG_BT_HCIBTSDIO) += btsdio.o