usbatm.c 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387
  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/netdevice.h>
  73. #include <linux/proc_fs.h>
  74. #include <linux/sched.h>
  75. #include <linux/signal.h>
  76. #include <linux/slab.h>
  77. #include <linux/stat.h>
  78. #include <linux/timer.h>
  79. #include <linux/wait.h>
  80. #include <linux/kthread.h>
  81. #include <linux/ratelimit.h>
  82. #ifdef VERBOSE_DEBUG
  83. static int usbatm_print_packet(struct usbatm_data *instance, const unsigned char *data, int len);
  84. #define PACKETDEBUG(arg...) usbatm_print_packet(arg)
  85. #define vdbg(arg...) dev_dbg(arg)
  86. #else
  87. #define PACKETDEBUG(arg...)
  88. #define vdbg(arg...)
  89. #endif
  90. #define DRIVER_AUTHOR "Johan Verrept, Duncan Sands <duncan.sands@free.fr>"
  91. #define DRIVER_VERSION "1.10"
  92. #define DRIVER_DESC "Generic USB ATM/DSL I/O, version " DRIVER_VERSION
  93. static const char usbatm_driver_name[] = "usbatm";
  94. #define UDSL_MAX_RCV_URBS 16
  95. #define UDSL_MAX_SND_URBS 16
  96. #define UDSL_MAX_BUF_SIZE 65536
  97. #define UDSL_DEFAULT_RCV_URBS 4
  98. #define UDSL_DEFAULT_SND_URBS 4
  99. #define UDSL_DEFAULT_RCV_BUF_SIZE 3392 /* 64 * ATM_CELL_SIZE */
  100. #define UDSL_DEFAULT_SND_BUF_SIZE 3392 /* 64 * ATM_CELL_SIZE */
  101. #define ATM_CELL_HEADER (ATM_CELL_SIZE - ATM_CELL_PAYLOAD)
  102. #define THROTTLE_MSECS 100 /* delay to recover processing after urb submission fails */
  103. static unsigned int num_rcv_urbs = UDSL_DEFAULT_RCV_URBS;
  104. static unsigned int num_snd_urbs = UDSL_DEFAULT_SND_URBS;
  105. static unsigned int rcv_buf_bytes = UDSL_DEFAULT_RCV_BUF_SIZE;
  106. static unsigned int snd_buf_bytes = UDSL_DEFAULT_SND_BUF_SIZE;
  107. module_param(num_rcv_urbs, uint, S_IRUGO);
  108. MODULE_PARM_DESC(num_rcv_urbs,
  109. "Number of urbs used for reception (range: 0-"
  110. __MODULE_STRING(UDSL_MAX_RCV_URBS) ", default: "
  111. __MODULE_STRING(UDSL_DEFAULT_RCV_URBS) ")");
  112. module_param(num_snd_urbs, uint, S_IRUGO);
  113. MODULE_PARM_DESC(num_snd_urbs,
  114. "Number of urbs used for transmission (range: 0-"
  115. __MODULE_STRING(UDSL_MAX_SND_URBS) ", default: "
  116. __MODULE_STRING(UDSL_DEFAULT_SND_URBS) ")");
  117. module_param(rcv_buf_bytes, uint, S_IRUGO);
  118. MODULE_PARM_DESC(rcv_buf_bytes,
  119. "Size of the buffers used for reception, in bytes (range: 1-"
  120. __MODULE_STRING(UDSL_MAX_BUF_SIZE) ", default: "
  121. __MODULE_STRING(UDSL_DEFAULT_RCV_BUF_SIZE) ")");
  122. module_param(snd_buf_bytes, uint, S_IRUGO);
  123. MODULE_PARM_DESC(snd_buf_bytes,
  124. "Size of the buffers used for transmission, in bytes (range: 1-"
  125. __MODULE_STRING(UDSL_MAX_BUF_SIZE) ", default: "
  126. __MODULE_STRING(UDSL_DEFAULT_SND_BUF_SIZE) ")");
  127. /* receive */
  128. struct usbatm_vcc_data {
  129. /* vpi/vci lookup */
  130. struct list_head list;
  131. short vpi;
  132. int vci;
  133. struct atm_vcc *vcc;
  134. /* raw cell reassembly */
  135. struct sk_buff *sarb;
  136. };
  137. /* send */
  138. struct usbatm_control {
  139. struct atm_skb_data atm;
  140. u32 len;
  141. u32 crc;
  142. };
  143. #define UDSL_SKB(x) ((struct usbatm_control *)(x)->cb)
  144. /* ATM */
  145. static void usbatm_atm_dev_close(struct atm_dev *atm_dev);
  146. static int usbatm_atm_open(struct atm_vcc *vcc);
  147. static void usbatm_atm_close(struct atm_vcc *vcc);
  148. static int usbatm_atm_ioctl(struct atm_dev *atm_dev, unsigned int cmd, void __user * arg);
  149. static int usbatm_atm_send(struct atm_vcc *vcc, struct sk_buff *skb);
  150. static int usbatm_atm_proc_read(struct atm_dev *atm_dev, loff_t * pos, char *page);
  151. static struct atmdev_ops usbatm_atm_devops = {
  152. .dev_close = usbatm_atm_dev_close,
  153. .open = usbatm_atm_open,
  154. .close = usbatm_atm_close,
  155. .ioctl = usbatm_atm_ioctl,
  156. .send = usbatm_atm_send,
  157. .proc_read = usbatm_atm_proc_read,
  158. .owner = THIS_MODULE,
  159. };
  160. /***********
  161. ** misc **
  162. ***********/
  163. static inline unsigned int usbatm_pdu_length(unsigned int length)
  164. {
  165. length += ATM_CELL_PAYLOAD - 1 + ATM_AAL5_TRAILER;
  166. return length - length % ATM_CELL_PAYLOAD;
  167. }
  168. static inline void usbatm_pop(struct atm_vcc *vcc, struct sk_buff *skb)
  169. {
  170. if (vcc->pop)
  171. vcc->pop(vcc, skb);
  172. else
  173. dev_kfree_skb_any(skb);
  174. }
  175. /***********
  176. ** urbs **
  177. ************/
  178. static struct urb *usbatm_pop_urb(struct usbatm_channel *channel)
  179. {
  180. struct urb *urb;
  181. spin_lock_irq(&channel->lock);
  182. if (list_empty(&channel->list)) {
  183. spin_unlock_irq(&channel->lock);
  184. return NULL;
  185. }
  186. urb = list_entry(channel->list.next, struct urb, urb_list);
  187. list_del(&urb->urb_list);
  188. spin_unlock_irq(&channel->lock);
  189. return urb;
  190. }
  191. static int usbatm_submit_urb(struct urb *urb)
  192. {
  193. struct usbatm_channel *channel = urb->context;
  194. int ret;
  195. /* vdbg("%s: submitting urb 0x%p, size %u",
  196. __func__, urb, urb->transfer_buffer_length); */
  197. ret = usb_submit_urb(urb, GFP_ATOMIC);
  198. if (ret) {
  199. if (printk_ratelimit())
  200. atm_warn(channel->usbatm, "%s: urb 0x%p submission failed (%d)!\n",
  201. __func__, urb, ret);
  202. /* consider all errors transient and return the buffer back to the queue */
  203. urb->status = -EAGAIN;
  204. spin_lock_irq(&channel->lock);
  205. /* must add to the front when sending; doesn't matter when receiving */
  206. list_add(&urb->urb_list, &channel->list);
  207. spin_unlock_irq(&channel->lock);
  208. /* make sure the channel doesn't stall */
  209. mod_timer(&channel->delay, jiffies + msecs_to_jiffies(THROTTLE_MSECS));
  210. }
  211. return ret;
  212. }
  213. static void usbatm_complete(struct urb *urb)
  214. {
  215. struct usbatm_channel *channel = urb->context;
  216. unsigned long flags;
  217. int status = urb->status;
  218. /* vdbg("%s: urb 0x%p, status %d, actual_length %d",
  219. __func__, urb, status, urb->actual_length); */
  220. /* usually in_interrupt(), but not always */
  221. spin_lock_irqsave(&channel->lock, flags);
  222. /* must add to the back when receiving; doesn't matter when sending */
  223. list_add_tail(&urb->urb_list, &channel->list);
  224. spin_unlock_irqrestore(&channel->lock, flags);
  225. if (unlikely(status) &&
  226. (!(channel->usbatm->flags & UDSL_IGNORE_EILSEQ) ||
  227. status != -EILSEQ)) {
  228. if (status == -ESHUTDOWN)
  229. return;
  230. if (printk_ratelimit())
  231. atm_warn(channel->usbatm, "%s: urb 0x%p failed (%d)!\n",
  232. __func__, urb, status);
  233. /* throttle processing in case of an error */
  234. mod_timer(&channel->delay, jiffies + msecs_to_jiffies(THROTTLE_MSECS));
  235. } else
  236. tasklet_schedule(&channel->tasklet);
  237. }
  238. /*************
  239. ** decode **
  240. *************/
  241. static inline struct usbatm_vcc_data *usbatm_find_vcc(struct usbatm_data *instance,
  242. short vpi, int vci)
  243. {
  244. struct usbatm_vcc_data *vcc_data;
  245. list_for_each_entry(vcc_data, &instance->vcc_list, list)
  246. if ((vcc_data->vci == vci) && (vcc_data->vpi == vpi))
  247. return vcc_data;
  248. return NULL;
  249. }
  250. static void usbatm_extract_one_cell(struct usbatm_data *instance, unsigned char *source)
  251. {
  252. struct atm_vcc *vcc;
  253. struct sk_buff *sarb;
  254. short vpi = ((source[0] & 0x0f) << 4) | (source[1] >> 4);
  255. int vci = ((source[1] & 0x0f) << 12) | (source[2] << 4) | (source[3] >> 4);
  256. u8 pti = ((source[3] & 0xe) >> 1);
  257. vdbg(&instance->usb_intf->dev, "%s: vpi %hd, vci %d, pti %d", __func__, vpi, vci, pti);
  258. if ((vci != instance->cached_vci) || (vpi != instance->cached_vpi)) {
  259. instance->cached_vpi = vpi;
  260. instance->cached_vci = vci;
  261. instance->cached_vcc = usbatm_find_vcc(instance, vpi, vci);
  262. if (!instance->cached_vcc)
  263. atm_rldbg(instance, "%s: unknown vpi/vci (%hd/%d)!\n", __func__, vpi, vci);
  264. }
  265. if (!instance->cached_vcc)
  266. return;
  267. vcc = instance->cached_vcc->vcc;
  268. /* OAM F5 end-to-end */
  269. if (pti == ATM_PTI_E2EF5) {
  270. if (printk_ratelimit())
  271. atm_warn(instance, "%s: OAM not supported (vpi %d, vci %d)!\n",
  272. __func__, vpi, vci);
  273. atomic_inc(&vcc->stats->rx_err);
  274. return;
  275. }
  276. sarb = instance->cached_vcc->sarb;
  277. if (sarb->tail + ATM_CELL_PAYLOAD > sarb->end) {
  278. atm_rldbg(instance, "%s: buffer overrun (sarb->len %u, vcc: 0x%p)!\n",
  279. __func__, sarb->len, vcc);
  280. /* discard cells already received */
  281. skb_trim(sarb, 0);
  282. UDSL_ASSERT(instance, sarb->tail + ATM_CELL_PAYLOAD <= sarb->end);
  283. }
  284. memcpy(skb_tail_pointer(sarb), source + ATM_CELL_HEADER, ATM_CELL_PAYLOAD);
  285. __skb_put(sarb, ATM_CELL_PAYLOAD);
  286. if (pti & 1) {
  287. struct sk_buff *skb;
  288. unsigned int length;
  289. unsigned int pdu_length;
  290. length = (source[ATM_CELL_SIZE - 6] << 8) + source[ATM_CELL_SIZE - 5];
  291. /* guard against overflow */
  292. if (length > ATM_MAX_AAL5_PDU) {
  293. atm_rldbg(instance, "%s: bogus length %u (vcc: 0x%p)!\n",
  294. __func__, length, vcc);
  295. atomic_inc(&vcc->stats->rx_err);
  296. goto out;
  297. }
  298. pdu_length = usbatm_pdu_length(length);
  299. if (sarb->len < pdu_length) {
  300. atm_rldbg(instance, "%s: bogus pdu_length %u (sarb->len: %u, vcc: 0x%p)!\n",
  301. __func__, pdu_length, sarb->len, vcc);
  302. atomic_inc(&vcc->stats->rx_err);
  303. goto out;
  304. }
  305. if (crc32_be(~0, skb_tail_pointer(sarb) - pdu_length, pdu_length) != 0xc704dd7b) {
  306. atm_rldbg(instance, "%s: packet failed crc check (vcc: 0x%p)!\n",
  307. __func__, vcc);
  308. atomic_inc(&vcc->stats->rx_err);
  309. goto out;
  310. }
  311. vdbg(&instance->usb_intf->dev,
  312. "%s: got packet (length: %u, pdu_length: %u, vcc: 0x%p)",
  313. __func__, length, pdu_length, vcc);
  314. if (!(skb = dev_alloc_skb(length))) {
  315. if (printk_ratelimit())
  316. atm_err(instance, "%s: no memory for skb (length: %u)!\n",
  317. __func__, length);
  318. atomic_inc(&vcc->stats->rx_drop);
  319. goto out;
  320. }
  321. vdbg(&instance->usb_intf->dev,
  322. "%s: allocated new sk_buff (skb: 0x%p, skb->truesize: %u)",
  323. __func__, skb, skb->truesize);
  324. if (!atm_charge(vcc, skb->truesize)) {
  325. atm_rldbg(instance, "%s: failed atm_charge (skb->truesize: %u)!\n",
  326. __func__, skb->truesize);
  327. dev_kfree_skb_any(skb);
  328. goto out; /* atm_charge increments rx_drop */
  329. }
  330. skb_copy_to_linear_data(skb,
  331. skb_tail_pointer(sarb) - pdu_length,
  332. length);
  333. __skb_put(skb, length);
  334. vdbg(&instance->usb_intf->dev,
  335. "%s: sending skb 0x%p, skb->len %u, skb->truesize %u",
  336. __func__, skb, skb->len, skb->truesize);
  337. PACKETDEBUG(instance, skb->data, skb->len);
  338. vcc->push(vcc, skb);
  339. atomic_inc(&vcc->stats->rx);
  340. out:
  341. skb_trim(sarb, 0);
  342. }
  343. }
  344. static void usbatm_extract_cells(struct usbatm_data *instance,
  345. unsigned char *source, unsigned int avail_data)
  346. {
  347. unsigned int stride = instance->rx_channel.stride;
  348. unsigned int buf_usage = instance->buf_usage;
  349. /* extract cells from incoming data, taking into account that
  350. * the length of avail data may not be a multiple of stride */
  351. if (buf_usage > 0) {
  352. /* we have a partially received atm cell */
  353. unsigned char *cell_buf = instance->cell_buf;
  354. unsigned int space_left = stride - buf_usage;
  355. UDSL_ASSERT(instance, buf_usage <= stride);
  356. if (avail_data >= space_left) {
  357. /* add new data and process cell */
  358. memcpy(cell_buf + buf_usage, source, space_left);
  359. source += space_left;
  360. avail_data -= space_left;
  361. usbatm_extract_one_cell(instance, cell_buf);
  362. instance->buf_usage = 0;
  363. } else {
  364. /* not enough data to fill the cell */
  365. memcpy(cell_buf + buf_usage, source, avail_data);
  366. instance->buf_usage = buf_usage + avail_data;
  367. return;
  368. }
  369. }
  370. for (; avail_data >= stride; avail_data -= stride, source += stride)
  371. usbatm_extract_one_cell(instance, source);
  372. if (avail_data > 0) {
  373. /* length was not a multiple of stride -
  374. * save remaining data for next call */
  375. memcpy(instance->cell_buf, source, avail_data);
  376. instance->buf_usage = avail_data;
  377. }
  378. }
  379. /*************
  380. ** encode **
  381. *************/
  382. static unsigned int usbatm_write_cells(struct usbatm_data *instance,
  383. struct sk_buff *skb,
  384. u8 *target, unsigned int avail_space)
  385. {
  386. struct usbatm_control *ctrl = UDSL_SKB(skb);
  387. struct atm_vcc *vcc = ctrl->atm.vcc;
  388. unsigned int bytes_written;
  389. unsigned int stride = instance->tx_channel.stride;
  390. vdbg(&instance->usb_intf->dev, "%s: skb->len=%d, avail_space=%u",
  391. __func__, skb->len, avail_space);
  392. UDSL_ASSERT(instance, !(avail_space % stride));
  393. for (bytes_written = 0; bytes_written < avail_space && ctrl->len;
  394. bytes_written += stride, target += stride) {
  395. unsigned int data_len = min_t(unsigned int, skb->len, ATM_CELL_PAYLOAD);
  396. unsigned int left = ATM_CELL_PAYLOAD - data_len;
  397. u8 *ptr = target;
  398. ptr[0] = vcc->vpi >> 4;
  399. ptr[1] = (vcc->vpi << 4) | (vcc->vci >> 12);
  400. ptr[2] = vcc->vci >> 4;
  401. ptr[3] = vcc->vci << 4;
  402. ptr[4] = 0xec;
  403. ptr += ATM_CELL_HEADER;
  404. skb_copy_from_linear_data(skb, ptr, data_len);
  405. ptr += data_len;
  406. __skb_pull(skb, data_len);
  407. if (!left)
  408. continue;
  409. memset(ptr, 0, left);
  410. if (left >= ATM_AAL5_TRAILER) { /* trailer will go in this cell */
  411. u8 *trailer = target + ATM_CELL_SIZE - ATM_AAL5_TRAILER;
  412. /* trailer[0] = 0; UU = 0 */
  413. /* trailer[1] = 0; CPI = 0 */
  414. trailer[2] = ctrl->len >> 8;
  415. trailer[3] = ctrl->len;
  416. ctrl->crc = ~crc32_be(ctrl->crc, ptr, left - 4);
  417. trailer[4] = ctrl->crc >> 24;
  418. trailer[5] = ctrl->crc >> 16;
  419. trailer[6] = ctrl->crc >> 8;
  420. trailer[7] = ctrl->crc;
  421. target[3] |= 0x2; /* adjust PTI */
  422. ctrl->len = 0; /* tag this skb finished */
  423. } else
  424. ctrl->crc = crc32_be(ctrl->crc, ptr, left);
  425. }
  426. return bytes_written;
  427. }
  428. /**************
  429. ** receive **
  430. **************/
  431. static void usbatm_rx_process(unsigned long data)
  432. {
  433. struct usbatm_data *instance = (struct usbatm_data *)data;
  434. struct urb *urb;
  435. while ((urb = usbatm_pop_urb(&instance->rx_channel))) {
  436. vdbg(&instance->usb_intf->dev,
  437. "%s: processing urb 0x%p", __func__, urb);
  438. if (usb_pipeisoc(urb->pipe)) {
  439. unsigned char *merge_start = NULL;
  440. unsigned int merge_length = 0;
  441. const unsigned int packet_size = instance->rx_channel.packet_size;
  442. int i;
  443. for (i = 0; i < urb->number_of_packets; i++) {
  444. if (!urb->iso_frame_desc[i].status) {
  445. unsigned int actual_length = urb->iso_frame_desc[i].actual_length;
  446. UDSL_ASSERT(instance, actual_length <= packet_size);
  447. if (!merge_length)
  448. merge_start = (unsigned char *)urb->transfer_buffer + urb->iso_frame_desc[i].offset;
  449. merge_length += actual_length;
  450. if (merge_length && (actual_length < packet_size)) {
  451. usbatm_extract_cells(instance, merge_start, merge_length);
  452. merge_length = 0;
  453. }
  454. } else {
  455. atm_rldbg(instance, "%s: status %d in frame %d!\n", __func__, urb->status, i);
  456. if (merge_length)
  457. usbatm_extract_cells(instance, merge_start, merge_length);
  458. merge_length = 0;
  459. instance->buf_usage = 0;
  460. }
  461. }
  462. if (merge_length)
  463. usbatm_extract_cells(instance, merge_start, merge_length);
  464. } else
  465. if (!urb->status)
  466. usbatm_extract_cells(instance, urb->transfer_buffer, urb->actual_length);
  467. else
  468. instance->buf_usage = 0;
  469. if (usbatm_submit_urb(urb))
  470. return;
  471. }
  472. }
  473. /***********
  474. ** send **
  475. ***********/
  476. static void usbatm_tx_process(unsigned long data)
  477. {
  478. struct usbatm_data *instance = (struct usbatm_data *)data;
  479. struct sk_buff *skb = instance->current_skb;
  480. struct urb *urb = NULL;
  481. const unsigned int buf_size = instance->tx_channel.buf_size;
  482. unsigned int bytes_written = 0;
  483. u8 *buffer = NULL;
  484. if (!skb)
  485. skb = skb_dequeue(&instance->sndqueue);
  486. while (skb) {
  487. if (!urb) {
  488. urb = usbatm_pop_urb(&instance->tx_channel);
  489. if (!urb)
  490. break; /* no more senders */
  491. buffer = urb->transfer_buffer;
  492. bytes_written = (urb->status == -EAGAIN) ?
  493. urb->transfer_buffer_length : 0;
  494. }
  495. bytes_written += usbatm_write_cells(instance, skb,
  496. buffer + bytes_written,
  497. buf_size - bytes_written);
  498. vdbg(&instance->usb_intf->dev,
  499. "%s: wrote %u bytes from skb 0x%p to urb 0x%p",
  500. __func__, bytes_written, skb, urb);
  501. if (!UDSL_SKB(skb)->len) {
  502. struct atm_vcc *vcc = UDSL_SKB(skb)->atm.vcc;
  503. usbatm_pop(vcc, skb);
  504. atomic_inc(&vcc->stats->tx);
  505. skb = skb_dequeue(&instance->sndqueue);
  506. }
  507. if (bytes_written == buf_size || (!skb && bytes_written)) {
  508. urb->transfer_buffer_length = bytes_written;
  509. if (usbatm_submit_urb(urb))
  510. break;
  511. urb = NULL;
  512. }
  513. }
  514. instance->current_skb = skb;
  515. }
  516. static void usbatm_cancel_send(struct usbatm_data *instance,
  517. struct atm_vcc *vcc)
  518. {
  519. struct sk_buff *skb, *n;
  520. atm_dbg(instance, "%s entered\n", __func__);
  521. spin_lock_irq(&instance->sndqueue.lock);
  522. skb_queue_walk_safe(&instance->sndqueue, skb, n) {
  523. if (UDSL_SKB(skb)->atm.vcc == vcc) {
  524. atm_dbg(instance, "%s: popping skb 0x%p\n", __func__, skb);
  525. __skb_unlink(skb, &instance->sndqueue);
  526. usbatm_pop(vcc, skb);
  527. }
  528. }
  529. spin_unlock_irq(&instance->sndqueue.lock);
  530. tasklet_disable(&instance->tx_channel.tasklet);
  531. if ((skb = instance->current_skb) && (UDSL_SKB(skb)->atm.vcc == vcc)) {
  532. atm_dbg(instance, "%s: popping current skb (0x%p)\n", __func__, skb);
  533. instance->current_skb = NULL;
  534. usbatm_pop(vcc, skb);
  535. }
  536. tasklet_enable(&instance->tx_channel.tasklet);
  537. atm_dbg(instance, "%s done\n", __func__);
  538. }
  539. static int usbatm_atm_send(struct atm_vcc *vcc, struct sk_buff *skb)
  540. {
  541. struct usbatm_data *instance = vcc->dev->dev_data;
  542. struct usbatm_control *ctrl = UDSL_SKB(skb);
  543. int err;
  544. /* racy disconnection check - fine */
  545. if (!instance || instance->disconnected) {
  546. #ifdef DEBUG
  547. printk_ratelimited(KERN_DEBUG "%s: %s!\n", __func__, instance ? "disconnected" : "NULL instance");
  548. #endif
  549. err = -ENODEV;
  550. goto fail;
  551. }
  552. vdbg(&instance->usb_intf->dev, "%s called (skb 0x%p, len %u)", __func__,
  553. skb, skb->len);
  554. if (vcc->qos.aal != ATM_AAL5) {
  555. atm_rldbg(instance, "%s: unsupported ATM type %d!\n", __func__, vcc->qos.aal);
  556. err = -EINVAL;
  557. goto fail;
  558. }
  559. if (skb->len > ATM_MAX_AAL5_PDU) {
  560. atm_rldbg(instance, "%s: packet too long (%d vs %d)!\n",
  561. __func__, skb->len, ATM_MAX_AAL5_PDU);
  562. err = -EINVAL;
  563. goto fail;
  564. }
  565. PACKETDEBUG(instance, skb->data, skb->len);
  566. /* initialize the control block */
  567. ctrl->atm.vcc = vcc;
  568. ctrl->len = skb->len;
  569. ctrl->crc = crc32_be(~0, skb->data, skb->len);
  570. skb_queue_tail(&instance->sndqueue, skb);
  571. tasklet_schedule(&instance->tx_channel.tasklet);
  572. return 0;
  573. fail:
  574. usbatm_pop(vcc, skb);
  575. return err;
  576. }
  577. /********************
  578. ** bean counting **
  579. ********************/
  580. static void usbatm_destroy_instance(struct kref *kref)
  581. {
  582. struct usbatm_data *instance = container_of(kref, struct usbatm_data, refcount);
  583. usb_dbg(instance, "%s\n", __func__);
  584. tasklet_kill(&instance->rx_channel.tasklet);
  585. tasklet_kill(&instance->tx_channel.tasklet);
  586. usb_put_dev(instance->usb_dev);
  587. kfree(instance);
  588. }
  589. static void usbatm_get_instance(struct usbatm_data *instance)
  590. {
  591. usb_dbg(instance, "%s\n", __func__);
  592. kref_get(&instance->refcount);
  593. }
  594. static void usbatm_put_instance(struct usbatm_data *instance)
  595. {
  596. usb_dbg(instance, "%s\n", __func__);
  597. kref_put(&instance->refcount, usbatm_destroy_instance);
  598. }
  599. /**********
  600. ** ATM **
  601. **********/
  602. static void usbatm_atm_dev_close(struct atm_dev *atm_dev)
  603. {
  604. struct usbatm_data *instance = atm_dev->dev_data;
  605. if (!instance)
  606. return;
  607. usb_dbg(instance, "%s\n", __func__);
  608. atm_dev->dev_data = NULL; /* catch bugs */
  609. usbatm_put_instance(instance); /* taken in usbatm_atm_init */
  610. }
  611. static int usbatm_atm_proc_read(struct atm_dev *atm_dev, loff_t * pos, char *page)
  612. {
  613. struct usbatm_data *instance = atm_dev->dev_data;
  614. int left = *pos;
  615. if (!instance)
  616. return -ENODEV;
  617. if (!left--)
  618. return sprintf(page, "%s\n", instance->description);
  619. if (!left--)
  620. return sprintf(page, "MAC: %pM\n", atm_dev->esi);
  621. if (!left--)
  622. return sprintf(page,
  623. "AAL5: tx %d ( %d err ), rx %d ( %d err, %d drop )\n",
  624. atomic_read(&atm_dev->stats.aal5.tx),
  625. atomic_read(&atm_dev->stats.aal5.tx_err),
  626. atomic_read(&atm_dev->stats.aal5.rx),
  627. atomic_read(&atm_dev->stats.aal5.rx_err),
  628. atomic_read(&atm_dev->stats.aal5.rx_drop));
  629. if (!left--) {
  630. if (instance->disconnected)
  631. return sprintf(page, "Disconnected\n");
  632. else
  633. switch (atm_dev->signal) {
  634. case ATM_PHY_SIG_FOUND:
  635. return sprintf(page, "Line up\n");
  636. case ATM_PHY_SIG_LOST:
  637. return sprintf(page, "Line down\n");
  638. default:
  639. return sprintf(page, "Line state unknown\n");
  640. }
  641. }
  642. return 0;
  643. }
  644. static int usbatm_atm_open(struct atm_vcc *vcc)
  645. {
  646. struct usbatm_data *instance = vcc->dev->dev_data;
  647. struct usbatm_vcc_data *new = NULL;
  648. int ret;
  649. int vci = vcc->vci;
  650. short vpi = vcc->vpi;
  651. if (!instance)
  652. return -ENODEV;
  653. atm_dbg(instance, "%s: vpi %hd, vci %d\n", __func__, vpi, vci);
  654. /* only support AAL5 */
  655. if ((vcc->qos.aal != ATM_AAL5)) {
  656. atm_warn(instance, "%s: unsupported ATM type %d!\n", __func__, vcc->qos.aal);
  657. return -EINVAL;
  658. }
  659. /* sanity checks */
  660. if ((vcc->qos.rxtp.max_sdu < 0) || (vcc->qos.rxtp.max_sdu > ATM_MAX_AAL5_PDU)) {
  661. atm_dbg(instance, "%s: max_sdu %d out of range!\n", __func__, vcc->qos.rxtp.max_sdu);
  662. return -EINVAL;
  663. }
  664. mutex_lock(&instance->serialize); /* vs self, usbatm_atm_close, usbatm_usb_disconnect */
  665. if (instance->disconnected) {
  666. atm_dbg(instance, "%s: disconnected!\n", __func__);
  667. ret = -ENODEV;
  668. goto fail;
  669. }
  670. if (usbatm_find_vcc(instance, vpi, vci)) {
  671. atm_dbg(instance, "%s: %hd/%d already in use!\n", __func__, vpi, vci);
  672. ret = -EADDRINUSE;
  673. goto fail;
  674. }
  675. if (!(new = kzalloc(sizeof(struct usbatm_vcc_data), GFP_KERNEL))) {
  676. atm_err(instance, "%s: no memory for vcc_data!\n", __func__);
  677. ret = -ENOMEM;
  678. goto fail;
  679. }
  680. new->vcc = vcc;
  681. new->vpi = vpi;
  682. new->vci = vci;
  683. new->sarb = alloc_skb(usbatm_pdu_length(vcc->qos.rxtp.max_sdu), GFP_KERNEL);
  684. if (!new->sarb) {
  685. atm_err(instance, "%s: no memory for SAR buffer!\n", __func__);
  686. ret = -ENOMEM;
  687. goto fail;
  688. }
  689. vcc->dev_data = new;
  690. tasklet_disable(&instance->rx_channel.tasklet);
  691. instance->cached_vcc = new;
  692. instance->cached_vpi = vpi;
  693. instance->cached_vci = vci;
  694. list_add(&new->list, &instance->vcc_list);
  695. tasklet_enable(&instance->rx_channel.tasklet);
  696. set_bit(ATM_VF_ADDR, &vcc->flags);
  697. set_bit(ATM_VF_PARTIAL, &vcc->flags);
  698. set_bit(ATM_VF_READY, &vcc->flags);
  699. mutex_unlock(&instance->serialize);
  700. atm_dbg(instance, "%s: allocated vcc data 0x%p\n", __func__, new);
  701. return 0;
  702. fail:
  703. kfree(new);
  704. mutex_unlock(&instance->serialize);
  705. return ret;
  706. }
  707. static void usbatm_atm_close(struct atm_vcc *vcc)
  708. {
  709. struct usbatm_data *instance = vcc->dev->dev_data;
  710. struct usbatm_vcc_data *vcc_data = vcc->dev_data;
  711. if (!instance || !vcc_data)
  712. return;
  713. atm_dbg(instance, "%s entered\n", __func__);
  714. atm_dbg(instance, "%s: deallocating vcc 0x%p with vpi %d vci %d\n",
  715. __func__, vcc_data, vcc_data->vpi, vcc_data->vci);
  716. usbatm_cancel_send(instance, vcc);
  717. mutex_lock(&instance->serialize); /* vs self, usbatm_atm_open, usbatm_usb_disconnect */
  718. tasklet_disable(&instance->rx_channel.tasklet);
  719. if (instance->cached_vcc == vcc_data) {
  720. instance->cached_vcc = NULL;
  721. instance->cached_vpi = ATM_VPI_UNSPEC;
  722. instance->cached_vci = ATM_VCI_UNSPEC;
  723. }
  724. list_del(&vcc_data->list);
  725. tasklet_enable(&instance->rx_channel.tasklet);
  726. kfree_skb(vcc_data->sarb);
  727. vcc_data->sarb = NULL;
  728. kfree(vcc_data);
  729. vcc->dev_data = NULL;
  730. vcc->vpi = ATM_VPI_UNSPEC;
  731. vcc->vci = ATM_VCI_UNSPEC;
  732. clear_bit(ATM_VF_READY, &vcc->flags);
  733. clear_bit(ATM_VF_PARTIAL, &vcc->flags);
  734. clear_bit(ATM_VF_ADDR, &vcc->flags);
  735. mutex_unlock(&instance->serialize);
  736. atm_dbg(instance, "%s successful\n", __func__);
  737. }
  738. static int usbatm_atm_ioctl(struct atm_dev *atm_dev, unsigned int cmd,
  739. void __user * arg)
  740. {
  741. struct usbatm_data *instance = atm_dev->dev_data;
  742. if (!instance || instance->disconnected)
  743. return -ENODEV;
  744. switch (cmd) {
  745. case ATM_QUERYLOOP:
  746. return put_user(ATM_LM_NONE, (int __user *)arg) ? -EFAULT : 0;
  747. default:
  748. return -ENOIOCTLCMD;
  749. }
  750. }
  751. static int usbatm_atm_init(struct usbatm_data *instance)
  752. {
  753. struct atm_dev *atm_dev;
  754. int ret, i;
  755. /* ATM init. The ATM initialization scheme suffers from an intrinsic race
  756. * condition: callbacks we register can be executed at once, before we have
  757. * initialized the struct atm_dev. To protect against this, all callbacks
  758. * abort if atm_dev->dev_data is NULL. */
  759. atm_dev = atm_dev_register(instance->driver_name,
  760. &instance->usb_intf->dev, &usbatm_atm_devops,
  761. -1, NULL);
  762. if (!atm_dev) {
  763. usb_err(instance, "%s: failed to register ATM device!\n", __func__);
  764. return -1;
  765. }
  766. instance->atm_dev = atm_dev;
  767. atm_dev->ci_range.vpi_bits = ATM_CI_MAX;
  768. atm_dev->ci_range.vci_bits = ATM_CI_MAX;
  769. atm_dev->signal = ATM_PHY_SIG_UNKNOWN;
  770. /* temp init ATM device, set to 128kbit */
  771. atm_dev->link_rate = 128 * 1000 / 424;
  772. if (instance->driver->atm_start && ((ret = instance->driver->atm_start(instance, atm_dev)) < 0)) {
  773. atm_err(instance, "%s: atm_start failed: %d!\n", __func__, ret);
  774. goto fail;
  775. }
  776. usbatm_get_instance(instance); /* dropped in usbatm_atm_dev_close */
  777. /* ready for ATM callbacks */
  778. mb();
  779. atm_dev->dev_data = instance;
  780. /* submit all rx URBs */
  781. for (i = 0; i < num_rcv_urbs; i++)
  782. usbatm_submit_urb(instance->urbs[i]);
  783. return 0;
  784. fail:
  785. instance->atm_dev = NULL;
  786. atm_dev_deregister(atm_dev); /* usbatm_atm_dev_close will eventually be called */
  787. return ret;
  788. }
  789. /**********
  790. ** USB **
  791. **********/
  792. static int usbatm_do_heavy_init(void *arg)
  793. {
  794. struct usbatm_data *instance = arg;
  795. int ret;
  796. allow_signal(SIGTERM);
  797. complete(&instance->thread_started);
  798. ret = instance->driver->heavy_init(instance, instance->usb_intf);
  799. if (!ret)
  800. ret = usbatm_atm_init(instance);
  801. mutex_lock(&instance->serialize);
  802. instance->thread = NULL;
  803. mutex_unlock(&instance->serialize);
  804. complete_and_exit(&instance->thread_exited, ret);
  805. }
  806. static int usbatm_heavy_init(struct usbatm_data *instance)
  807. {
  808. struct task_struct *t;
  809. t = kthread_create(usbatm_do_heavy_init, instance, "%s",
  810. instance->driver->driver_name);
  811. if (IS_ERR(t)) {
  812. usb_err(instance, "%s: failed to create kernel_thread (%ld)!\n",
  813. __func__, PTR_ERR(t));
  814. return PTR_ERR(t);
  815. }
  816. instance->thread = t;
  817. wake_up_process(t);
  818. wait_for_completion(&instance->thread_started);
  819. return 0;
  820. }
  821. static void usbatm_tasklet_schedule(unsigned long data)
  822. {
  823. tasklet_schedule((struct tasklet_struct *) data);
  824. }
  825. static void usbatm_init_channel(struct usbatm_channel *channel)
  826. {
  827. spin_lock_init(&channel->lock);
  828. INIT_LIST_HEAD(&channel->list);
  829. channel->delay.function = usbatm_tasklet_schedule;
  830. channel->delay.data = (unsigned long) &channel->tasklet;
  831. init_timer(&channel->delay);
  832. }
  833. int usbatm_usb_probe(struct usb_interface *intf, const struct usb_device_id *id,
  834. struct usbatm_driver *driver)
  835. {
  836. struct device *dev = &intf->dev;
  837. struct usb_device *usb_dev = interface_to_usbdev(intf);
  838. struct usbatm_data *instance;
  839. char *buf;
  840. int error = -ENOMEM;
  841. int i, length;
  842. unsigned int maxpacket, num_packets;
  843. dev_dbg(dev, "%s: trying driver %s with vendor=%04x, product=%04x, ifnum %2d\n",
  844. __func__, driver->driver_name,
  845. le16_to_cpu(usb_dev->descriptor.idVendor),
  846. le16_to_cpu(usb_dev->descriptor.idProduct),
  847. intf->altsetting->desc.bInterfaceNumber);
  848. /* instance init */
  849. instance = kzalloc(sizeof(*instance) + sizeof(struct urb *) * (num_rcv_urbs + num_snd_urbs), GFP_KERNEL);
  850. if (!instance) {
  851. dev_err(dev, "%s: no memory for instance data!\n", __func__);
  852. return -ENOMEM;
  853. }
  854. /* public fields */
  855. instance->driver = driver;
  856. strlcpy(instance->driver_name, driver->driver_name,
  857. sizeof(instance->driver_name));
  858. instance->usb_dev = usb_dev;
  859. instance->usb_intf = intf;
  860. buf = instance->description;
  861. length = sizeof(instance->description);
  862. if ((i = usb_string(usb_dev, usb_dev->descriptor.iProduct, buf, length)) < 0)
  863. goto bind;
  864. buf += i;
  865. length -= i;
  866. i = scnprintf(buf, length, " (");
  867. buf += i;
  868. length -= i;
  869. if (length <= 0 || (i = usb_make_path(usb_dev, buf, length)) < 0)
  870. goto bind;
  871. buf += i;
  872. length -= i;
  873. snprintf(buf, length, ")");
  874. bind:
  875. if (driver->bind && (error = driver->bind(instance, intf, id)) < 0) {
  876. dev_err(dev, "%s: bind failed: %d!\n", __func__, error);
  877. goto fail_free;
  878. }
  879. /* private fields */
  880. kref_init(&instance->refcount); /* dropped in usbatm_usb_disconnect */
  881. mutex_init(&instance->serialize);
  882. instance->thread = NULL;
  883. init_completion(&instance->thread_started);
  884. init_completion(&instance->thread_exited);
  885. INIT_LIST_HEAD(&instance->vcc_list);
  886. skb_queue_head_init(&instance->sndqueue);
  887. usbatm_init_channel(&instance->rx_channel);
  888. usbatm_init_channel(&instance->tx_channel);
  889. tasklet_init(&instance->rx_channel.tasklet, usbatm_rx_process, (unsigned long)instance);
  890. tasklet_init(&instance->tx_channel.tasklet, usbatm_tx_process, (unsigned long)instance);
  891. instance->rx_channel.stride = ATM_CELL_SIZE + driver->rx_padding;
  892. instance->tx_channel.stride = ATM_CELL_SIZE + driver->tx_padding;
  893. instance->rx_channel.usbatm = instance->tx_channel.usbatm = instance;
  894. if ((instance->flags & UDSL_USE_ISOC) && driver->isoc_in)
  895. instance->rx_channel.endpoint = usb_rcvisocpipe(usb_dev, driver->isoc_in);
  896. else
  897. instance->rx_channel.endpoint = usb_rcvbulkpipe(usb_dev, driver->bulk_in);
  898. instance->tx_channel.endpoint = usb_sndbulkpipe(usb_dev, driver->bulk_out);
  899. /* tx buffer size must be a positive multiple of the stride */
  900. instance->tx_channel.buf_size = max(instance->tx_channel.stride,
  901. snd_buf_bytes - (snd_buf_bytes % instance->tx_channel.stride));
  902. /* rx buffer size must be a positive multiple of the endpoint maxpacket */
  903. maxpacket = usb_maxpacket(usb_dev, instance->rx_channel.endpoint, 0);
  904. if ((maxpacket < 1) || (maxpacket > UDSL_MAX_BUF_SIZE)) {
  905. dev_err(dev, "%s: invalid endpoint %02x!\n", __func__,
  906. usb_pipeendpoint(instance->rx_channel.endpoint));
  907. error = -EINVAL;
  908. goto fail_unbind;
  909. }
  910. num_packets = max(1U, (rcv_buf_bytes + maxpacket / 2) / maxpacket); /* round */
  911. if (num_packets * maxpacket > UDSL_MAX_BUF_SIZE)
  912. num_packets--;
  913. instance->rx_channel.buf_size = num_packets * maxpacket;
  914. instance->rx_channel.packet_size = maxpacket;
  915. #ifdef DEBUG
  916. for (i = 0; i < 2; i++) {
  917. struct usbatm_channel *channel = i ?
  918. &instance->tx_channel : &instance->rx_channel;
  919. dev_dbg(dev, "%s: using %d byte buffer for %s channel 0x%p\n", __func__, channel->buf_size, i ? "tx" : "rx", channel);
  920. }
  921. #endif
  922. /* initialize urbs */
  923. for (i = 0; i < num_rcv_urbs + num_snd_urbs; i++) {
  924. u8 *buffer;
  925. struct usbatm_channel *channel = i < num_rcv_urbs ?
  926. &instance->rx_channel : &instance->tx_channel;
  927. struct urb *urb;
  928. unsigned int iso_packets = usb_pipeisoc(channel->endpoint) ? channel->buf_size / channel->packet_size : 0;
  929. UDSL_ASSERT(instance, !usb_pipeisoc(channel->endpoint) || usb_pipein(channel->endpoint));
  930. urb = usb_alloc_urb(iso_packets, GFP_KERNEL);
  931. if (!urb) {
  932. dev_err(dev, "%s: no memory for urb %d!\n", __func__, i);
  933. error = -ENOMEM;
  934. goto fail_unbind;
  935. }
  936. instance->urbs[i] = urb;
  937. /* zero the tx padding to avoid leaking information */
  938. buffer = kzalloc(channel->buf_size, GFP_KERNEL);
  939. if (!buffer) {
  940. dev_err(dev, "%s: no memory for buffer %d!\n", __func__, i);
  941. error = -ENOMEM;
  942. goto fail_unbind;
  943. }
  944. usb_fill_bulk_urb(urb, instance->usb_dev, channel->endpoint,
  945. buffer, channel->buf_size, usbatm_complete, channel);
  946. if (iso_packets) {
  947. int j;
  948. urb->interval = 1;
  949. urb->transfer_flags = URB_ISO_ASAP;
  950. urb->number_of_packets = iso_packets;
  951. for (j = 0; j < iso_packets; j++) {
  952. urb->iso_frame_desc[j].offset = channel->packet_size * j;
  953. urb->iso_frame_desc[j].length = channel->packet_size;
  954. }
  955. }
  956. /* put all tx URBs on the list of spares */
  957. if (i >= num_rcv_urbs)
  958. list_add_tail(&urb->urb_list, &channel->list);
  959. vdbg(&intf->dev, "%s: alloced buffer 0x%p buf size %u urb 0x%p",
  960. __func__, urb->transfer_buffer, urb->transfer_buffer_length, urb);
  961. }
  962. instance->cached_vpi = ATM_VPI_UNSPEC;
  963. instance->cached_vci = ATM_VCI_UNSPEC;
  964. instance->cell_buf = kmalloc(instance->rx_channel.stride, GFP_KERNEL);
  965. if (!instance->cell_buf) {
  966. dev_err(dev, "%s: no memory for cell buffer!\n", __func__);
  967. error = -ENOMEM;
  968. goto fail_unbind;
  969. }
  970. if (!(instance->flags & UDSL_SKIP_HEAVY_INIT) && driver->heavy_init) {
  971. error = usbatm_heavy_init(instance);
  972. } else {
  973. complete(&instance->thread_exited); /* pretend that heavy_init was run */
  974. error = usbatm_atm_init(instance);
  975. }
  976. if (error < 0)
  977. goto fail_unbind;
  978. usb_get_dev(usb_dev);
  979. usb_set_intfdata(intf, instance);
  980. return 0;
  981. fail_unbind:
  982. if (instance->driver->unbind)
  983. instance->driver->unbind(instance, intf);
  984. fail_free:
  985. kfree(instance->cell_buf);
  986. for (i = 0; i < num_rcv_urbs + num_snd_urbs; i++) {
  987. if (instance->urbs[i])
  988. kfree(instance->urbs[i]->transfer_buffer);
  989. usb_free_urb(instance->urbs[i]);
  990. }
  991. kfree(instance);
  992. return error;
  993. }
  994. EXPORT_SYMBOL_GPL(usbatm_usb_probe);
  995. void usbatm_usb_disconnect(struct usb_interface *intf)
  996. {
  997. struct device *dev = &intf->dev;
  998. struct usbatm_data *instance = usb_get_intfdata(intf);
  999. struct usbatm_vcc_data *vcc_data;
  1000. int i;
  1001. dev_dbg(dev, "%s entered\n", __func__);
  1002. if (!instance) {
  1003. dev_dbg(dev, "%s: NULL instance!\n", __func__);
  1004. return;
  1005. }
  1006. usb_set_intfdata(intf, NULL);
  1007. mutex_lock(&instance->serialize);
  1008. instance->disconnected = 1;
  1009. if (instance->thread != NULL)
  1010. send_sig(SIGTERM, instance->thread, 1);
  1011. mutex_unlock(&instance->serialize);
  1012. wait_for_completion(&instance->thread_exited);
  1013. mutex_lock(&instance->serialize);
  1014. list_for_each_entry(vcc_data, &instance->vcc_list, list)
  1015. vcc_release_async(vcc_data->vcc, -EPIPE);
  1016. mutex_unlock(&instance->serialize);
  1017. tasklet_disable(&instance->rx_channel.tasklet);
  1018. tasklet_disable(&instance->tx_channel.tasklet);
  1019. for (i = 0; i < num_rcv_urbs + num_snd_urbs; i++)
  1020. usb_kill_urb(instance->urbs[i]);
  1021. del_timer_sync(&instance->rx_channel.delay);
  1022. del_timer_sync(&instance->tx_channel.delay);
  1023. /* turn usbatm_[rt]x_process into something close to a no-op */
  1024. /* no need to take the spinlock */
  1025. INIT_LIST_HEAD(&instance->rx_channel.list);
  1026. INIT_LIST_HEAD(&instance->tx_channel.list);
  1027. tasklet_enable(&instance->rx_channel.tasklet);
  1028. tasklet_enable(&instance->tx_channel.tasklet);
  1029. if (instance->atm_dev && instance->driver->atm_stop)
  1030. instance->driver->atm_stop(instance, instance->atm_dev);
  1031. if (instance->driver->unbind)
  1032. instance->driver->unbind(instance, intf);
  1033. instance->driver_data = NULL;
  1034. for (i = 0; i < num_rcv_urbs + num_snd_urbs; i++) {
  1035. kfree(instance->urbs[i]->transfer_buffer);
  1036. usb_free_urb(instance->urbs[i]);
  1037. }
  1038. kfree(instance->cell_buf);
  1039. /* ATM finalize */
  1040. if (instance->atm_dev) {
  1041. atm_dev_deregister(instance->atm_dev);
  1042. instance->atm_dev = NULL;
  1043. }
  1044. usbatm_put_instance(instance); /* taken in usbatm_usb_probe */
  1045. }
  1046. EXPORT_SYMBOL_GPL(usbatm_usb_disconnect);
  1047. /***********
  1048. ** init **
  1049. ***********/
  1050. static int __init usbatm_usb_init(void)
  1051. {
  1052. if (sizeof(struct usbatm_control) > FIELD_SIZEOF(struct sk_buff, cb)) {
  1053. printk(KERN_ERR "%s unusable with this kernel!\n", usbatm_driver_name);
  1054. return -EIO;
  1055. }
  1056. if ((num_rcv_urbs > UDSL_MAX_RCV_URBS)
  1057. || (num_snd_urbs > UDSL_MAX_SND_URBS)
  1058. || (rcv_buf_bytes < 1)
  1059. || (rcv_buf_bytes > UDSL_MAX_BUF_SIZE)
  1060. || (snd_buf_bytes < 1)
  1061. || (snd_buf_bytes > UDSL_MAX_BUF_SIZE))
  1062. return -EINVAL;
  1063. return 0;
  1064. }
  1065. module_init(usbatm_usb_init);
  1066. static void __exit usbatm_usb_exit(void)
  1067. {
  1068. }
  1069. module_exit(usbatm_usb_exit);
  1070. MODULE_AUTHOR(DRIVER_AUTHOR);
  1071. MODULE_DESCRIPTION(DRIVER_DESC);
  1072. MODULE_LICENSE("GPL");
  1073. MODULE_VERSION(DRIVER_VERSION);
  1074. /************
  1075. ** debug **
  1076. ************/
  1077. #ifdef VERBOSE_DEBUG
  1078. static int usbatm_print_packet(struct usbatm_data *instance,
  1079. const unsigned char *data, int len)
  1080. {
  1081. unsigned char buffer[256];
  1082. int i = 0, j = 0;
  1083. for (i = 0; i < len;) {
  1084. buffer[0] = '\0';
  1085. sprintf(buffer, "%.3d :", i);
  1086. for (j = 0; (j < 16) && (i < len); j++, i++)
  1087. sprintf(buffer, "%s %2.2x", buffer, data[i]);
  1088. dev_dbg(&instance->usb_intf->dev, "%s", buffer);
  1089. }
  1090. return i;
  1091. }
  1092. #endif