usbatm.c 37 KB

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