usbatm.c 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231
  1. /******************************************************************************
  2. * usbatm.c - Generic USB xDSL driver core
  3. *
  4. * Copyright (C) 2001, Alcatel
  5. * Copyright (C) 2003, Duncan Sands, SolNegro, Josep Comas
  6. * Copyright (C) 2004, David Woodhouse, Roman Kagan
  7. *
  8. * This program is free software; you can redistribute it and/or modify it
  9. * under the terms of the GNU General Public License as published by the Free
  10. * Software Foundation; either version 2 of the License, or (at your option)
  11. * any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful, but WITHOUT
  14. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  15. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  16. * more details.
  17. *
  18. * You should have received a copy of the GNU General Public License along with
  19. * this program; if not, write to the Free Software Foundation, Inc., 59
  20. * Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  21. *
  22. ******************************************************************************/
  23. /*
  24. * Written by Johan Verrept, Duncan Sands (duncan.sands@free.fr) and David Woodhouse
  25. *
  26. * 1.7+: - See the check-in logs
  27. *
  28. * 1.6: - No longer opens a connection if the firmware is not loaded
  29. * - Added support for the speedtouch 330
  30. * - Removed the limit on the number of devices
  31. * - Module now autoloads on device plugin
  32. * - Merged relevant parts of sarlib
  33. * - Replaced the kernel thread with a tasklet
  34. * - New packet transmission code
  35. * - Changed proc file contents
  36. * - Fixed all known SMP races
  37. * - Many fixes and cleanups
  38. * - Various fixes by Oliver Neukum (oliver@neukum.name)
  39. *
  40. * 1.5A: - Version for inclusion in 2.5 series kernel
  41. * - Modifications by Richard Purdie (rpurdie@rpsys.net)
  42. * - made compatible with kernel 2.5.6 onwards by changing
  43. * usbatm_usb_send_data_context->urb to a pointer and adding code
  44. * to alloc and free it
  45. * - remove_wait_queue() added to usbatm_atm_processqueue_thread()
  46. *
  47. * 1.5: - fixed memory leak when atmsar_decode_aal5 returned NULL.
  48. * (reported by stephen.robinson@zen.co.uk)
  49. *
  50. * 1.4: - changed the spin_lock() under interrupt to spin_lock_irqsave()
  51. * - unlink all active send urbs of a vcc that is being closed.
  52. *
  53. * 1.3.1: - added the version number
  54. *
  55. * 1.3: - Added multiple send urb support
  56. * - fixed memory leak and vcc->tx_inuse starvation bug
  57. * when not enough memory left in vcc.
  58. *
  59. * 1.2: - Fixed race condition in usbatm_usb_send_data()
  60. * 1.1: - Turned off packet debugging
  61. *
  62. */
  63. #include "usbatm.h"
  64. #include <asm/uaccess.h>
  65. #include <linux/crc32.h>
  66. #include <linux/errno.h>
  67. #include <linux/init.h>
  68. #include <linux/interrupt.h>
  69. #include <linux/kernel.h>
  70. #include <linux/module.h>
  71. #include <linux/moduleparam.h>
  72. #include <linux/proc_fs.h>
  73. #include <linux/sched.h>
  74. #include <linux/signal.h>
  75. #include <linux/slab.h>
  76. #include <linux/smp_lock.h>
  77. #include <linux/stat.h>
  78. #include <linux/timer.h>
  79. #include <linux/wait.h>
  80. #ifdef VERBOSE_DEBUG
  81. static int usbatm_print_packet(const unsigned char *data, int len);
  82. #define PACKETDEBUG(arg...) usbatm_print_packet (arg)
  83. #define vdbg(arg...) dbg (arg)
  84. #else
  85. #define PACKETDEBUG(arg...)
  86. #define vdbg(arg...)
  87. #endif
  88. #define DRIVER_AUTHOR "Johan Verrept, Duncan Sands <duncan.sands@free.fr>"
  89. #define DRIVER_VERSION "1.9"
  90. #define DRIVER_DESC "Generic USB ATM/DSL I/O, version " DRIVER_VERSION
  91. static const char usbatm_driver_name[] = "usbatm";
  92. #define UDSL_MAX_RCV_URBS 16
  93. #define UDSL_MAX_SND_URBS 16
  94. #define UDSL_MAX_RCV_BUF_SIZE 1024 /* ATM cells */
  95. #define UDSL_MAX_SND_BUF_SIZE 1024 /* ATM cells */
  96. #define UDSL_DEFAULT_RCV_URBS 4
  97. #define UDSL_DEFAULT_SND_URBS 4
  98. #define UDSL_DEFAULT_RCV_BUF_SIZE 64 /* ATM cells */
  99. #define UDSL_DEFAULT_SND_BUF_SIZE 64 /* ATM cells */
  100. #define ATM_CELL_HEADER (ATM_CELL_SIZE - ATM_CELL_PAYLOAD)
  101. #define THROTTLE_MSECS 100 /* delay to recover processing after urb submission fails */
  102. static unsigned int num_rcv_urbs = UDSL_DEFAULT_RCV_URBS;
  103. static unsigned int num_snd_urbs = UDSL_DEFAULT_SND_URBS;
  104. static unsigned int rcv_buf_size = UDSL_DEFAULT_RCV_BUF_SIZE;
  105. static unsigned int snd_buf_size = UDSL_DEFAULT_SND_BUF_SIZE;
  106. module_param(num_rcv_urbs, uint, S_IRUGO);
  107. MODULE_PARM_DESC(num_rcv_urbs,
  108. "Number of urbs used for reception (range: 0-"
  109. __MODULE_STRING(UDSL_MAX_RCV_URBS) ", default: "
  110. __MODULE_STRING(UDSL_DEFAULT_RCV_URBS) ")");
  111. module_param(num_snd_urbs, uint, S_IRUGO);
  112. MODULE_PARM_DESC(num_snd_urbs,
  113. "Number of urbs used for transmission (range: 0-"
  114. __MODULE_STRING(UDSL_MAX_SND_URBS) ", default: "
  115. __MODULE_STRING(UDSL_DEFAULT_SND_URBS) ")");
  116. module_param(rcv_buf_size, uint, S_IRUGO);
  117. MODULE_PARM_DESC(rcv_buf_size,
  118. "Size of the buffers used for reception in ATM cells (range: 1-"
  119. __MODULE_STRING(UDSL_MAX_RCV_BUF_SIZE) ", default: "
  120. __MODULE_STRING(UDSL_DEFAULT_RCV_BUF_SIZE) ")");
  121. module_param(snd_buf_size, uint, S_IRUGO);
  122. MODULE_PARM_DESC(snd_buf_size,
  123. "Size of the buffers used for transmission in ATM cells (range: 1-"
  124. __MODULE_STRING(UDSL_MAX_SND_BUF_SIZE) ", default: "
  125. __MODULE_STRING(UDSL_DEFAULT_SND_BUF_SIZE) ")");
  126. /* receive */
  127. struct usbatm_vcc_data {
  128. /* vpi/vci lookup */
  129. struct list_head list;
  130. short vpi;
  131. int vci;
  132. struct atm_vcc *vcc;
  133. /* raw cell reassembly */
  134. struct sk_buff *sarb;
  135. };
  136. /* send */
  137. struct usbatm_control {
  138. struct atm_skb_data atm;
  139. u32 len;
  140. u32 crc;
  141. };
  142. #define UDSL_SKB(x) ((struct usbatm_control *)(x)->cb)
  143. /* ATM */
  144. static void usbatm_atm_dev_close(struct atm_dev *dev);
  145. static int usbatm_atm_open(struct atm_vcc *vcc);
  146. static void usbatm_atm_close(struct atm_vcc *vcc);
  147. static int usbatm_atm_ioctl(struct atm_dev *dev, unsigned int cmd, void __user * arg);
  148. static int usbatm_atm_send(struct atm_vcc *vcc, struct sk_buff *skb);
  149. static int usbatm_atm_proc_read(struct atm_dev *atm_dev, loff_t * pos, char *page);
  150. static struct atmdev_ops usbatm_atm_devops = {
  151. .dev_close = usbatm_atm_dev_close,
  152. .open = usbatm_atm_open,
  153. .close = usbatm_atm_close,
  154. .ioctl = usbatm_atm_ioctl,
  155. .send = usbatm_atm_send,
  156. .proc_read = usbatm_atm_proc_read,
  157. .owner = THIS_MODULE,
  158. };
  159. /***********
  160. ** misc **
  161. ***********/
  162. static inline unsigned int usbatm_pdu_length(unsigned int length)
  163. {
  164. length += ATM_CELL_PAYLOAD - 1 + ATM_AAL5_TRAILER;
  165. return length - length % ATM_CELL_PAYLOAD;
  166. }
  167. static inline void usbatm_pop(struct atm_vcc *vcc, struct sk_buff *skb)
  168. {
  169. if (vcc->pop)
  170. vcc->pop(vcc, skb);
  171. else
  172. dev_kfree_skb(skb);
  173. }
  174. /***********
  175. ** urbs **
  176. ************/
  177. static inline struct urb *usbatm_pop_urb(struct usbatm_channel *channel)
  178. {
  179. struct urb *urb;
  180. spin_lock_irq(&channel->lock);
  181. if (list_empty(&channel->list)) {
  182. spin_unlock_irq(&channel->lock);
  183. return NULL;
  184. }
  185. urb = list_entry(channel->list.next, struct urb, urb_list);
  186. list_del(&urb->urb_list);
  187. spin_unlock_irq(&channel->lock);
  188. return urb;
  189. }
  190. static inline int usbatm_submit_urb(struct urb *urb)
  191. {
  192. struct usbatm_channel *channel = urb->context;
  193. int ret;
  194. vdbg("%s: submitting urb 0x%p, size %u",
  195. __func__, urb, urb->transfer_buffer_length);
  196. ret = usb_submit_urb(urb, GFP_ATOMIC);
  197. if (ret) {
  198. atm_dbg(channel->usbatm, "%s: urb 0x%p submission failed (%d)!\n",
  199. __func__, urb, ret);
  200. /* consider all errors transient and return the buffer back to the queue */
  201. urb->status = -EAGAIN;
  202. spin_lock_irq(&channel->lock);
  203. /* must add to the front when sending; doesn't matter when receiving */
  204. list_add(&urb->urb_list, &channel->list);
  205. spin_unlock_irq(&channel->lock);
  206. /* make sure the channel doesn't stall */
  207. mod_timer(&channel->delay, jiffies + msecs_to_jiffies(THROTTLE_MSECS));
  208. }
  209. return ret;
  210. }
  211. static void usbatm_complete(struct urb *urb, struct pt_regs *regs)
  212. {
  213. struct usbatm_channel *channel = urb->context;
  214. unsigned long flags;
  215. vdbg("%s: urb 0x%p, status %d, actual_length %d",
  216. __func__, urb, urb->status, urb->actual_length);
  217. /* usually in_interrupt(), but not always */
  218. spin_lock_irqsave(&channel->lock, flags);
  219. /* must add to the back when receiving; doesn't matter when sending */
  220. list_add_tail(&urb->urb_list, &channel->list);
  221. spin_unlock_irqrestore(&channel->lock, flags);
  222. if (unlikely(urb->status))
  223. /* throttle processing in case of an error */
  224. mod_timer(&channel->delay, jiffies + msecs_to_jiffies(THROTTLE_MSECS));
  225. else
  226. tasklet_schedule(&channel->tasklet);
  227. }
  228. /*************
  229. ** decode **
  230. *************/
  231. static inline struct usbatm_vcc_data *usbatm_find_vcc(struct usbatm_data *instance,
  232. short vpi, int vci)
  233. {
  234. struct usbatm_vcc_data *vcc;
  235. list_for_each_entry(vcc, &instance->vcc_list, list)
  236. if ((vcc->vci == vci) && (vcc->vpi == vpi))
  237. return vcc;
  238. return NULL;
  239. }
  240. static void usbatm_extract_cells(struct usbatm_data *instance,
  241. unsigned char *source, unsigned int avail_data)
  242. {
  243. struct usbatm_vcc_data *cached_vcc = NULL;
  244. struct atm_vcc *vcc;
  245. struct sk_buff *sarb;
  246. struct usbatm_vcc_data *vcc_data;
  247. unsigned int stride = instance->rx_channel.stride;
  248. int vci, cached_vci = 0;
  249. short vpi, cached_vpi = 0;
  250. u8 pti;
  251. for (; avail_data >= stride; avail_data -= stride, source += stride) {
  252. vpi = ((source[0] & 0x0f) << 4) | (source[1] >> 4);
  253. vci = ((source[1] & 0x0f) << 12) | (source[2] << 4) | (source[3] >> 4);
  254. pti = ((source[3] & 0xe) >> 1);
  255. vdbg("%s: vpi %hd, vci %d, pti %d", __func__, vpi, vci, pti);
  256. if (cached_vcc && (vci == cached_vci) && (vpi == cached_vpi))
  257. vcc_data = cached_vcc;
  258. else if ((vcc_data = usbatm_find_vcc(instance, vpi, vci))) {
  259. cached_vcc = vcc_data;
  260. cached_vpi = vpi;
  261. cached_vci = vci;
  262. } else {
  263. atm_dbg(instance, "%s: unknown vpi/vci (%hd/%d)!\n", __func__, vpi, vci);
  264. continue;
  265. }
  266. vcc = vcc_data->vcc;
  267. /* OAM F5 end-to-end */
  268. if (pti == ATM_PTI_E2EF5) {
  269. atm_warn(instance, "%s: OAM not supported (vpi %d, vci %d)!\n", __func__, vpi, vci);
  270. atomic_inc(&vcc->stats->rx_err);
  271. continue;
  272. }
  273. sarb = vcc_data->sarb;
  274. if (sarb->tail + ATM_CELL_PAYLOAD > sarb->end) {
  275. atm_dbg(instance, "%s: buffer overrun (sarb->len %u, vcc: 0x%p)!\n",
  276. __func__, sarb->len, vcc);
  277. /* discard cells already received */
  278. skb_trim(sarb, 0);
  279. UDSL_ASSERT(sarb->tail + ATM_CELL_PAYLOAD <= sarb->end);
  280. }
  281. memcpy(sarb->tail, source + ATM_CELL_HEADER, ATM_CELL_PAYLOAD);
  282. __skb_put(sarb, ATM_CELL_PAYLOAD);
  283. if (pti & 1) {
  284. struct sk_buff *skb;
  285. unsigned int length;
  286. unsigned int pdu_length;
  287. length = (source[ATM_CELL_SIZE - 6] << 8) + source[ATM_CELL_SIZE - 5];
  288. /* guard against overflow */
  289. if (length > ATM_MAX_AAL5_PDU) {
  290. atm_dbg(instance, "%s: bogus length %u (vcc: 0x%p)!\n",
  291. __func__, length, vcc);
  292. atomic_inc(&vcc->stats->rx_err);
  293. goto out;
  294. }
  295. pdu_length = usbatm_pdu_length(length);
  296. if (sarb->len < pdu_length) {
  297. atm_dbg(instance, "%s: bogus pdu_length %u (sarb->len: %u, vcc: 0x%p)!\n",
  298. __func__, pdu_length, sarb->len, vcc);
  299. atomic_inc(&vcc->stats->rx_err);
  300. goto out;
  301. }
  302. if (crc32_be(~0, sarb->tail - pdu_length, pdu_length) != 0xc704dd7b) {
  303. atm_dbg(instance, "%s: packet failed crc check (vcc: 0x%p)!\n",
  304. __func__, vcc);
  305. atomic_inc(&vcc->stats->rx_err);
  306. goto out;
  307. }
  308. vdbg("%s: got packet (length: %u, pdu_length: %u, vcc: 0x%p)", __func__, length, pdu_length, vcc);
  309. if (!(skb = dev_alloc_skb(length))) {
  310. atm_dbg(instance, "%s: no memory for skb (length: %u)!\n", __func__, length);
  311. atomic_inc(&vcc->stats->rx_drop);
  312. goto out;
  313. }
  314. vdbg("%s: allocated new sk_buff (skb: 0x%p, skb->truesize: %u)", __func__, skb, skb->truesize);
  315. if (!atm_charge(vcc, skb->truesize)) {
  316. atm_dbg(instance, "%s: failed atm_charge (skb->truesize: %u)!\n", __func__, skb->truesize);
  317. dev_kfree_skb(skb);
  318. goto out; /* atm_charge increments rx_drop */
  319. }
  320. memcpy(skb->data, sarb->tail - pdu_length, length);
  321. __skb_put(skb, length);
  322. vdbg("%s: sending skb 0x%p, skb->len %u, skb->truesize %u",
  323. __func__, skb, skb->len, skb->truesize);
  324. PACKETDEBUG(skb->data, skb->len);
  325. vcc->push(vcc, skb);
  326. atomic_inc(&vcc->stats->rx);
  327. out:
  328. skb_trim(sarb, 0);
  329. }
  330. }
  331. }
  332. /*************
  333. ** encode **
  334. *************/
  335. static unsigned int usbatm_write_cells(struct usbatm_data *instance,
  336. struct sk_buff *skb,
  337. u8 *target, unsigned int avail_space)
  338. {
  339. struct usbatm_control *ctrl = UDSL_SKB(skb);
  340. struct atm_vcc *vcc = ctrl->atm.vcc;
  341. unsigned int num_written;
  342. unsigned int stride = instance->tx_channel.stride;
  343. vdbg("%s: skb->len=%d, avail_space=%u", __func__, skb->len, avail_space);
  344. UDSL_ASSERT(!(avail_space % stride));
  345. for (num_written = 0; num_written < avail_space && ctrl->len;
  346. num_written += stride, target += stride) {
  347. unsigned int data_len = min_t(unsigned int, skb->len, ATM_CELL_PAYLOAD);
  348. unsigned int left = ATM_CELL_PAYLOAD - data_len;
  349. u8 *ptr = target;
  350. ptr[0] = vcc->vpi >> 4;
  351. ptr[1] = (vcc->vpi << 4) | (vcc->vci >> 12);
  352. ptr[2] = vcc->vci >> 4;
  353. ptr[3] = vcc->vci << 4;
  354. ptr[4] = 0xec;
  355. ptr += ATM_CELL_HEADER;
  356. memcpy(ptr, skb->data, data_len);
  357. ptr += data_len;
  358. __skb_pull(skb, data_len);
  359. if(!left)
  360. continue;
  361. memset(ptr, 0, left);
  362. if (left >= ATM_AAL5_TRAILER) { /* trailer will go in this cell */
  363. u8 *trailer = target + ATM_CELL_SIZE - ATM_AAL5_TRAILER;
  364. /* trailer[0] = 0; UU = 0 */
  365. /* trailer[1] = 0; CPI = 0 */
  366. trailer[2] = ctrl->len >> 8;
  367. trailer[3] = ctrl->len;
  368. ctrl->crc = ~ crc32_be(ctrl->crc, ptr, left - 4);
  369. trailer[4] = ctrl->crc >> 24;
  370. trailer[5] = ctrl->crc >> 16;
  371. trailer[6] = ctrl->crc >> 8;
  372. trailer[7] = ctrl->crc;
  373. target[3] |= 0x2; /* adjust PTI */
  374. ctrl->len = 0; /* tag this skb finished */
  375. }
  376. else
  377. ctrl->crc = crc32_be(ctrl->crc, ptr, left);
  378. }
  379. return num_written;
  380. }
  381. /**************
  382. ** receive **
  383. **************/
  384. static void usbatm_rx_process(unsigned long data)
  385. {
  386. struct usbatm_data *instance = (struct usbatm_data *)data;
  387. struct urb *urb;
  388. while ((urb = usbatm_pop_urb(&instance->rx_channel))) {
  389. vdbg("%s: processing urb 0x%p", __func__, urb);
  390. if (usb_pipeisoc(urb->pipe)) {
  391. int i;
  392. for (i = 0; i < urb->number_of_packets; i++)
  393. if (!urb->iso_frame_desc[i].status)
  394. usbatm_extract_cells(instance,
  395. (u8 *)urb->transfer_buffer + urb->iso_frame_desc[i].offset,
  396. urb->iso_frame_desc[i].actual_length);
  397. }
  398. else
  399. if (!urb->status)
  400. usbatm_extract_cells(instance, urb->transfer_buffer, urb->actual_length);
  401. if (usbatm_submit_urb(urb))
  402. return;
  403. }
  404. }
  405. /***********
  406. ** send **
  407. ***********/
  408. static void usbatm_tx_process(unsigned long data)
  409. {
  410. struct usbatm_data *instance = (struct usbatm_data *)data;
  411. struct sk_buff *skb = instance->current_skb;
  412. struct urb *urb = NULL;
  413. const unsigned int buf_size = instance->tx_channel.buf_size;
  414. unsigned int num_written = 0;
  415. u8 *buffer = NULL;
  416. if (!skb)
  417. skb = skb_dequeue(&instance->sndqueue);
  418. while (skb) {
  419. if (!urb) {
  420. urb = usbatm_pop_urb(&instance->tx_channel);
  421. if (!urb)
  422. break; /* no more senders */
  423. buffer = urb->transfer_buffer;
  424. num_written = (urb->status == -EAGAIN) ?
  425. urb->transfer_buffer_length : 0;
  426. }
  427. num_written += usbatm_write_cells(instance, skb,
  428. buffer + num_written,
  429. buf_size - num_written);
  430. vdbg("%s: wrote %u bytes from skb 0x%p to urb 0x%p",
  431. __func__, num_written, skb, urb);
  432. if (!UDSL_SKB(skb)->len) {
  433. struct atm_vcc *vcc = UDSL_SKB(skb)->atm.vcc;
  434. usbatm_pop(vcc, skb);
  435. atomic_inc(&vcc->stats->tx);
  436. skb = skb_dequeue(&instance->sndqueue);
  437. }
  438. if (num_written == buf_size || (!skb && num_written)) {
  439. urb->transfer_buffer_length = num_written;
  440. if (usbatm_submit_urb(urb))
  441. break;
  442. urb = NULL;
  443. }
  444. }
  445. instance->current_skb = skb;
  446. }
  447. static void usbatm_cancel_send(struct usbatm_data *instance,
  448. struct atm_vcc *vcc)
  449. {
  450. struct sk_buff *skb, *n;
  451. atm_dbg(instance, "%s entered\n", __func__);
  452. spin_lock_irq(&instance->sndqueue.lock);
  453. for (skb = instance->sndqueue.next, n = skb->next;
  454. skb != (struct sk_buff *)&instance->sndqueue;
  455. skb = n, n = skb->next)
  456. if (UDSL_SKB(skb)->atm.vcc == vcc) {
  457. atm_dbg(instance, "%s: popping skb 0x%p\n", __func__, skb);
  458. __skb_unlink(skb, &instance->sndqueue);
  459. usbatm_pop(vcc, skb);
  460. }
  461. spin_unlock_irq(&instance->sndqueue.lock);
  462. tasklet_disable(&instance->tx_channel.tasklet);
  463. if ((skb = instance->current_skb) && (UDSL_SKB(skb)->atm.vcc == vcc)) {
  464. atm_dbg(instance, "%s: popping current skb (0x%p)\n", __func__, skb);
  465. instance->current_skb = NULL;
  466. usbatm_pop(vcc, skb);
  467. }
  468. tasklet_enable(&instance->tx_channel.tasklet);
  469. atm_dbg(instance, "%s done\n", __func__);
  470. }
  471. static int usbatm_atm_send(struct atm_vcc *vcc, struct sk_buff *skb)
  472. {
  473. struct usbatm_data *instance = vcc->dev->dev_data;
  474. struct usbatm_control *ctrl = UDSL_SKB(skb);
  475. int err;
  476. vdbg("%s called (skb 0x%p, len %u)", __func__, skb, skb->len);
  477. if (!instance) {
  478. dbg("%s: NULL data!", __func__);
  479. err = -ENODEV;
  480. goto fail;
  481. }
  482. if (vcc->qos.aal != ATM_AAL5) {
  483. atm_dbg(instance, "%s: unsupported ATM type %d!\n", __func__, vcc->qos.aal);
  484. err = -EINVAL;
  485. goto fail;
  486. }
  487. if (skb->len > ATM_MAX_AAL5_PDU) {
  488. atm_dbg(instance, "%s: packet too long (%d vs %d)!\n",
  489. __func__, skb->len, ATM_MAX_AAL5_PDU);
  490. err = -EINVAL;
  491. goto fail;
  492. }
  493. PACKETDEBUG(skb->data, skb->len);
  494. /* initialize the control block */
  495. ctrl->atm.vcc = vcc;
  496. ctrl->len = skb->len;
  497. ctrl->crc = crc32_be(~0, skb->data, skb->len);
  498. skb_queue_tail(&instance->sndqueue, skb);
  499. tasklet_schedule(&instance->tx_channel.tasklet);
  500. return 0;
  501. fail:
  502. usbatm_pop(vcc, skb);
  503. return err;
  504. }
  505. /********************
  506. ** bean counting **
  507. ********************/
  508. static void usbatm_destroy_instance(struct kref *kref)
  509. {
  510. struct usbatm_data *instance = container_of(kref, struct usbatm_data, refcount);
  511. dbg("%s", __func__);
  512. tasklet_kill(&instance->rx_channel.tasklet);
  513. tasklet_kill(&instance->tx_channel.tasklet);
  514. usb_put_dev(instance->usb_dev);
  515. kfree(instance);
  516. }
  517. void usbatm_get_instance(struct usbatm_data *instance)
  518. {
  519. dbg("%s", __func__);
  520. kref_get(&instance->refcount);
  521. }
  522. void usbatm_put_instance(struct usbatm_data *instance)
  523. {
  524. dbg("%s", __func__);
  525. kref_put(&instance->refcount, usbatm_destroy_instance);
  526. }
  527. /**********
  528. ** ATM **
  529. **********/
  530. static void usbatm_atm_dev_close(struct atm_dev *dev)
  531. {
  532. struct usbatm_data *instance = dev->dev_data;
  533. dbg("%s", __func__);
  534. if (!instance)
  535. return;
  536. dev->dev_data = NULL;
  537. usbatm_put_instance(instance); /* taken in usbatm_atm_init */
  538. }
  539. static int usbatm_atm_proc_read(struct atm_dev *atm_dev, loff_t * pos, char *page)
  540. {
  541. struct usbatm_data *instance = atm_dev->dev_data;
  542. int left = *pos;
  543. if (!instance) {
  544. dbg("%s: NULL instance!", __func__);
  545. return -ENODEV;
  546. }
  547. if (!left--)
  548. return sprintf(page, "%s\n", instance->description);
  549. if (!left--)
  550. return sprintf(page, "MAC: %02x:%02x:%02x:%02x:%02x:%02x\n",
  551. atm_dev->esi[0], atm_dev->esi[1],
  552. atm_dev->esi[2], atm_dev->esi[3],
  553. atm_dev->esi[4], atm_dev->esi[5]);
  554. if (!left--)
  555. return sprintf(page,
  556. "AAL5: tx %d ( %d err ), rx %d ( %d err, %d drop )\n",
  557. atomic_read(&atm_dev->stats.aal5.tx),
  558. atomic_read(&atm_dev->stats.aal5.tx_err),
  559. atomic_read(&atm_dev->stats.aal5.rx),
  560. atomic_read(&atm_dev->stats.aal5.rx_err),
  561. atomic_read(&atm_dev->stats.aal5.rx_drop));
  562. if (!left--)
  563. switch (atm_dev->signal) {
  564. case ATM_PHY_SIG_FOUND:
  565. return sprintf(page, "Line up\n");
  566. case ATM_PHY_SIG_LOST:
  567. return sprintf(page, "Line down\n");
  568. default:
  569. return sprintf(page, "Line state unknown\n");
  570. }
  571. return 0;
  572. }
  573. static int usbatm_atm_open(struct atm_vcc *vcc)
  574. {
  575. struct usbatm_data *instance = vcc->dev->dev_data;
  576. struct usbatm_vcc_data *new = NULL;
  577. int ret;
  578. int vci = vcc->vci;
  579. short vpi = vcc->vpi;
  580. if (!instance) {
  581. dbg("%s: NULL data!", __func__);
  582. return -ENODEV;
  583. }
  584. atm_dbg(instance, "%s: vpi %hd, vci %d\n", __func__, vpi, vci);
  585. /* only support AAL5 */
  586. if ((vcc->qos.aal != ATM_AAL5) || (vcc->qos.rxtp.max_sdu < 0)
  587. || (vcc->qos.rxtp.max_sdu > ATM_MAX_AAL5_PDU)) {
  588. atm_dbg(instance, "%s: unsupported ATM type %d!\n", __func__, vcc->qos.aal);
  589. return -EINVAL;
  590. }
  591. down(&instance->serialize); /* vs self, usbatm_atm_close */
  592. if (usbatm_find_vcc(instance, vpi, vci)) {
  593. atm_dbg(instance, "%s: %hd/%d already in use!\n", __func__, vpi, vci);
  594. ret = -EADDRINUSE;
  595. goto fail;
  596. }
  597. if (!(new = kmalloc(sizeof(struct usbatm_vcc_data), GFP_KERNEL))) {
  598. atm_dbg(instance, "%s: no memory for vcc_data!\n", __func__);
  599. ret = -ENOMEM;
  600. goto fail;
  601. }
  602. memset(new, 0, sizeof(struct usbatm_vcc_data));
  603. new->vcc = vcc;
  604. new->vpi = vpi;
  605. new->vci = vci;
  606. new->sarb = alloc_skb(usbatm_pdu_length(vcc->qos.rxtp.max_sdu), GFP_KERNEL);
  607. if (!new->sarb) {
  608. atm_dbg(instance, "%s: no memory for SAR buffer!\n", __func__);
  609. ret = -ENOMEM;
  610. goto fail;
  611. }
  612. vcc->dev_data = new;
  613. tasklet_disable(&instance->rx_channel.tasklet);
  614. list_add(&new->list, &instance->vcc_list);
  615. tasklet_enable(&instance->rx_channel.tasklet);
  616. set_bit(ATM_VF_ADDR, &vcc->flags);
  617. set_bit(ATM_VF_PARTIAL, &vcc->flags);
  618. set_bit(ATM_VF_READY, &vcc->flags);
  619. up(&instance->serialize);
  620. atm_dbg(instance, "%s: allocated vcc data 0x%p\n", __func__, new);
  621. return 0;
  622. fail:
  623. kfree(new);
  624. up(&instance->serialize);
  625. return ret;
  626. }
  627. static void usbatm_atm_close(struct atm_vcc *vcc)
  628. {
  629. struct usbatm_data *instance = vcc->dev->dev_data;
  630. struct usbatm_vcc_data *vcc_data = vcc->dev_data;
  631. if (!instance || !vcc_data) {
  632. dbg("%s: NULL data!", __func__);
  633. return;
  634. }
  635. atm_dbg(instance, "%s entered\n", __func__);
  636. atm_dbg(instance, "%s: deallocating vcc 0x%p with vpi %d vci %d\n",
  637. __func__, vcc_data, vcc_data->vpi, vcc_data->vci);
  638. usbatm_cancel_send(instance, vcc);
  639. down(&instance->serialize); /* vs self, usbatm_atm_open */
  640. tasklet_disable(&instance->rx_channel.tasklet);
  641. list_del(&vcc_data->list);
  642. tasklet_enable(&instance->rx_channel.tasklet);
  643. kfree_skb(vcc_data->sarb);
  644. vcc_data->sarb = NULL;
  645. kfree(vcc_data);
  646. vcc->dev_data = NULL;
  647. vcc->vpi = ATM_VPI_UNSPEC;
  648. vcc->vci = ATM_VCI_UNSPEC;
  649. clear_bit(ATM_VF_READY, &vcc->flags);
  650. clear_bit(ATM_VF_PARTIAL, &vcc->flags);
  651. clear_bit(ATM_VF_ADDR, &vcc->flags);
  652. up(&instance->serialize);
  653. atm_dbg(instance, "%s successful\n", __func__);
  654. }
  655. static int usbatm_atm_ioctl(struct atm_dev *dev, unsigned int cmd,
  656. void __user * arg)
  657. {
  658. switch (cmd) {
  659. case ATM_QUERYLOOP:
  660. return put_user(ATM_LM_NONE, (int __user *)arg) ? -EFAULT : 0;
  661. default:
  662. return -ENOIOCTLCMD;
  663. }
  664. }
  665. static int usbatm_atm_init(struct usbatm_data *instance)
  666. {
  667. struct atm_dev *atm_dev;
  668. int ret, i;
  669. /* ATM init */
  670. atm_dev = atm_dev_register(instance->driver_name, &usbatm_atm_devops, -1, NULL);
  671. if (!atm_dev) {
  672. usb_dbg(instance, "%s: failed to register ATM device!\n", __func__);
  673. return -1;
  674. }
  675. instance->atm_dev = atm_dev;
  676. atm_dev->ci_range.vpi_bits = ATM_CI_MAX;
  677. atm_dev->ci_range.vci_bits = ATM_CI_MAX;
  678. atm_dev->signal = ATM_PHY_SIG_UNKNOWN;
  679. /* temp init ATM device, set to 128kbit */
  680. atm_dev->link_rate = 128 * 1000 / 424;
  681. if (instance->driver->atm_start && ((ret = instance->driver->atm_start(instance, atm_dev)) < 0)) {
  682. atm_dbg(instance, "%s: atm_start failed: %d!\n", __func__, ret);
  683. goto fail;
  684. }
  685. /* ready for ATM callbacks */
  686. usbatm_get_instance(instance); /* dropped in usbatm_atm_dev_close */
  687. mb();
  688. atm_dev->dev_data = instance;
  689. /* submit all rx URBs */
  690. for (i = 0; i < num_rcv_urbs; i++)
  691. usbatm_submit_urb(instance->urbs[i]);
  692. return 0;
  693. fail:
  694. instance->atm_dev = NULL;
  695. shutdown_atm_dev(atm_dev); /* usbatm_atm_dev_close will eventually be called */
  696. return ret;
  697. }
  698. /**********
  699. ** USB **
  700. **********/
  701. static int usbatm_do_heavy_init(void *arg)
  702. {
  703. struct usbatm_data *instance = arg;
  704. int ret;
  705. daemonize(instance->driver->driver_name);
  706. allow_signal(SIGTERM);
  707. complete(&instance->thread_started);
  708. ret = instance->driver->heavy_init(instance, instance->usb_intf);
  709. if (!ret)
  710. ret = usbatm_atm_init(instance);
  711. down(&instance->serialize);
  712. instance->thread_pid = -1;
  713. up(&instance->serialize);
  714. complete_and_exit(&instance->thread_exited, ret);
  715. }
  716. static int usbatm_heavy_init(struct usbatm_data *instance)
  717. {
  718. int ret = kernel_thread(usbatm_do_heavy_init, instance, CLONE_KERNEL);
  719. if (ret < 0) {
  720. usb_dbg(instance, "%s: failed to create kernel_thread (%d)!\n", __func__, ret);
  721. return ret;
  722. }
  723. down(&instance->serialize);
  724. instance->thread_pid = ret;
  725. up(&instance->serialize);
  726. wait_for_completion(&instance->thread_started);
  727. return 0;
  728. }
  729. static void usbatm_tasklet_schedule(unsigned long data)
  730. {
  731. tasklet_schedule((struct tasklet_struct *) data);
  732. }
  733. static inline void usbatm_init_channel(struct usbatm_channel *channel)
  734. {
  735. spin_lock_init(&channel->lock);
  736. INIT_LIST_HEAD(&channel->list);
  737. channel->delay.function = usbatm_tasklet_schedule;
  738. channel->delay.data = (unsigned long) &channel->tasklet;
  739. init_timer(&channel->delay);
  740. }
  741. int usbatm_usb_probe(struct usb_interface *intf, const struct usb_device_id *id,
  742. struct usbatm_driver *driver)
  743. {
  744. struct device *dev = &intf->dev;
  745. struct usb_device *usb_dev = interface_to_usbdev(intf);
  746. struct usbatm_data *instance;
  747. char *buf;
  748. int error = -ENOMEM;
  749. int i, length;
  750. int need_heavy;
  751. dev_dbg(dev, "%s: trying driver %s with vendor=0x%x, product=0x%x, ifnum %d\n",
  752. __func__, driver->driver_name,
  753. le16_to_cpu(usb_dev->descriptor.idVendor),
  754. le16_to_cpu(usb_dev->descriptor.idProduct),
  755. intf->altsetting->desc.bInterfaceNumber);
  756. /* instance init */
  757. instance = kmalloc(sizeof(*instance) + sizeof(struct urb *) * (num_rcv_urbs + num_snd_urbs),
  758. GFP_KERNEL);
  759. if (!instance) {
  760. dev_dbg(dev, "%s: no memory for instance data!\n", __func__);
  761. return -ENOMEM;
  762. }
  763. memset(instance, 0, sizeof(*instance));
  764. /* public fields */
  765. instance->driver = driver;
  766. snprintf(instance->driver_name, sizeof(instance->driver_name), driver->driver_name);
  767. instance->usb_dev = usb_dev;
  768. instance->usb_intf = intf;
  769. buf = instance->description;
  770. length = sizeof(instance->description);
  771. if ((i = usb_string(usb_dev, usb_dev->descriptor.iProduct, buf, length)) < 0)
  772. goto bind;
  773. buf += i;
  774. length -= i;
  775. i = scnprintf(buf, length, " (");
  776. buf += i;
  777. length -= i;
  778. if (length <= 0 || (i = usb_make_path(usb_dev, buf, length)) < 0)
  779. goto bind;
  780. buf += i;
  781. length -= i;
  782. snprintf(buf, length, ")");
  783. bind:
  784. need_heavy = 1;
  785. if (driver->bind && (error = driver->bind(instance, intf, id, &need_heavy)) < 0) {
  786. dev_dbg(dev, "%s: bind failed: %d!\n", __func__, error);
  787. goto fail_free;
  788. }
  789. /* private fields */
  790. kref_init(&instance->refcount); /* dropped in usbatm_usb_disconnect */
  791. init_MUTEX(&instance->serialize);
  792. instance->thread_pid = -1;
  793. init_completion(&instance->thread_started);
  794. init_completion(&instance->thread_exited);
  795. INIT_LIST_HEAD(&instance->vcc_list);
  796. usbatm_init_channel(&instance->rx_channel);
  797. usbatm_init_channel(&instance->tx_channel);
  798. tasklet_init(&instance->rx_channel.tasklet, usbatm_rx_process, (unsigned long)instance);
  799. tasklet_init(&instance->tx_channel.tasklet, usbatm_tx_process, (unsigned long)instance);
  800. instance->rx_channel.endpoint = usb_rcvbulkpipe(usb_dev, driver->in);
  801. instance->tx_channel.endpoint = usb_sndbulkpipe(usb_dev, driver->out);
  802. instance->rx_channel.stride = ATM_CELL_SIZE + driver->rx_padding;
  803. instance->tx_channel.stride = ATM_CELL_SIZE + driver->tx_padding;
  804. instance->rx_channel.buf_size = rcv_buf_size * instance->rx_channel.stride;
  805. instance->tx_channel.buf_size = snd_buf_size * instance->tx_channel.stride;
  806. instance->rx_channel.usbatm = instance->tx_channel.usbatm = instance;
  807. skb_queue_head_init(&instance->sndqueue);
  808. for (i = 0; i < num_rcv_urbs + num_snd_urbs; i++) {
  809. struct urb *urb;
  810. u8 *buffer;
  811. unsigned int iso_packets = 0, iso_size = 0;
  812. struct usbatm_channel *channel = i < num_rcv_urbs ?
  813. &instance->rx_channel : &instance->tx_channel;
  814. if (usb_pipeisoc(channel->endpoint)) {
  815. /* don't expect iso out endpoints */
  816. iso_size = usb_maxpacket(instance->usb_dev, channel->endpoint, 0);
  817. iso_size -= iso_size % channel->stride; /* alignment */
  818. BUG_ON(!iso_size);
  819. iso_packets = (channel->buf_size - 1) / iso_size + 1;
  820. }
  821. urb = usb_alloc_urb(iso_packets, GFP_KERNEL);
  822. if (!urb) {
  823. dev_dbg(dev, "%s: no memory for urb %d!\n", __func__, i);
  824. goto fail_unbind;
  825. }
  826. buffer = kmalloc(channel->buf_size, GFP_KERNEL);
  827. if (!buffer) {
  828. dev_dbg(dev, "%s: no memory for buffer %d!\n", __func__, i);
  829. goto fail_unbind;
  830. }
  831. memset(buffer, 0, channel->buf_size);
  832. usb_fill_bulk_urb(urb, instance->usb_dev, channel->endpoint,
  833. buffer, channel->buf_size, usbatm_complete, channel);
  834. if (iso_packets) {
  835. int j;
  836. urb->interval = 1;
  837. urb->transfer_flags = URB_ISO_ASAP;
  838. urb->number_of_packets = iso_packets;
  839. for (j = 0; j < iso_packets; j++) {
  840. urb->iso_frame_desc[j].offset = iso_size * j;
  841. urb->iso_frame_desc[j].length = min_t(int, iso_size,
  842. channel->buf_size - urb->iso_frame_desc[j].offset);
  843. }
  844. }
  845. /* put all tx URBs on the list of spares */
  846. if (i >= num_rcv_urbs)
  847. list_add_tail(&urb->urb_list, &channel->list);
  848. vdbg("%s: alloced buffer 0x%p buf size %u urb 0x%p",
  849. __func__, urb->transfer_buffer, urb->transfer_buffer_length, urb);
  850. instance->urbs[i] = urb;
  851. }
  852. if (need_heavy && driver->heavy_init) {
  853. error = usbatm_heavy_init(instance);
  854. } else {
  855. complete(&instance->thread_exited); /* pretend that heavy_init was run */
  856. error = usbatm_atm_init(instance);
  857. }
  858. if (error < 0)
  859. goto fail_unbind;
  860. usb_get_dev(usb_dev);
  861. usb_set_intfdata(intf, instance);
  862. return 0;
  863. fail_unbind:
  864. if (instance->driver->unbind)
  865. instance->driver->unbind(instance, intf);
  866. fail_free:
  867. for (i = 0; i < num_rcv_urbs + num_snd_urbs; i++) {
  868. if (instance->urbs[i])
  869. kfree(instance->urbs[i]->transfer_buffer);
  870. usb_free_urb(instance->urbs[i]);
  871. }
  872. kfree (instance);
  873. return error;
  874. }
  875. EXPORT_SYMBOL_GPL(usbatm_usb_probe);
  876. void usbatm_usb_disconnect(struct usb_interface *intf)
  877. {
  878. struct device *dev = &intf->dev;
  879. struct usbatm_data *instance = usb_get_intfdata(intf);
  880. int i;
  881. dev_dbg(dev, "%s entered\n", __func__);
  882. if (!instance) {
  883. dev_dbg(dev, "%s: NULL instance!\n", __func__);
  884. return;
  885. }
  886. usb_set_intfdata(intf, NULL);
  887. down(&instance->serialize);
  888. if (instance->thread_pid >= 0)
  889. kill_proc(instance->thread_pid, SIGTERM, 1);
  890. up(&instance->serialize);
  891. wait_for_completion(&instance->thread_exited);
  892. tasklet_disable(&instance->rx_channel.tasklet);
  893. tasklet_disable(&instance->tx_channel.tasklet);
  894. for (i = 0; i < num_rcv_urbs + num_snd_urbs; i++)
  895. usb_kill_urb(instance->urbs[i]);
  896. del_timer_sync(&instance->rx_channel.delay);
  897. del_timer_sync(&instance->tx_channel.delay);
  898. if (instance->atm_dev && instance->driver->atm_stop)
  899. instance->driver->atm_stop(instance, instance->atm_dev);
  900. if (instance->driver->unbind)
  901. instance->driver->unbind(instance, intf);
  902. instance->driver_data = NULL;
  903. /* turn usbatm_[rt]x_process into noop */
  904. /* no need to take the spinlock */
  905. INIT_LIST_HEAD(&instance->rx_channel.list);
  906. INIT_LIST_HEAD(&instance->tx_channel.list);
  907. tasklet_enable(&instance->rx_channel.tasklet);
  908. tasklet_enable(&instance->tx_channel.tasklet);
  909. for (i = 0; i < num_rcv_urbs + num_snd_urbs; i++) {
  910. kfree(instance->urbs[i]->transfer_buffer);
  911. usb_free_urb(instance->urbs[i]);
  912. }
  913. /* ATM finalize */
  914. if (instance->atm_dev)
  915. shutdown_atm_dev(instance->atm_dev);
  916. usbatm_put_instance(instance); /* taken in usbatm_usb_probe */
  917. }
  918. EXPORT_SYMBOL_GPL(usbatm_usb_disconnect);
  919. /***********
  920. ** init **
  921. ***********/
  922. static int __init usbatm_usb_init(void)
  923. {
  924. dbg("%s: driver version %s", __func__, DRIVER_VERSION);
  925. if (sizeof(struct usbatm_control) > sizeof(((struct sk_buff *) 0)->cb)) {
  926. printk(KERN_ERR "%s unusable with this kernel!\n", usbatm_driver_name);
  927. return -EIO;
  928. }
  929. if ((num_rcv_urbs > UDSL_MAX_RCV_URBS)
  930. || (num_snd_urbs > UDSL_MAX_SND_URBS)
  931. || (rcv_buf_size < 1)
  932. || (rcv_buf_size > UDSL_MAX_RCV_BUF_SIZE)
  933. || (snd_buf_size < 1)
  934. || (snd_buf_size > UDSL_MAX_SND_BUF_SIZE))
  935. return -EINVAL;
  936. return 0;
  937. }
  938. module_init(usbatm_usb_init);
  939. static void __exit usbatm_usb_exit(void)
  940. {
  941. dbg("%s", __func__);
  942. }
  943. module_exit(usbatm_usb_exit);
  944. MODULE_AUTHOR(DRIVER_AUTHOR);
  945. MODULE_DESCRIPTION(DRIVER_DESC);
  946. MODULE_LICENSE("GPL");
  947. MODULE_VERSION(DRIVER_VERSION);
  948. /************
  949. ** debug **
  950. ************/
  951. #ifdef VERBOSE_DEBUG
  952. static int usbatm_print_packet(const unsigned char *data, int len)
  953. {
  954. unsigned char buffer[256];
  955. int i = 0, j = 0;
  956. for (i = 0; i < len;) {
  957. buffer[0] = '\0';
  958. sprintf(buffer, "%.3d :", i);
  959. for (j = 0; (j < 16) && (i < len); j++, i++) {
  960. sprintf(buffer, "%s %2.2x", buffer, data[i]);
  961. }
  962. dbg("%s", buffer);
  963. }
  964. return i;
  965. }
  966. #endif