usbatm.c 35 KB

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