usbatm.c 37 KB

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