ozpd.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883
  1. /* -----------------------------------------------------------------------------
  2. * Copyright (c) 2011 Ozmo Inc
  3. * Released under the GNU General Public License Version 2 (GPLv2).
  4. * -----------------------------------------------------------------------------
  5. */
  6. #include <linux/module.h>
  7. #include <linux/timer.h>
  8. #include <linux/sched.h>
  9. #include <linux/netdevice.h>
  10. #include <linux/etherdevice.h>
  11. #include <linux/errno.h>
  12. #include "ozdbg.h"
  13. #include "ozprotocol.h"
  14. #include "ozeltbuf.h"
  15. #include "ozpd.h"
  16. #include "ozproto.h"
  17. #include "ozcdev.h"
  18. #include "ozusbsvc.h"
  19. #include <asm/unaligned.h>
  20. #include <linux/uaccess.h>
  21. #include <net/psnap.h>
  22. static struct oz_tx_frame *oz_tx_frame_alloc(struct oz_pd *pd);
  23. static void oz_tx_frame_free(struct oz_pd *pd, struct oz_tx_frame *f);
  24. static void oz_tx_isoc_free(struct oz_pd *pd, struct oz_tx_frame *f);
  25. static struct sk_buff *oz_build_frame(struct oz_pd *pd, struct oz_tx_frame *f);
  26. static int oz_send_isoc_frame(struct oz_pd *pd);
  27. static void oz_retire_frame(struct oz_pd *pd, struct oz_tx_frame *f);
  28. static void oz_isoc_stream_free(struct oz_isoc_stream *st);
  29. static int oz_send_next_queued_frame(struct oz_pd *pd, int more_data);
  30. static void oz_isoc_destructor(struct sk_buff *skb);
  31. /*
  32. * Counts the uncompleted isoc frames submitted to netcard.
  33. */
  34. static atomic_t g_submitted_isoc = ATOMIC_INIT(0);
  35. /* Application handler functions.
  36. */
  37. static const struct oz_app_if g_app_if[OZ_NB_APPS] = {
  38. [OZ_APPID_USB] = {
  39. .init = oz_usb_init,
  40. .term = oz_usb_term,
  41. .start = oz_usb_start,
  42. .stop = oz_usb_stop,
  43. .rx = oz_usb_rx,
  44. .heartbeat = oz_usb_heartbeat,
  45. .farewell = oz_usb_farewell,
  46. },
  47. [OZ_APPID_SERIAL] = {
  48. .init = oz_cdev_init,
  49. .term = oz_cdev_term,
  50. .start = oz_cdev_start,
  51. .stop = oz_cdev_stop,
  52. .rx = oz_cdev_rx,
  53. },
  54. };
  55. /*
  56. * Context: softirq or process
  57. */
  58. void oz_pd_set_state(struct oz_pd *pd, unsigned state)
  59. {
  60. pd->state = state;
  61. switch (state) {
  62. case OZ_PD_S_IDLE:
  63. oz_pd_dbg(pd, ON, "PD State: OZ_PD_S_IDLE\n");
  64. break;
  65. case OZ_PD_S_CONNECTED:
  66. oz_pd_dbg(pd, ON, "PD State: OZ_PD_S_CONNECTED\n");
  67. break;
  68. case OZ_PD_S_STOPPED:
  69. oz_pd_dbg(pd, ON, "PD State: OZ_PD_S_STOPPED\n");
  70. break;
  71. case OZ_PD_S_SLEEP:
  72. oz_pd_dbg(pd, ON, "PD State: OZ_PD_S_SLEEP\n");
  73. break;
  74. }
  75. }
  76. /*
  77. * Context: softirq or process
  78. */
  79. void oz_pd_get(struct oz_pd *pd)
  80. {
  81. atomic_inc(&pd->ref_count);
  82. }
  83. /*
  84. * Context: softirq or process
  85. */
  86. void oz_pd_put(struct oz_pd *pd)
  87. {
  88. if (atomic_dec_and_test(&pd->ref_count))
  89. oz_pd_destroy(pd);
  90. }
  91. /*
  92. * Context: softirq-serialized
  93. */
  94. struct oz_pd *oz_pd_alloc(const u8 *mac_addr)
  95. {
  96. struct oz_pd *pd = kzalloc(sizeof(struct oz_pd), GFP_ATOMIC);
  97. if (pd) {
  98. int i;
  99. atomic_set(&pd->ref_count, 2);
  100. for (i = 0; i < OZ_NB_APPS; i++)
  101. spin_lock_init(&pd->app_lock[i]);
  102. pd->last_rx_pkt_num = 0xffffffff;
  103. oz_pd_set_state(pd, OZ_PD_S_IDLE);
  104. pd->max_tx_size = OZ_MAX_TX_SIZE;
  105. ether_addr_copy(pd->mac_addr, mac_addr);
  106. oz_elt_buf_init(&pd->elt_buff);
  107. spin_lock_init(&pd->tx_frame_lock);
  108. INIT_LIST_HEAD(&pd->tx_queue);
  109. INIT_LIST_HEAD(&pd->farewell_list);
  110. pd->last_sent_frame = &pd->tx_queue;
  111. spin_lock_init(&pd->stream_lock);
  112. INIT_LIST_HEAD(&pd->stream_list);
  113. tasklet_init(&pd->heartbeat_tasklet, oz_pd_heartbeat_handler,
  114. (unsigned long)pd);
  115. tasklet_init(&pd->timeout_tasklet, oz_pd_timeout_handler,
  116. (unsigned long)pd);
  117. hrtimer_init(&pd->heartbeat, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
  118. hrtimer_init(&pd->timeout, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
  119. pd->heartbeat.function = oz_pd_heartbeat_event;
  120. pd->timeout.function = oz_pd_timeout_event;
  121. }
  122. return pd;
  123. }
  124. /*
  125. * Context: softirq or process
  126. */
  127. static void oz_pd_free(struct work_struct *work)
  128. {
  129. struct list_head *e, *n;
  130. struct oz_pd *pd;
  131. oz_pd_dbg(pd, ON, "Destroying PD\n");
  132. pd = container_of(work, struct oz_pd, workitem);
  133. /*Disable timer tasklets*/
  134. tasklet_kill(&pd->heartbeat_tasklet);
  135. tasklet_kill(&pd->timeout_tasklet);
  136. /* Free streams, queued tx frames and farewells. */
  137. list_for_each_safe(e, n, &pd->stream_list)
  138. oz_isoc_stream_free(list_entry(e, struct oz_isoc_stream, link));
  139. list_for_each_safe(e, n, &pd->tx_queue) {
  140. struct oz_tx_frame *f = list_entry(e, struct oz_tx_frame, link);
  141. if (f->skb != NULL)
  142. kfree_skb(f->skb);
  143. oz_retire_frame(pd, f);
  144. }
  145. oz_elt_buf_term(&pd->elt_buff);
  146. list_for_each_safe(e, n, &pd->farewell_list)
  147. kfree(list_entry(e, struct oz_farewell, link));
  148. if (pd->net_dev)
  149. dev_put(pd->net_dev);
  150. kfree(pd);
  151. }
  152. /*
  153. * Context: softirq or Process
  154. */
  155. void oz_pd_destroy(struct oz_pd *pd)
  156. {
  157. if (hrtimer_active(&pd->timeout))
  158. hrtimer_cancel(&pd->timeout);
  159. if (hrtimer_active(&pd->heartbeat))
  160. hrtimer_cancel(&pd->heartbeat);
  161. INIT_WORK(&pd->workitem, oz_pd_free);
  162. if (!schedule_work(&pd->workitem))
  163. oz_pd_dbg(pd, ON, "failed to schedule workitem\n");
  164. }
  165. /*
  166. * Context: softirq-serialized
  167. */
  168. int oz_services_start(struct oz_pd *pd, u16 apps, int resume)
  169. {
  170. int i, rc = 0;
  171. oz_pd_dbg(pd, ON, "%s: (0x%x) resume(%d)\n", __func__, apps, resume);
  172. for (i = 0; i < OZ_NB_APPS; i++) {
  173. if (g_app_if[i].start && (apps & (1 << i))) {
  174. if (g_app_if[i].start(pd, resume)) {
  175. rc = -1;
  176. oz_pd_dbg(pd, ON,
  177. "Unable to start service %d\n", i);
  178. break;
  179. }
  180. spin_lock_bh(&g_polling_lock);
  181. pd->total_apps |= (1 << i);
  182. if (resume)
  183. pd->paused_apps &= ~(1 << i);
  184. spin_unlock_bh(&g_polling_lock);
  185. }
  186. }
  187. return rc;
  188. }
  189. /*
  190. * Context: softirq or process
  191. */
  192. void oz_services_stop(struct oz_pd *pd, u16 apps, int pause)
  193. {
  194. int i;
  195. oz_pd_dbg(pd, ON, "%s: (0x%x) pause(%d)\n", __func__, apps, pause);
  196. for (i = 0; i < OZ_NB_APPS; i++) {
  197. if (g_app_if[i].stop && (apps & (1 << i))) {
  198. spin_lock_bh(&g_polling_lock);
  199. if (pause) {
  200. pd->paused_apps |= (1 << i);
  201. } else {
  202. pd->total_apps &= ~(1 << i);
  203. pd->paused_apps &= ~(1 << i);
  204. }
  205. spin_unlock_bh(&g_polling_lock);
  206. g_app_if[i].stop(pd, pause);
  207. }
  208. }
  209. }
  210. /*
  211. * Context: softirq
  212. */
  213. void oz_pd_heartbeat(struct oz_pd *pd, u16 apps)
  214. {
  215. int i, more = 0;
  216. for (i = 0; i < OZ_NB_APPS; i++) {
  217. if (g_app_if[i].heartbeat && (apps & (1 << i))) {
  218. if (g_app_if[i].heartbeat(pd))
  219. more = 1;
  220. }
  221. }
  222. if ((!more) && (hrtimer_active(&pd->heartbeat)))
  223. hrtimer_cancel(&pd->heartbeat);
  224. if (pd->mode & OZ_F_ISOC_ANYTIME) {
  225. int count = 8;
  226. while (count-- && (oz_send_isoc_frame(pd) >= 0))
  227. ;
  228. }
  229. }
  230. /*
  231. * Context: softirq or process
  232. */
  233. void oz_pd_stop(struct oz_pd *pd)
  234. {
  235. u16 stop_apps;
  236. oz_dbg(ON, "oz_pd_stop() State = 0x%x\n", pd->state);
  237. oz_pd_indicate_farewells(pd);
  238. spin_lock_bh(&g_polling_lock);
  239. stop_apps = pd->total_apps;
  240. pd->total_apps = 0;
  241. pd->paused_apps = 0;
  242. spin_unlock_bh(&g_polling_lock);
  243. oz_services_stop(pd, stop_apps, 0);
  244. spin_lock_bh(&g_polling_lock);
  245. oz_pd_set_state(pd, OZ_PD_S_STOPPED);
  246. /* Remove from PD list.*/
  247. list_del(&pd->link);
  248. spin_unlock_bh(&g_polling_lock);
  249. oz_dbg(ON, "pd ref count = %d\n", atomic_read(&pd->ref_count));
  250. oz_pd_put(pd);
  251. }
  252. /*
  253. * Context: softirq
  254. */
  255. int oz_pd_sleep(struct oz_pd *pd)
  256. {
  257. int do_stop = 0;
  258. u16 stop_apps;
  259. spin_lock_bh(&g_polling_lock);
  260. if (pd->state & (OZ_PD_S_SLEEP | OZ_PD_S_STOPPED)) {
  261. spin_unlock_bh(&g_polling_lock);
  262. return 0;
  263. }
  264. if (pd->keep_alive && pd->session_id)
  265. oz_pd_set_state(pd, OZ_PD_S_SLEEP);
  266. else
  267. do_stop = 1;
  268. stop_apps = pd->total_apps;
  269. spin_unlock_bh(&g_polling_lock);
  270. if (do_stop) {
  271. oz_pd_stop(pd);
  272. } else {
  273. oz_services_stop(pd, stop_apps, 1);
  274. oz_timer_add(pd, OZ_TIMER_STOP, pd->keep_alive);
  275. }
  276. return do_stop;
  277. }
  278. /*
  279. * Context: softirq
  280. */
  281. static struct oz_tx_frame *oz_tx_frame_alloc(struct oz_pd *pd)
  282. {
  283. struct oz_tx_frame *f;
  284. f = kmem_cache_alloc(oz_tx_frame_cache, GFP_ATOMIC);
  285. if (f) {
  286. f->total_size = sizeof(struct oz_hdr);
  287. INIT_LIST_HEAD(&f->link);
  288. INIT_LIST_HEAD(&f->elt_list);
  289. }
  290. return f;
  291. }
  292. /*
  293. * Context: softirq or process
  294. */
  295. static void oz_tx_isoc_free(struct oz_pd *pd, struct oz_tx_frame *f)
  296. {
  297. pd->nb_queued_isoc_frames--;
  298. list_del_init(&f->link);
  299. kmem_cache_free(oz_tx_frame_cache, f);
  300. oz_dbg(TX_FRAMES, "Releasing ISOC Frame isoc_nb= %d\n",
  301. pd->nb_queued_isoc_frames);
  302. }
  303. /*
  304. * Context: softirq or process
  305. */
  306. static void oz_tx_frame_free(struct oz_pd *pd, struct oz_tx_frame *f)
  307. {
  308. kmem_cache_free(oz_tx_frame_cache, f);
  309. }
  310. /*
  311. * Context: softirq-serialized
  312. */
  313. static void oz_set_more_bit(struct sk_buff *skb)
  314. {
  315. struct oz_hdr *oz_hdr = (struct oz_hdr *)skb_network_header(skb);
  316. oz_hdr->control |= OZ_F_MORE_DATA;
  317. }
  318. /*
  319. * Context: softirq-serialized
  320. */
  321. static void oz_set_last_pkt_nb(struct oz_pd *pd, struct sk_buff *skb)
  322. {
  323. struct oz_hdr *oz_hdr = (struct oz_hdr *)skb_network_header(skb);
  324. oz_hdr->last_pkt_num = pd->trigger_pkt_num & OZ_LAST_PN_MASK;
  325. }
  326. /*
  327. * Context: softirq
  328. */
  329. int oz_prepare_frame(struct oz_pd *pd, int empty)
  330. {
  331. struct oz_tx_frame *f;
  332. if ((pd->mode & OZ_MODE_MASK) != OZ_MODE_TRIGGERED)
  333. return -1;
  334. if (pd->nb_queued_frames >= OZ_MAX_QUEUED_FRAMES)
  335. return -1;
  336. if (!empty && !oz_are_elts_available(&pd->elt_buff))
  337. return -1;
  338. f = oz_tx_frame_alloc(pd);
  339. if (f == NULL)
  340. return -1;
  341. f->skb = NULL;
  342. f->hdr.control =
  343. (OZ_PROTOCOL_VERSION<<OZ_VERSION_SHIFT) | OZ_F_ACK_REQUESTED;
  344. ++pd->last_tx_pkt_num;
  345. put_unaligned(cpu_to_le32(pd->last_tx_pkt_num), &f->hdr.pkt_num);
  346. if (empty == 0) {
  347. oz_select_elts_for_tx(&pd->elt_buff, 0, &f->total_size,
  348. pd->max_tx_size, &f->elt_list);
  349. }
  350. spin_lock(&pd->tx_frame_lock);
  351. list_add_tail(&f->link, &pd->tx_queue);
  352. pd->nb_queued_frames++;
  353. spin_unlock(&pd->tx_frame_lock);
  354. return 0;
  355. }
  356. /*
  357. * Context: softirq-serialized
  358. */
  359. static struct sk_buff *oz_build_frame(struct oz_pd *pd, struct oz_tx_frame *f)
  360. {
  361. struct sk_buff *skb;
  362. struct net_device *dev = pd->net_dev;
  363. struct oz_hdr *oz_hdr;
  364. struct oz_elt *elt;
  365. struct oz_elt_info *ei;
  366. /* Allocate skb with enough space for the lower layers as well
  367. * as the space we need.
  368. */
  369. skb = alloc_skb(f->total_size + OZ_ALLOCATED_SPACE(dev), GFP_ATOMIC);
  370. if (skb == NULL)
  371. return NULL;
  372. /* Reserve the head room for lower layers.
  373. */
  374. skb_reserve(skb, LL_RESERVED_SPACE(dev));
  375. skb_reset_network_header(skb);
  376. skb->dev = dev;
  377. skb->protocol = htons(OZ_ETHERTYPE);
  378. if (dev_hard_header(skb, dev, OZ_ETHERTYPE, pd->mac_addr,
  379. dev->dev_addr, skb->len) < 0)
  380. goto fail;
  381. /* Push the tail to the end of the area we are going to copy to.
  382. */
  383. oz_hdr = (struct oz_hdr *)skb_put(skb, f->total_size);
  384. f->hdr.last_pkt_num = pd->trigger_pkt_num & OZ_LAST_PN_MASK;
  385. memcpy(oz_hdr, &f->hdr, sizeof(struct oz_hdr));
  386. /* Copy the elements into the frame body.
  387. */
  388. elt = (struct oz_elt *)(oz_hdr+1);
  389. list_for_each_entry(ei, &f->elt_list, link) {
  390. memcpy(elt, ei->data, ei->length);
  391. elt = oz_next_elt(elt);
  392. }
  393. return skb;
  394. fail:
  395. kfree_skb(skb);
  396. return NULL;
  397. }
  398. /*
  399. * Context: softirq or process
  400. */
  401. static void oz_retire_frame(struct oz_pd *pd, struct oz_tx_frame *f)
  402. {
  403. struct oz_elt_info *ei, *n;
  404. list_for_each_entry_safe(ei, n, &f->elt_list, link) {
  405. list_del_init(&ei->link);
  406. if (ei->callback)
  407. ei->callback(pd, ei->context);
  408. spin_lock_bh(&pd->elt_buff.lock);
  409. oz_elt_info_free(&pd->elt_buff, ei);
  410. spin_unlock_bh(&pd->elt_buff.lock);
  411. }
  412. oz_tx_frame_free(pd, f);
  413. }
  414. /*
  415. * Context: softirq-serialized
  416. */
  417. static int oz_send_next_queued_frame(struct oz_pd *pd, int more_data)
  418. {
  419. struct sk_buff *skb;
  420. struct oz_tx_frame *f;
  421. struct list_head *e;
  422. spin_lock(&pd->tx_frame_lock);
  423. e = pd->last_sent_frame->next;
  424. if (e == &pd->tx_queue) {
  425. spin_unlock(&pd->tx_frame_lock);
  426. return -1;
  427. }
  428. f = list_entry(e, struct oz_tx_frame, link);
  429. if (f->skb != NULL) {
  430. skb = f->skb;
  431. oz_tx_isoc_free(pd, f);
  432. spin_unlock(&pd->tx_frame_lock);
  433. if (more_data)
  434. oz_set_more_bit(skb);
  435. oz_set_last_pkt_nb(pd, skb);
  436. if ((int)atomic_read(&g_submitted_isoc) <
  437. OZ_MAX_SUBMITTED_ISOC) {
  438. if (dev_queue_xmit(skb) < 0) {
  439. oz_dbg(TX_FRAMES, "Dropping ISOC Frame\n");
  440. return -1;
  441. }
  442. atomic_inc(&g_submitted_isoc);
  443. oz_dbg(TX_FRAMES, "Sending ISOC Frame, nb_isoc= %d\n",
  444. pd->nb_queued_isoc_frames);
  445. return 0;
  446. }
  447. kfree_skb(skb);
  448. oz_dbg(TX_FRAMES, "Dropping ISOC Frame>\n");
  449. return -1;
  450. }
  451. pd->last_sent_frame = e;
  452. skb = oz_build_frame(pd, f);
  453. spin_unlock(&pd->tx_frame_lock);
  454. if (!skb)
  455. return -1;
  456. if (more_data)
  457. oz_set_more_bit(skb);
  458. oz_dbg(TX_FRAMES, "TX frame PN=0x%x\n", f->hdr.pkt_num);
  459. if (dev_queue_xmit(skb) < 0)
  460. return -1;
  461. return 0;
  462. }
  463. /*
  464. * Context: softirq-serialized
  465. */
  466. void oz_send_queued_frames(struct oz_pd *pd, int backlog)
  467. {
  468. while (oz_prepare_frame(pd, 0) >= 0)
  469. backlog++;
  470. switch (pd->mode & (OZ_F_ISOC_NO_ELTS | OZ_F_ISOC_ANYTIME)) {
  471. case OZ_F_ISOC_NO_ELTS: {
  472. backlog += pd->nb_queued_isoc_frames;
  473. if (backlog <= 0)
  474. goto out;
  475. if (backlog > OZ_MAX_SUBMITTED_ISOC)
  476. backlog = OZ_MAX_SUBMITTED_ISOC;
  477. break;
  478. }
  479. case OZ_NO_ELTS_ANYTIME: {
  480. if ((backlog <= 0) && (pd->isoc_sent == 0))
  481. goto out;
  482. break;
  483. }
  484. default: {
  485. if (backlog <= 0)
  486. goto out;
  487. break;
  488. }
  489. }
  490. while (backlog--) {
  491. if (oz_send_next_queued_frame(pd, backlog) < 0)
  492. break;
  493. }
  494. return;
  495. out: oz_prepare_frame(pd, 1);
  496. oz_send_next_queued_frame(pd, 0);
  497. }
  498. /*
  499. * Context: softirq
  500. */
  501. static int oz_send_isoc_frame(struct oz_pd *pd)
  502. {
  503. struct sk_buff *skb;
  504. struct net_device *dev = pd->net_dev;
  505. struct oz_hdr *oz_hdr;
  506. struct oz_elt *elt;
  507. struct oz_elt_info *ei;
  508. LIST_HEAD(list);
  509. int total_size = sizeof(struct oz_hdr);
  510. oz_select_elts_for_tx(&pd->elt_buff, 1, &total_size,
  511. pd->max_tx_size, &list);
  512. if (list_empty(&list))
  513. return 0;
  514. skb = alloc_skb(total_size + OZ_ALLOCATED_SPACE(dev), GFP_ATOMIC);
  515. if (skb == NULL) {
  516. oz_dbg(ON, "Cannot alloc skb\n");
  517. oz_elt_info_free_chain(&pd->elt_buff, &list);
  518. return -1;
  519. }
  520. skb_reserve(skb, LL_RESERVED_SPACE(dev));
  521. skb_reset_network_header(skb);
  522. skb->dev = dev;
  523. skb->protocol = htons(OZ_ETHERTYPE);
  524. if (dev_hard_header(skb, dev, OZ_ETHERTYPE, pd->mac_addr,
  525. dev->dev_addr, skb->len) < 0) {
  526. kfree_skb(skb);
  527. return -1;
  528. }
  529. oz_hdr = (struct oz_hdr *)skb_put(skb, total_size);
  530. oz_hdr->control = (OZ_PROTOCOL_VERSION<<OZ_VERSION_SHIFT) | OZ_F_ISOC;
  531. oz_hdr->last_pkt_num = pd->trigger_pkt_num & OZ_LAST_PN_MASK;
  532. elt = (struct oz_elt *)(oz_hdr+1);
  533. list_for_each_entry(ei, &list, link) {
  534. memcpy(elt, ei->data, ei->length);
  535. elt = oz_next_elt(elt);
  536. }
  537. dev_queue_xmit(skb);
  538. oz_elt_info_free_chain(&pd->elt_buff, &list);
  539. return 0;
  540. }
  541. /*
  542. * Context: softirq-serialized
  543. */
  544. void oz_retire_tx_frames(struct oz_pd *pd, u8 lpn)
  545. {
  546. struct oz_tx_frame *f, *tmp = NULL;
  547. u8 diff;
  548. u32 pkt_num;
  549. LIST_HEAD(list);
  550. spin_lock(&pd->tx_frame_lock);
  551. list_for_each_entry(f, &pd->tx_queue, link) {
  552. pkt_num = le32_to_cpu(get_unaligned(&f->hdr.pkt_num));
  553. diff = (lpn - (pkt_num & OZ_LAST_PN_MASK)) & OZ_LAST_PN_MASK;
  554. if ((diff > OZ_LAST_PN_HALF_CYCLE) || (pkt_num == 0))
  555. break;
  556. oz_dbg(TX_FRAMES, "Releasing pkt_num= %u, nb= %d\n",
  557. pkt_num, pd->nb_queued_frames);
  558. tmp = f;
  559. pd->nb_queued_frames--;
  560. }
  561. if (tmp)
  562. list_cut_position(&list, &pd->tx_queue, &tmp->link);
  563. pd->last_sent_frame = &pd->tx_queue;
  564. spin_unlock(&pd->tx_frame_lock);
  565. list_for_each_entry_safe(f, tmp, &list, link)
  566. oz_retire_frame(pd, f);
  567. }
  568. /*
  569. * Precondition: stream_lock must be held.
  570. * Context: softirq
  571. */
  572. static struct oz_isoc_stream *pd_stream_find(struct oz_pd *pd, u8 ep_num)
  573. {
  574. struct oz_isoc_stream *st;
  575. list_for_each_entry(st, &pd->stream_list, link) {
  576. if (st->ep_num == ep_num)
  577. return st;
  578. }
  579. return NULL;
  580. }
  581. /*
  582. * Context: softirq
  583. */
  584. int oz_isoc_stream_create(struct oz_pd *pd, u8 ep_num)
  585. {
  586. struct oz_isoc_stream *st =
  587. kzalloc(sizeof(struct oz_isoc_stream), GFP_ATOMIC);
  588. if (!st)
  589. return -ENOMEM;
  590. st->ep_num = ep_num;
  591. spin_lock_bh(&pd->stream_lock);
  592. if (!pd_stream_find(pd, ep_num)) {
  593. list_add(&st->link, &pd->stream_list);
  594. st = NULL;
  595. }
  596. spin_unlock_bh(&pd->stream_lock);
  597. kfree(st);
  598. return 0;
  599. }
  600. /*
  601. * Context: softirq or process
  602. */
  603. static void oz_isoc_stream_free(struct oz_isoc_stream *st)
  604. {
  605. kfree_skb(st->skb);
  606. kfree(st);
  607. }
  608. /*
  609. * Context: softirq
  610. */
  611. int oz_isoc_stream_delete(struct oz_pd *pd, u8 ep_num)
  612. {
  613. struct oz_isoc_stream *st;
  614. spin_lock_bh(&pd->stream_lock);
  615. st = pd_stream_find(pd, ep_num);
  616. if (st)
  617. list_del(&st->link);
  618. spin_unlock_bh(&pd->stream_lock);
  619. if (st)
  620. oz_isoc_stream_free(st);
  621. return 0;
  622. }
  623. /*
  624. * Context: any
  625. */
  626. static void oz_isoc_destructor(struct sk_buff *skb)
  627. {
  628. atomic_dec(&g_submitted_isoc);
  629. }
  630. /*
  631. * Context: softirq
  632. */
  633. int oz_send_isoc_unit(struct oz_pd *pd, u8 ep_num, const u8 *data, int len)
  634. {
  635. struct net_device *dev = pd->net_dev;
  636. struct oz_isoc_stream *st;
  637. u8 nb_units = 0;
  638. struct sk_buff *skb = NULL;
  639. struct oz_hdr *oz_hdr = NULL;
  640. int size = 0;
  641. spin_lock_bh(&pd->stream_lock);
  642. st = pd_stream_find(pd, ep_num);
  643. if (st) {
  644. skb = st->skb;
  645. st->skb = NULL;
  646. nb_units = st->nb_units;
  647. st->nb_units = 0;
  648. oz_hdr = st->oz_hdr;
  649. size = st->size;
  650. }
  651. spin_unlock_bh(&pd->stream_lock);
  652. if (!st)
  653. return 0;
  654. if (!skb) {
  655. /* Allocate enough space for max size frame. */
  656. skb = alloc_skb(pd->max_tx_size + OZ_ALLOCATED_SPACE(dev),
  657. GFP_ATOMIC);
  658. if (skb == NULL)
  659. return 0;
  660. /* Reserve the head room for lower layers. */
  661. skb_reserve(skb, LL_RESERVED_SPACE(dev));
  662. skb_reset_network_header(skb);
  663. skb->dev = dev;
  664. skb->protocol = htons(OZ_ETHERTYPE);
  665. /* For audio packet set priority to AC_VO */
  666. skb->priority = 0x7;
  667. size = sizeof(struct oz_hdr) + sizeof(struct oz_isoc_large);
  668. oz_hdr = (struct oz_hdr *)skb_put(skb, size);
  669. }
  670. memcpy(skb_put(skb, len), data, len);
  671. size += len;
  672. if (++nb_units < pd->ms_per_isoc) {
  673. spin_lock_bh(&pd->stream_lock);
  674. st->skb = skb;
  675. st->nb_units = nb_units;
  676. st->oz_hdr = oz_hdr;
  677. st->size = size;
  678. spin_unlock_bh(&pd->stream_lock);
  679. } else {
  680. struct oz_hdr oz;
  681. struct oz_isoc_large iso;
  682. spin_lock_bh(&pd->stream_lock);
  683. iso.frame_number = st->frame_num;
  684. st->frame_num += nb_units;
  685. spin_unlock_bh(&pd->stream_lock);
  686. oz.control =
  687. (OZ_PROTOCOL_VERSION<<OZ_VERSION_SHIFT) | OZ_F_ISOC;
  688. oz.last_pkt_num = pd->trigger_pkt_num & OZ_LAST_PN_MASK;
  689. oz.pkt_num = 0;
  690. iso.endpoint = ep_num;
  691. iso.format = OZ_DATA_F_ISOC_LARGE;
  692. iso.ms_data = nb_units;
  693. memcpy(oz_hdr, &oz, sizeof(oz));
  694. memcpy(oz_hdr+1, &iso, sizeof(iso));
  695. if (dev_hard_header(skb, dev, OZ_ETHERTYPE, pd->mac_addr,
  696. dev->dev_addr, skb->len) < 0)
  697. goto out;
  698. skb->destructor = oz_isoc_destructor;
  699. /*Queue for Xmit if mode is not ANYTIME*/
  700. if (!(pd->mode & OZ_F_ISOC_ANYTIME)) {
  701. struct oz_tx_frame *isoc_unit = NULL;
  702. int nb = pd->nb_queued_isoc_frames;
  703. if (nb >= pd->isoc_latency) {
  704. struct oz_tx_frame *f;
  705. oz_dbg(TX_FRAMES, "Dropping ISOC Unit nb= %d\n",
  706. nb);
  707. spin_lock(&pd->tx_frame_lock);
  708. list_for_each_entry(f, &pd->tx_queue, link) {
  709. if (f->skb != NULL) {
  710. oz_tx_isoc_free(pd, f);
  711. break;
  712. }
  713. }
  714. spin_unlock(&pd->tx_frame_lock);
  715. }
  716. isoc_unit = oz_tx_frame_alloc(pd);
  717. if (isoc_unit == NULL)
  718. goto out;
  719. isoc_unit->hdr = oz;
  720. isoc_unit->skb = skb;
  721. spin_lock_bh(&pd->tx_frame_lock);
  722. list_add_tail(&isoc_unit->link, &pd->tx_queue);
  723. pd->nb_queued_isoc_frames++;
  724. spin_unlock_bh(&pd->tx_frame_lock);
  725. oz_dbg(TX_FRAMES,
  726. "Added ISOC Frame to Tx Queue isoc_nb= %d, nb= %d\n",
  727. pd->nb_queued_isoc_frames, pd->nb_queued_frames);
  728. return 0;
  729. }
  730. /*In ANYTIME mode Xmit unit immediately*/
  731. if (atomic_read(&g_submitted_isoc) < OZ_MAX_SUBMITTED_ISOC) {
  732. atomic_inc(&g_submitted_isoc);
  733. if (dev_queue_xmit(skb) < 0)
  734. return -1;
  735. return 0;
  736. }
  737. out: kfree_skb(skb);
  738. return -1;
  739. }
  740. return 0;
  741. }
  742. /*
  743. * Context: process
  744. */
  745. void oz_apps_init(void)
  746. {
  747. int i;
  748. for (i = 0; i < OZ_NB_APPS; i++) {
  749. if (g_app_if[i].init)
  750. g_app_if[i].init();
  751. }
  752. }
  753. /*
  754. * Context: process
  755. */
  756. void oz_apps_term(void)
  757. {
  758. int i;
  759. /* Terminate all the apps. */
  760. for (i = 0; i < OZ_NB_APPS; i++) {
  761. if (g_app_if[i].term)
  762. g_app_if[i].term();
  763. }
  764. }
  765. /*
  766. * Context: softirq-serialized
  767. */
  768. void oz_handle_app_elt(struct oz_pd *pd, u8 app_id, struct oz_elt *elt)
  769. {
  770. if (app_id < OZ_NB_APPS && g_app_if[app_id].rx)
  771. g_app_if[app_id].rx(pd, elt);
  772. }
  773. /*
  774. * Context: softirq or process
  775. */
  776. void oz_pd_indicate_farewells(struct oz_pd *pd)
  777. {
  778. struct oz_farewell *f;
  779. const struct oz_app_if *ai = &g_app_if[OZ_APPID_USB];
  780. while (1) {
  781. spin_lock_bh(&g_polling_lock);
  782. if (list_empty(&pd->farewell_list)) {
  783. spin_unlock_bh(&g_polling_lock);
  784. break;
  785. }
  786. f = list_first_entry(&pd->farewell_list,
  787. struct oz_farewell, link);
  788. list_del(&f->link);
  789. spin_unlock_bh(&g_polling_lock);
  790. if (ai->farewell)
  791. ai->farewell(pd, f->ep_num, f->report, f->len);
  792. kfree(f);
  793. }
  794. }