usbatm.c 37 KB

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