usbatm.c 35 KB

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